1

I am new to Java, I am using selenium with Junit to automate web application. Our website takes 5-8 seconds to load as its pre production environment, Hence i have used Thread.sleep in my methods. which i feel is not a good option, hence need a code which will slow down the automation with controlled flow of execution in steps. Also i have no time to install testng to use implicit and explicit wait. A variable to slow the methods will do, but how?

Aniket
  • 29
  • 3

1 Answers1

0

the explicit and implicit wait is not TestNG feature its selenium , testng is only used as test framwork for controlling test flow and assertions.

Imports:

import java.time.Duration;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

Code:

WebDriver driver = new ChromeDriver();

driver.get("https://google.com/ncr");

driver.findElement(By.name("q")).sendKeys("cheese" + Keys.ENTER);

   // Initialize and wait till element(link) became clickable - timeout in 10 seconds

WebElement firstResult = new WebDriverWait(driver, Duration.ofSeconds(10))
           .until(ExpectedConditions.elementToBeClickable(By.xpath("//a/h3")));

    // Print the first result
System.out.println(firstResult.getText());
PDHide
  • 18,113
  • 2
  • 31
  • 46