Different types of Selenium WebDriver Waits
The web driver user with two geneses of waits to handle the recurring page loads. It loads appearance of windows, pop ups and error messages reflection of web elements on the web page.
- Implicit Wait
- Explicit Wait
Implicit waits use to provide a default waiting time. The consecutive test step/command across the test script. Thus, next test steps the 30 seconds after executing the previous test step/command.
- The implicit wait is a single line of a code and can declare in the setup method of the test script.
- When compared to Explicit wait, the Implicit wait is transparent and uncomplicated. The syntax and approach are simpler than explicit wait.
It gives rise to the test script execution time as each of the commands. It would stop to wait for a stipulated amount of time before resuming the execution. Thus to troubleshoot this issue. WebDriver introduces Explicit waits can apply the executing each of the test steps.
import java.util.concurrent.TimeUnit To access and apply implicit wait in our test scripts. We are bound to import this package into our test script.
drv.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); The line of code into test script after instantiation of WebDriver instance variable. Thus, this needs to set an implicit wait into your test script.
The implicit wait mandates to pass two values as parameters. The first argument indicates the time in the numeric digits system needs to wait. The second argument indicates the time measurement scale. Thus we have mentioned the “30” seconds as default wait time and the time unit has been set to “seconds”.
Explicit waits use to halt the execution till the time a particular condition met. The greatest time has elapsed. Unlike Implicit waits, Explicit waits apply for a particular instance only. WebDriver introduces like WebDriverWait and ExpectedConditions to Explicit waits into the test scripts. In the ambit of this discussion, we will use “gmail.com” as a specimen.
Scenario to be automating
- 1. Launch the web browser and open the “gmail.com
- Enter a valid username
- Enter a valid password
- Click on the sign in button
- Wait for Compose button to be visible after page load
The “wait” reference variable of WebDriverWait class created with Expected Conditions class. An actual condition expected to occur. The program control would move to the next execution step waiting for the entire 30 seconds. We wait for the “compose” button to be present loaded as a part of home page load. then we move forward with calling the click command on the “compose” button.
Types of Expected Conditions
Expected Conditions class provides a great help to deal with scenarios. We have to learn for a condition to before executing the actual test step. Expected Conditions class comes with a wide range of expected conditions. The WebDriverWait method is a reference to the variable and until () method.
Let us discuss a few of them at length:
The expected condition waits for an element to be clickable. It should be present/displayed/visible on the screen as well as enabled. Sample Code wait.until(ExpectedConditions.elementToBeClickable(By.xpath(“//div[contains(text(),’COMPOSE’)]”)));
The expected condition waits for an element having a certain string pattern. Sample Code wait.until(ExpectedConditions.textToBePresentInElement(By.xpath(“//div[@id= ‘forgotPass'”), “text to be found”));
The expected condition waits for an alert box to appear. Sample Code wait.until(ExpectedConditions.alertIsPresent()) !=null);
The expected condition waits for a page with a specific title. Sample Code wait.until(ExpectedConditions.titleIs(“gmail”));
The expected condition waits for a frame to be available. The control switches to it automatic.
There is a very common user action clicks on the back and forward buttons of the web browser. The n forth to navigate to the different web pages visited in the current session on the browser’s history. So many actions performed by the users of WebDriver introduces Navigate commands.
Let us examine these commands in detail
This command lets the user navigate to the previous web page.
driver.navigate().back(); The command requires no parameters. It takes back the user to the previous web page in the web browser’s history.
This command lets the user navigate to the next web page with reference to the browser’s history.
driver.navigate().forward(); The command requires no parameters. It takes forward the user to the next web page in the web browser’s history.
The user to fresh the current web page thereby reloading all the web elements.
driver.navigate().refresh(); The above command requires no parameters and reloads the web page.
This command lets the user launch a new web browser window and navigate to the specified URL.
driver.navigate().to(“http://google.com”); The command requires a web URL as a parameter. Then it opens the specified URL in a launched web browser.
Best Online Selenium Training Hyderabad
Best Online Selenium Training Hyderabad: We Provides Best Online Selenium Training Hyderabad by real time experts. We offer online & classroom Training.