【问题标题】:Implement a global listener for Selenium tests为 Selenium 测试实现一个全局监听器
【发布时间】:2021-08-26 20:05:20
【问题描述】:

我使用此代码单击一系列具有唯一按钮 ID 的对话框窗口。到目前为止,它工作正常,因为订单很严格:

clickConfirmWindow(driver, SURVEY_EXIT_BUTTON_ID_LOCATOR, "Survey exit window");

... and many mode windows with unique ID with random order

protected void clickConfirmWindow(WebDriver driver, String elementId, String name) {
        // Check if warning window is displayed using button ID
        System.out.println("Searching " + name + " using " + elementId);
        if (isClickable(driver, elementId, 1)) {
            System.out.println("Found " + name + " using " + elementId);
            driver.findElement(By.id(elementId)).click();
        }
    }

protected void clickConfirmWindow(WebDriver driver, String elementId, String name) {
        // Check if warning window is displayed using button ID
        System.out.println("Searching " + name + " using " + elementId);
        if (isClickable(driver, elementId, 1)) {
            System.out.println("Found " + name + " using " + elementId);
            driver.findElement(By.id(elementId)).click();
        }
    }

private Boolean isClickable(WebDriver driver, String elementId, int timeOut) {
        try {
            new WebDriverWait(driver, timeOut).until(ExpectedConditions.visibilityOfElementLocated(By.id(elementId)));
            return true;
        } catch (TimeoutException e) {
            e.printStackTrace();
            return false;
        }
    }

但是现在窗口的顺序是随机的。是否可以实现某种全局侦听器,通过整个应用程序侦听 Web 元素 ID 并在存在时单击它?

【问题讨论】:

  • 有一个 webdriver 事件监听器......(显然是它的新版本):selenium.dev/selenium/docs/api/java/org/openqa/selenium/support/…(不确定它的支持程度......)您也可以创建一个超类(抽象类)用于您的包装器并让您的包装器扩展它。
  • 能否分享一下确认窗口的HTML代码?如果您可以仅使用一种方法处理所有的确认框,该怎么办。我正在考虑使按钮定位器独立于 id [所有确认窗口通用]。

标签: java selenium selenium-webdriver selenium-chromedriver junit5


【解决方案1】:

不确定你的具体情况,但在 vba 中我会写一个 if 语句来看看它是否属实,

如果 driver.findelementsbyid("your Id").count > 0 那么 driver.findelementbyid("你的 ID").click 别的 结束如果

【讨论】:

    【解决方案2】:

    搜索元素是否存在应该会对您有所帮助

    WebElement elementName = driver.findElement(By.id("idElement")); 
    
    if(elementName != null){
       //Loaded Element 
    }
    

    【讨论】:

      猜你喜欢
      • 2021-04-29
      • 2011-10-25
      • 1970-01-01
      • 1970-01-01
      • 2013-11-15
      • 2012-02-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多