November 20

0  comments

1. Implicit wait usage in Selenium

2. Custom implict wait

3. Source code

Implicit wait usage in Selenium

The below official page describes the various Timeouts in Selenium https://www.selenium.dev/selenium/docs/api/java/org/openqa/selenium/WebDriver.Timeouts.html 

As seen in below description, no limit is set for default timeout in Selenium

Let us go to http://uitestingplayground.com/ajax to understand the usage of timeouts in selenium

The highlighted descrition (see above) says that when we press the blue colored button, the ajax text would appear after 15 secs.

Let us manually click the button, notice that the ajax data loads after 15 sec 

Inspect the blue colored button

We can make use of xpath to identify the button

Let us now inspect the AJAX data 

We can make use of xpath to identify the ajax output. 

Let us catch the text of ajax output in the string variable and try to print it

The code so far:

Execute test 

The test fails and ‘NoSuchElementException’ is thrown (see above). 

This means that the default timeout is less than 15 sec because selenium did not wait automatically for 15 sec for the ajax data to display.

Custom implict wait 

Introduce an implicit wait time of 15 seonds (see line#16 below)

Execute test. 

The test passes this time and ajax message gets printed in console

This is the magic of implicit wait. It waits for each webelement on the page upto the duration of implicit wait time we have mentioned in the script 

Let us comment line#16. 

Add line#17 to include 14 ssec implicit wait time. 

Let us execute. 

Notice below that we get same exception. This was expected. The reason being, we had asked selenium to implicity wait only for 14 sec but the ajax text appears after 15 sec  

Comment line#17

Add line#18 (implicit wait of 16 sec)

Execute. 

This will surely pass since the implicit wait is greater than 15 secs.

We can use implicit wait times in our script in a similar fashion.

Source code

package Intro;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.firefox.FirefoxDriver;

import io.github.bonigarcia.wdm.WebDriverManager;

public class Blog8_ImplicitWait {

@SuppressWarnings("deprecation")

public static void main(String[] args) {

WebDriverManager.firefoxdriver().setup();

WebDriver driver = new FirefoxDriver();

//driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS); //success

//driver.manage().timeouts().implicitlyWait(14, TimeUnit.SECONDS); //fails

driver.manage().timeouts().implicitlyWait(16, TimeUnit.SECONDS); //success

driver.get("http://uitestingplayground.com/ajax");

driver.findElement(By.xpath("//button[@id='ajaxButton']")).click();

String ajax_message =  driver.findElement(By.xpath("//p[@class='bg-success']")).getText();

System.out.println(ajax_message);

}

}

Tags


You may also like

Setup Playwright-Java in Eclipse

Setup Playwright-Java in Eclipse
Leave a Reply

Your email address will not be published. Required fields are marked

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}