【问题标题】:Why are my ExpectedConditions commands being ignored? Selenium WebDriver in Java为什么我的 ExpectedConditions 命令被忽略? Java 中的 Selenium WebDriver
【发布时间】:2017-11-10 11:22:43
【问题描述】:

我正在使用 Cucumber 和 Selenium WebDriver 来测试应用程序,并且我刚刚注意到我的测试正在通过特定区域,无论我在“ExpectedConditions.textToBe”方法的参数中添加什么。

这部分测试简单地检查在测试添加用户后正确的文本是否出现在用户角色表中:

    public void admin_can_see_the_new_role_in_the_list() throws Throwable {
    // Write code here that turns the phrase above into concrete actions
    //throw new PendingException();
    Thread.sleep(3000);
    ExpectedConditions.textToBe(By.xpath("//*[@id=\"role-nbbbamze\"]"), "account manasdfsdfger");
    ExpectedConditions.textToBe(By.xpath("//*[@id=\"app\"]/dizz/dib[2]/divz/div[2]zzz/table/tbody/tr[11]/td[2]"), "Accmasfsdnager");
    ExpectedConditions.textToBe(By.xpath("//*[@id=\"app\"]/dbiv/div[2]/div/dziv[2]/tzable/tzzzbody/tr[11]/td[3]"), "Can acvfcess the normal dashboard");
}

如您所见,我在参数中添加了随机字符,测试运行并仍然通过。它应该失败,因为我定义的 xpath 不存在 - 或者我断言的文本与该 xpath 中的任何内容都不匹配。

我显然用错了 ExpectedConditions,但我不知道在哪里或如何。

提前谢谢大家!

【问题讨论】:

    标签: java selenium selenium-webdriver cucumber


    【解决方案1】:

    您必须将它与等待对象结合使用,例如:

    WebDriverWait wait = new WebDriverWait(driver, 60); 
    wait.until(ExpectedConditions.textToBe(By.xpath("//*[@id=\"app\"]/dbiv/div[2]/div/dziv[2]/tzable/tzzzbody/tr[11]/td[3]"), "Can acvfcess the normal dashboard"));
    

    【讨论】:

    • 魔术!非常感谢,这完美地回答了我的问题。
    【解决方案2】:

    这里你需要考虑几件事:

    • 看来你是在尝试诱导WebDriverWait,所以你可以去掉Thread.sleep(3000);
    • ExpectedConditions 必须与 WebDriverWait 的实例绑定,例如wait 连同 until 子句如下:

      WebDriverWait wait = new WebDriverWait(driver, 10); 
      wait.until(ExpectedConditions.textToBe(By.xpath("//*[@id=\"app\"]/dbiv/div[2]/div/dziv[2]/tzable/tzzzbody/tr[11]/td[3]"), "Can acvfcess the normal dashboard"));
      
    • 现在,最重要的一点是,textToBe 子句返回 boolean。因此,我们检查了返回的 Boolean Status 以及以下内容:

      WebDriverWait wait = new WebDriverWait(driver, 10); 
      Boolean bool = wait.until(ExpectedConditions.textToBe(By.xpath("//*[@id=\"app\"]/dbiv/div[2]/div/dziv[2]/tzable/tzzzbody/tr[11]/td[3]"), "Can acvfcess the normal dashboard"));
      

    【讨论】:

    • 关于布尔的好点,我什至没有想到这一点。非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-09-30
    • 2016-06-20
    • 1970-01-01
    • 2020-11-07
    • 2013-04-24
    • 2017-01-15
    • 2013-03-09
    相关资源
    最近更新 更多