What will you learn in this lecture?

In the previous video, we learned about ExpectedConditions. In this video, we will learn how to customize the WebDriverWait.

Customizing the WebDriverWait

Now that you’re getting better familiarized with the webdriverwait, the expected conditions, the understanding of explicit waits, let me show you some custom modifications. How to clean up your code and make it look a little bit better and then of course I need to tell you about a huge announcement that came from Selenium that’s gonna change the way we do everything.

So one thing that you can do now is I’ve created this test here, test for explicit wait rendered after I’ll show you exactly what it does. However, if you wanted to get rid of these expected conditions and to just have these methods present, here what you can do is actually it’s called a static import so if I come up here and I uncomment this line of code using static openQA dot selenium dot support dot UI dot expected conditions we now have removed the need to use expected conditions anywhere that we want.

So now instead of doing that, I can do something like this so obviously a preference for me it’s a little bit more readable and obviously less code so now it’s read something like wait until element to be clickable or wait until element is visible, something like that so much more readable and again reduces code adjust the preference.

But if you would like you have that option, the other things that I wanted to introduce you to were the different configuration options that are available on the wait the wait the web driver wait which actually inherits from a default wait class but the web driver wait, what you can do is you can set an ignore exception types method.

And that will allow you to ignore different types of exceptions that might be encountered while the wait is performing its operation.

So for example, you might be able to ignore a web driver timeout exception or you might want to ignore a page let’s say no I’m not sure invalid opera castex or some other exception that you don’t want to be that you don’t want to be present, whenever this is being evaluated keep in mind however you can’t ignore the webdriver timeout exception because that is the very basis of these methods right here so you can’t ignore this one but you can ignore others.

This is an OK method.

I’ve had the very few instances where I needed that you use it but you might find the need. I’m just making you aware the other thing that you can do in order to customize your wait is you can add a message the message is something that’s going to be thrown whenever your webdriver wait fails so.

If you’re unhappy with the webdriver timeout exception message that says unable to find element bah-bah-bah waited for this amount of seconds, you can say you know something like well, here’s the element that we’re waiting for.

Let me navigate to it real quick.

I forget which I’ll finish okay so finish so try to wait with ID finish but wasn’t so it’s just something more customizable, something that’s available.

If you would like a more customized message, something that’s more user-friendly, this is something I’ve used sometimes you may find that you know just the preference whether you want a customized message for your failure or not and you’ll see how this message manifests itself.

Whenever this actually fails and okay so, with all of that coded and written what I’ve done here is I actually finished up this test that goes to element rendered after URL. And so what its gonna do is attempt to wait for our element, right which was with the ID finish and that is not present because it’s only rendered after we click a button in the HTML.

So,, of course, this is gonna wait for 5 seconds and then is gonna throw a webdriver timeout exception which I catch and then I proceed with the rest of the actions which is waiting for or trying to click the button with the tag name button and then I wait for that element again for 10 seconds.

This time as you see, I’ve used another customization of the webdriver wait in wait that time out and I reset it instead of the previous 5 seconds that we had set here before I’ve customized it to be 10 seconds because I think that that is a better time to wait for that.

And so let me place a breakpoint here and I’m gonna run this test so you’ll see actly what’s happening and I’ll better explain it as everything’s going on. And of course, let me place the Visual Studio over here on the left on the right.

Actually and then we’ll wait for the browser to come up while the tests are spinning up there go see the play button and started going.

Now we’ve got a browser on the left-hand side and so now it’s waiting for five seconds for that finish elements to be found. However, the finish element is not found and so a webdriver timeout exception is thrown.

And if we look at the immediate window at the oh sorry at the way dot message, this is the message that would be thrown with our webdriver timeout exception, so you can see there’s that message that we wrote.

And so now we can step over this and actually, I’m gonna let it run and always gonna do is click the button you can see that now it’s started loading.

We have no idea how long this might load for but I’ve set a maximum amount of time of 10 seconds and once it finished loading it again checked for the element to be visible which is the element that we were waiting for with the ID success and in this case the test has now finally passed all fixed explicitly and now working because even though their element was rendered after the fact we were now able to wait for it dynamically and find the element in on the page.

Next Tutorial

In the next tutorial, we will learn about the future of ExpectedConditions.

What will you learn in this course?

Are your automated functional tests flaky? Do they randomly fail and you are unsure why? Is your team frustrated with the “instability” of your Selenium Webdriver tests?

If you are interested in fixing all of these problems and making your tests more stable, then you need to understand how to use all of the different types of waits in Selenium Webdriver. Knowing how to handle synchronization issues will make you a top-notch Test Engineer!

“The best tutorials I have ever seen, simply awesome. I take off my hat in front of you, you are a gorgeous professor, I am speechless to express how satisfied I am with these tutorials.” -Lisbey

In this course from Ultimate QA, you will learn:

– Learn the problems that can occur as a result of improper synchronization.

– Learn how to use Selenium Webdriver explicit waits.

– Master Selenium Webdriver implicit waits

– Understand what a DefaultWait is

– Comprehend how to use all of the different types of waits during appropriate situations

Take This Entire Course for Free

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

Pin It on Pinterest

Shares
Share This