Creating an Implicit Wait

The aim here is to implement an implicit for an automation script. To complete the automation process, we need to simulate the click of this button and then make our automation script wait before trying to find one of the elements whose attribute values has changed. The steps for completion are as follows:

  1. Open https://trainingbypackt.github.io/Beginning-Selenium/lesson_5/activity_5_A-1.html on Chrome and open the Dev Tools console.
  2. Review and analyze the DOM of the https://trainingbypackt.github.io/Beginning-Selenium/lesson_5/activity_5_A-1.html file, and see how elements are affected by the functionality of this file.
  3. Using IntelliJ IDEA, create a Selenium script that clicks on the available button by using the findElement method. We'll begin with importing the relevant packages and creating the implicitWaitExample() method, as follows:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

import org.openqa.selenium.chrome.ChromeDriver;

import java.util.concurrent.TimeUnit;

public class Activity5A {
public static void main(String[] args) {
implicitWaitExample();
}
  1. Include an implicit wait for any given time. Explore the different options provided by the TimeUnit enumeration, as follows:
private static void implicitWaitExample() {
WebDriver driver = new ChromeDriver();
driver.get("https://trainingbypackt.github.io/
Beginning-Selenium/lesson_5/activity_5_A-1.html");

// Set an implicit wait for 5 seconds
driver.manage().timeouts().implicitlyWait(5,
TimeUnit.SECONDS);

  1. Write a findElement command for those elements whose attribute values change once the button is clicked:
try {
/* Search for a button named runTestButton and
click on it to start the test*/
driver.findElement(By.id("runTestButton")).
click();
  1. Now, check the contents of the element using if … else statements:
if (info.getText().contains("run")) {
System.out.println("ImplicitWait worked,
the element contains 'run'");
} else {
System.out.println("Something went wrong
with ImplicitWait, 'run' was not found");
}
} finally {
driver.quit();
}
}
}
  1. Compile and run the script.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.137.64