Search code examples
javaseleniumselenium-webdriverbrowserstack

How to use explicit wait condition with selenium 4.0


error

using Java 17 and slenium 4.0

wait.until(ExpectedConditions.titleContains("BrowserStack"));

here is full code :

public class mainTestClass {
public static final String USERNAME = "myuser *****";
public static final String AUTOMATE_KEY = "my ke *****";
public static final String URL = "https://" + USERNAME + ":" + AUTOMATE_KEY + "@hub-cloud.browserstack.com/wd/hub";
public static void main(String[] args) throws Exception {
    Thread object1 = new Thread(new TestClass1());
    object1.start();
    Thread object2 = new Thread(new TestClass2());
    object2.start();
    Thread object3 = new Thread(new TestClass3());
    object3.start();
}
public void executeTest(Hashtable<String, String> capsHashtable) {
    String key;
    DesiredCapabilities caps = new DesiredCapabilities();
    // Iterate over the hashtable and set the capabilities
    Set<String> keys = capsHashtable.keySet();
    Iterator<String> itr = keys.iterator();
    while (itr.hasNext()) {
        key = itr.next();
        caps.setCapability(key, capsHashtable.get(key));
    }
    WebDriver driver;
    try {
        driver = new RemoteWebDriver(new URL(URL), caps);
        JavascriptExecutor jse = (JavascriptExecutor)driver;
        // Searching for 'BrowserStack' on google.com
        driver.get("https://www.google.com");
        WebElement element = driver.findElement(By.name("q"));
        element.sendKeys("BrowserStack");
        element.submit();
        // Setting the status of test as 'passed' or 'failed' based on the condition; if title of the web page contains 'BrowserStack'
        WebDriverWait wait = new WebDriverWait(driver, 5);
        try {
          //  wait.until(ExpectedConditions.titleContains("BrowserStack"));
            jse.executeScript("browserstack_executor: {\"action\": \"setSessionStatus\", \"arguments\": {\"status\": \"passed\", \"reason\": \"Title matched!\"}}");
        }
        catch(Exception e) {
            jse.executeScript("browserstack_executor: {\"action\": \"setSessionStatus\", \"arguments\": {\"status\":\"failed\", \"reason\": \"Title not matched\"}}");
        }
        System.out.println(driver.getTitle());
        driver.quit();
    } catch (MalformedURLException e) {
        e.printStackTrace();
    }
}

}


Solution

  • This is cause in Selenium 4.0.0 (Stable)

    WebDriverWait which takes driver and timeoutInSeconds long, have been Deprecated

      @Deprecated
      public WebDriverWait(WebDriver driver, long timeoutInSeconds) {
        this(driver, Duration.ofSeconds(timeoutInSeconds));
      }
    

    Fix:

    Selenium devs have given this method instead

      public WebDriverWait(WebDriver driver, Duration timeout) {
        this(
            driver,
            timeout,
            Duration.ofMillis(DEFAULT_SLEEP_TIMEOUT),
            Clock.systemDefaultZone(),
            Sleeper.SYSTEM_SLEEPER);
      }
    

    so your effective code will be :

     WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
        try {
            wait.until(ExpectedConditions.titleContains("BrowserStack"));
            JavascriptExecutor jse = (JavascriptExecutor)driver;
        }
        catch(Exception e) {
            e.printStackTrace();
        }