简体   繁体   English

如何在 selenium 4.0 中使用显式等待条件

[英]How to use explicit wait condition with selenium 4.0

错误

using Java 17 and slenium 4.0使用 Java 17 和 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();
    }
}

} }

This is cause in Selenium 4.0.0 (Stable)这是Selenium 4.0.0 (Stable)中的原因

WebDriverWait which takes driver and timeoutInSeconds long , have been Deprecated WebDriverWait需要drivertimeoutInSeconds long ,已被Deprecated

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

Fix:使固定:

Selenium devs have given this method instead Selenium 开发人员已经给出了这种方法

  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();
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM