[英]Why are my ExpectedConditions commands being ignored? Selenium WebDriver in Java
I am using Cucumber with Selenium WebDriver to test an application, and I have just noticed that my tests are passing on a particular area, no matter what I put into the parameters of the "ExpectedConditions.textToBe" method. 我正在将Cucumber与Selenium WebDriver一起用于测试应用程序,并且我刚刚注意到,无论我将“ ExpectedConditions.textToBe”方法的参数设置为什么,我的测试都通过了特定区域。
This part of the tests simple checks that the correct text appears in a table of user roles after the test has added a user: 测试的这一部分简单地检查在添加用户后,正确的文本是否出现在用户角色表中:
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");
}
As you can see, I have added random characters to the parameters and the test runs and still passes. 如您所见,我在参数中添加了随机字符,并且测试运行并仍然通过。 It should fail, either because the xpath I have defined doesn't exist - or the text I am asserting does not match anything within that xpath.
它应该失败,因为我定义的xpath不存在-或我声明的文本与该xpath中的任何内容都不匹配。
I am obviously using the ExpectedConditions wrong, but I can't figure out where or how. 我显然在使用ExpectedConditions错误,但我不知道在哪里或如何。
Thanks in advance guys! 在此先感谢大家!
You have to use it in combination with a wait object like: 您必须将其与以下等待对象结合使用:
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"));
Here you need to consider a couple of things : 在这里,您需要考虑以下几点:
WebDriverWait
, so you can remove the Thread.sleep(3000);
WebDriverWait
,因此可以删除Thread.sleep(3000);
ExpectedConditions
must be bonded with an instance of WebDriverWait
eg wait
along with until
clause as follows: 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"));
Now, the most important point is, textToBe
clause returns boolean
. 现在,最重要的一点是,
textToBe
子句返回boolean
。 So we have check for the returned Boolean Status
as well as follows: 因此,我们检查了返回的
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"));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.