Selenium 断言存在和不存在的元素
Selenium Assert with existant and non-existent element
我有登录表单,我需要使用正确和错误的凭据对其进行测试。
输入登录名和密码后点击"Log In"按钮,网站10秒处理。
如果凭据正常 - mainPage.menu 显示 WebElement。
如果凭据错误 - mainPage.menu 不显示 WebElement。
可以刷新登录页面或(不总是)显示错误消息。
如何在测试中检查它?
对于正确的凭据,测试有效:
Assert.assertEquals(true, mainPage.menu.isDisplayed());
对于错误的凭据,测试失败并出现异常,因为 mainPage.menu 无法建立:
Assert.assertEquals(false, mainPage.menu.isDisplayed());
如果我放入 Assert "Log in" 按钮,测试将始终成功,因为在任何情况下(任何凭据)前 10 秒都会显示 "Log in"。
当然,如果我放Thread.sleep,就解决了一个问题。
但是,这不是一个好习惯。
问题在这里
Assert.assertEquals(false, mainPage.menu.isDisplayed());
当凭据错误时,mainPage.menu
将无法正确使用,从而导致异常。所以需要处理this.Usetry/catch
boolean displayed=false;
try {
mainPage.menu.isDisplayed();
displayed=true;
}catch (Exception e) {
//element not displayed
//displayed is false
}
Assert.assertEquals(false, mainPage.menu.isDisplayed());
虽然这个答案会满足您的要求,但理想情况下 有效 和 无效 登录应该在单独的 测试用例中进行验证。此外,避免引用 mainPage.menu 之类的项目,在 False positives.
的情况下 true
理想的验证候选者可以是:
- 有效登录:欢迎消息
- 无效登录:错误消息
根据您的用例,您需要按如下方式引入 try-catch{}
块:
try{
Assert.assertEquals(true, <placeholder_of_welcome_message>.isDisplayed());
}catch (NoSuchElementException e) {
Assert.assertEquals(true, <placeholder_of_error_message>.isDisplayed());
}
此外,您可能需要如下诱导WebDriverWait:
try{
Assert.assertEquals(true, new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.id("welcome_message_element_id"))));
}catch (NoSuchElementException e) {
Assert.assertEquals(true, new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.id("error_message_element_id"))));
}
我有登录表单,我需要使用正确和错误的凭据对其进行测试。
输入登录名和密码后点击"Log In"按钮,网站10秒处理。
如果凭据正常 - mainPage.menu 显示 WebElement。
如果凭据错误 - mainPage.menu 不显示 WebElement。 可以刷新登录页面或(不总是)显示错误消息。
如何在测试中检查它?
对于正确的凭据,测试有效:
Assert.assertEquals(true, mainPage.menu.isDisplayed());
对于错误的凭据,测试失败并出现异常,因为 mainPage.menu 无法建立:
Assert.assertEquals(false, mainPage.menu.isDisplayed());
如果我放入 Assert "Log in" 按钮,测试将始终成功,因为在任何情况下(任何凭据)前 10 秒都会显示 "Log in"。 当然,如果我放Thread.sleep,就解决了一个问题。 但是,这不是一个好习惯。
问题在这里
Assert.assertEquals(false, mainPage.menu.isDisplayed());
当凭据错误时,mainPage.menu
将无法正确使用,从而导致异常。所以需要处理this.Usetry/catch
boolean displayed=false;
try {
mainPage.menu.isDisplayed();
displayed=true;
}catch (Exception e) {
//element not displayed
//displayed is false
}
Assert.assertEquals(false, mainPage.menu.isDisplayed());
虽然这个答案会满足您的要求,但理想情况下 有效 和 无效 登录应该在单独的 测试用例中进行验证。此外,避免引用 mainPage.menu 之类的项目,在 False positives.
的情况下 true理想的验证候选者可以是:
- 有效登录:欢迎消息
- 无效登录:错误消息
根据您的用例,您需要按如下方式引入 try-catch{}
块:
try{
Assert.assertEquals(true, <placeholder_of_welcome_message>.isDisplayed());
}catch (NoSuchElementException e) {
Assert.assertEquals(true, <placeholder_of_error_message>.isDisplayed());
}
此外,您可能需要如下诱导WebDriverWait:
try{
Assert.assertEquals(true, new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.id("welcome_message_element_id"))));
}catch (NoSuchElementException e) {
Assert.assertEquals(true, new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.id("error_message_element_id"))));
}