在尝试断言显示错误消息时,我遇到一些奇怪的不一致行为。
我有2个测试,除了将不同的值发送到表单中并检查不同的错误消息外,它们都在做相同的事情。
System.setProperty("webdriver.chrome.driver", "C:\\Users\\dan.febry\\Desktop\\chromedriver_win32\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://mywebsite.com");
driver.findElement(By.id("Identifier")).sendKeys("username");
driver.findElement(By.id("Password")).sendKeys("password");
driver.findElement(By.xpath("/html/body/div/div[1]/div[1]/div/div[2]/div[1]/div/form/button")).click();
driver.findElement(By.xpath("/html/body/div/div[1]/div[1]/div/div[4]/div/button")).click();
driver.findElement(By.xpath("/html/body/div/div[1]/div[1]/div/div[4]/div/div/form[1]/button")).click();
driver.findElement(By.id("OldPassword")).sendKeys("Password!23");
driver.findElement(By.id("NewPassword")).sendKeys("Password123!");
driver.findElement(By.id("ConfirmPassword")).sendKeys("");
driver.findElement(By.xpath("//*[@id=\"form0\"]/div[5]/div/button")).click();
List<WebElement> allElements = driver.findElements(By.xpath("//*[@id=\"form0\"]/div[4]/ul/li"));
Assert.assertEquals(allElements.size(), 1);
Assert.assertEquals(allElements.get(0).getText(), "The new password and confirmation password do not match.");
// TODO Verify failed counter has not gone up.
driver.close();
}
@Test
public void ChangePasswordCurrentPasswordIncorrect() {
System.setProperty("webdriver.chrome.driver", "C:\\Users\\dan.febry\\Desktop\\chromedriver_win32\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://mywebsite.com");
driver.findElement(By.id("Identifier")).sendKeys("username");
driver.findElement(By.id("Password")).sendKeys("password");
driver.findElement(By.xpath("/html/body/div/div[1]/div[1]/div/div[2]/div[1]/div/form/button")).click();
driver.findElement(By.xpath("/html/body/div/div[1]/div[1]/div/div[4]/div/button")).click();
driver.findElement(By.xpath("/html/body/div/div[1]/div[1]/div/div[4]/div/div/form[1]/button")).click();
driver.findElement(By.id("OldPassword")).sendKeys("Password@123");
driver.findElement(By.id("NewPassword")).sendKeys("Password123!");
driver.findElement(By.id("ConfirmPassword")).sendKeys("Password123!");
driver.findElement(By.xpath("//*[@id=\"form0\"]/div[5]/div/button")).click();
List<WebElement> allElements = driver.findElements(By.xpath("//*[@id=\"form0\"]/div[4]/ul/li"));
Assert.assertEquals(allElements.size(), 1);
Assert.assertEquals(allElements.get(0).getText(), "Incorrect password.");
// TODO Verify failed counter has gone up.
// TODO Reset failed counter value
driver.close();
}`
第一个测试正确运行,通过并声明了文本。但是,第二项测试失败并显示以下错误:
java.lang.AssertionError:预期为[错误密码。]但是找到了[]
HTML是:
<div class="validation-summary-errors text-danger" data-valmsg-summary="true">
<ul>
<li>Incorrect password.</li>
</ul>
</div>
<div class="text-danger validation-summary-errors" data-valmsg-summary="true">
<ul>
<li>The new password and confirmation password do not match.</li>
</ul>
</div>
PS。不知道我是否错过了这里的一些重要信息,所以让我知道是否应该提供有关此问题的更多信息。
答案 0 :(得分:0)
可能无法获取正确的 Day X Y Z
product1 1 10 12 5
product1 2 18 22 6
product2 1 9 100 88
product2 2 123 4 56
,因此它返回零列表。
代替这个xpath
Xpath
尝试下面的driver.findElements(By.xpath("//*[@id=\"form0\"]/div[4]/ul/li"))
并花一些时间Xpath
来查找元素
WebDriverWait