In This Course
In this course, you will:
1. Learn the problems that can occur as a result of improper synchronization
2. Learn how to use Selenium Webdriver explicit waits
3. Master Selenium Webdriver implicit wait
4. Understand what a DefaultWait is
5. Comprehend how to use all of the different types of waits during appropriate situations
In this Lecture
This lectures allows us to get understand deeply ExpectedConditions. You can see if an element exist by using the ExpectedConditions Class. By understanding ExpectedConditions and knowing the conditions under it makes it easier for you to check if an element actually exists.
Selenium Tutorial – Understanding the ExpectedConditions
The more important thing that I wanted to show you is this ExpectedConditions class that Selenium gives us to clean this mess up, up here. The ExpectedConditions class is really handy because it gives us many conditions that we can check to see if an element exists.
Here take a look, actually that’s so messy. Let me show you guys here. You can do ExpectedConditions. And you can see all of the ExpectedConditions. You can check if the ElementIsVisible, you can see that an AlertisPresent, the AlertState, ElementExists, ElementSelectionStateToBe, a certain selection state. You can check the InvisibilityOfElementLocated. There are many conditions that you can check to make sure that your element is actually there.
For our situation, it’s nice to see that the ElementIsVisible. Then we just passed in the elementId. If we have some kind of java script alert, we could have check that an alert is present or something like that. In this case, ElementIsVisible will suffice.
Again we are using the wait.Until class, which is going to return as either null or false or it will throw an exception if the element doesn’t exist or it might throw another random exception that is not in the list of ignore exception types for our WebDriverWait class.
You have actually already seen this run two times using this and it works exactly like this. Except now it’s much simpler and it’s easy to use because you can just have a single line statement that checks for your elements and then returns that element and you can utilize that element for your needs.
Now, I do have one other trick in my sleeve that I want to show you and that’s how to clean this up a little bit. If you don’t want to keep using this ExpectedConditions class, what you can do is actually import it statically. What’s that allows you to do is to just define your calling statements like this.
Rather than doing expectedtransitions., you can do ElementIsVisible or you can come over here and do AlertIsPresent or you can do ElementExist, like that. It’s just a little bit cleaner again depending on your preference but it’s just a little bit cleaner, a little bit shorter of the statement. If you want to use this kind of notation, you can.
Nikolay Advolodkin is a self-driven SDET on a lifelong mission to create profound change in the IT world and ultimately leave a legacy for his loved ones, community, and the world at large. Today, he serves as the CEO and Test Automation Instructor at UltimateQA.com and contributes informative articles to leading test automaton websites like SimpleProgrammer.com and TechBeacon.com