Speaker A
Right, so in the previous class, we have seen get methods, and also we have seen how to use conditional methods and then browser methods like close and quit. So today, we'll discuss about wait commands, or we can say wait methods, which are provided in Selenium. So first of all, why do we need to use the wait command? So before that, we need to understand what is synchronization problem. So almost in every automation tool, we face this problem, synchronize or synchronization problem. So what is synchronization? Sometimes what happens is when you run your automation test script, and parallel your application also should interact or should invoke the application, and it shows all the elements of the web page, right? So when you run your automation test scripts, at the same time the web page and elements should be available, and then only your automation test script will be able to perform the actions on the web elements. But sometimes what happens is maybe some application is not available by the time script is executing, and suppose some element is not available, then you will get some kind of exception like no such element exception, element not found exception. So such type of exceptions you will get. So that is called synchronization problem. So what is the main root cause for this? It is speed of your application, like execution speed is faster or the speed of your automation script is faster than your application response. So that will cause the synchronization problem. Almost in every automation tool, we can see this kind of problem. Okay, so let me give you some example. Let's say I have few lines of code I have written like this. Okay, and let us say this will launch our application. This is our application under test. So let us say I want to execute this particular step. Now on this particular element, to execute this particular step, I need some element on this page. Okay, or suppose sometimes what happens is I want to execute this step, but for this particular step, I need another web page. I need to navigate to another web page. So what will happen is by the time the statement is got executed, maybe the element is not available or the web page is not fully loaded. In those cases, what happens? This statement will throw exception, and because of this, we got synchronization problem. So this is called synchronization problem. So speed of your application is very slow, and your execution is faster than your speed of your application or the response of your application. That is called synchronization problem. So in that particular situation, we will get synchronization problem. Then how to solve this problem? So to solve the problem, what we can do is the application response is not in our hand because we cannot control the speed of your application because it depends on many factors. Like suppose sometimes your network speed is very slow, then your application will respond very slowly. And sometimes many people are accessing the same application, same page at the same time. In that case also, your application response will be slow. So there are multiple factors involved. So we cannot control the speed of your application. The only thing is we can control the execution speed of your automation test script. So this is in your hand. You can control this, but you cannot control your application. So what we can do is suppose there is a statement here, and to execute the statement, we need some element. So what we can do is while executing the statement, we can wait for this element, or we can pause your script for some time here, and once the element is available, then we can resume the execution, right? So this way we can handle this. And by default, we have one Sleep method in Java, thread.sleep. So wherever you can see this kind of a problem, you can put thread.sleep. So this is a normal method. We have used earlier also wherever the sync problem occurs, and wherever the page is taking more time to load or more time to display the element, we put this thread.sleep, and we put some time in the milliseconds, and then our script is paused for some time, and meanwhile the element is available, then it will go and execute the rest of the statements. But other than this, WebDriver itself is provided some of the wait commands. By using those wait commands, we can solve synchronization problem, and most of the times we will prefer to use wait commands instead of thread.sleep command. And thread.sleep is having some disadvantages. We will see what are the disadvantages, and then we will discuss other commands provided by Selenium WebDriver. And every wait statement has its own advantages and disadvantages, but WebDriver wait commands, like whatever waits are provided by WebDriver, they will work more efficiently than thread.sleep. Okay, so how many ways we can handle synchronization problem? So thread.sleep is actually not coming from WebDriver, which is Java method itself. It is coming from Java itself. So this is not a WebDriver command, remember this. Okay, if anybody asks you what are the wait methods provided in Selenium, there are only two kinds of waits provided. One is called implicit wait, second is explicit wait. Okay, implicit wait and explicit wait. So these are the only two wait statements provided in Selenium. And in explicit wait, again we have another flavor of wait which is called fluent wait. This is also explicit wait, but it will provide some more options than explicit wait. These are the only two wait commands provided by Selenium WebDriver. Thread.sleep also we can use, but this is not related to Selenium WebDriver. So Java itself is provided thread.sleep command. Okay, and implicit wait and explicit waits are provided by Selenium WebDriver. So these are the only two types of wait provided by Selenium WebDriver. In explicit wait, we have another flavor called fluent wait. Okay, now we'll discuss one by one. First, we'll start with the Sleep command. So what is the advantage and what is the disadvantage of sleep command? So before that, let me show you an example where we will try to use sleep command. Okay, go to Eclipse and create new package day 27. First thing, sleep command. Okay, so now let me write a small piece of code. Okay, now I have written this piece of code, and I'll import all the classes and everything, and this is thread.sleep. So if I don't put this here, what will happen is it will launch your application, and after that, I'm interacting with some element here. So if I open this particular web page, you can see login screen. So this is your login screen, and here there are some elements. So here I have, I'm interacting with this username input box, and I'm passing some value here through XPath. I have interacted with that element, and then I have passed some value. So I have not used sleep command for now. So we'll see what is a problem with this when I run as a Java application. So synchronization problem you may not get every time. Okay, so randomly depends upon the scenario, you will get it. But whenever you get a synchronization problem, then you can handle it. Even if you're having suppose doubt about synchronization, suppose you are guessing it sometimes. So in that case also, you can keep the synchronization part. Now you can see here application is launched, and it is waiting cursor here, but still the value is not entered in this box. And if you go back and see, you'll get this exception no such element exception. Okay, so there are two kinds of exceptions you have to notice here, most important. Sometimes in interview also will ask no such element exception. That is one type of exception, element not found exception. These are the two types of exceptions most of the time you can see. So when will you get no such element exception? When will you get element not found exception? Can anyone guess? No such element exception and element not found except...