【问题标题】:How to use explicit wait condition with selenium 4.0如何在 selenium 4.0 中使用显式等待条件
【发布时间】:2021-12-31 08:06:42
【问题描述】:

使用 Java 17 和 slenium 4.0

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

这里是完整的代码:

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

}

【问题讨论】:

  • 我们可以更新一下这张票吗?

标签: java selenium selenium-webdriver browserstack


【解决方案1】:

这是Selenium 4.0.0 (Stable)中的原因

WebDriverWaitdrivertimeoutInSeconds long,一直是Deprecated

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

修复:

Selenium 开发者已经给出了这种方法

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

所以你的有效代码将是:

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

【讨论】:

    猜你喜欢
    • 2021-12-18
    • 2012-05-11
    • 1970-01-01
    • 2012-05-11
    • 2021-01-19
    • 2023-03-11
    • 2018-01-04
    • 2016-01-02
    相关资源
    最近更新 更多