如何使用断言 selenium/TestNG/Java 提交表单后检查我是否在正确的页面中
How to check wether am in correct page after submitting the form using assertion selenium/TestNG/Java
这是我的场景“我正在提交表单,提交后导航到页面调用 WebTable.I 现在想确认提交表单后页面不正确”
如何使用断言来检查这个?请帮助 selenium /Java /TestNG
根据我的经验,您基本上有两种选择。
- 等待 URL 使用
driver.getCurrentUrl()
包含您期望的内容
- 等待只会出现在下一页的特定元素
如果您还没有为执行重试构建 class,您可以使用 FluentWait。
Selenium 为这种类型的验证提供了一个很好的实践,FluentWait 来了:
FluentWait fluentWait = new FluentWait(webDriverBPO).withTimeout(Duration.ofSeconds(20)).pollingEvery(Duration.ofSeconds(1)).ignoring(NullPointerException.class);
完成最后一个操作后,您只需等待 URL 包含您的预期值:
fluentWait.until(ExpectedConditions.urlContains("your URL or keyword from URL"));
您也可以尝试等待新页面中的任何元素,但 URL 加载速度比内容快,因此您将节省一些时间(因为您不需要执行任何其他验证).
单击表单后,您将使用 Webdriverwait 等待元素显示。
您拉出当前 URL 并检查值并断言它。
submitBtn.click();
WebDriverWait(driver,10).until(ExpectedConditions.visibilityOfElementLocated(By.id("etc")));
expectedUrl = driver.getCurrentUrl();
Assert.assertTrue(expectedUrl.equals("something"));
这是我的场景“我正在提交表单,提交后导航到页面调用 WebTable.I 现在想确认提交表单后页面不正确”
如何使用断言来检查这个?请帮助 selenium /Java /TestNG
根据我的经验,您基本上有两种选择。
- 等待 URL 使用
driver.getCurrentUrl()
包含您期望的内容
- 等待只会出现在下一页的特定元素
如果您还没有为执行重试构建 class,您可以使用 FluentWait。
Selenium 为这种类型的验证提供了一个很好的实践,FluentWait 来了:
FluentWait fluentWait = new FluentWait(webDriverBPO).withTimeout(Duration.ofSeconds(20)).pollingEvery(Duration.ofSeconds(1)).ignoring(NullPointerException.class);
完成最后一个操作后,您只需等待 URL 包含您的预期值:
fluentWait.until(ExpectedConditions.urlContains("your URL or keyword from URL"));
您也可以尝试等待新页面中的任何元素,但 URL 加载速度比内容快,因此您将节省一些时间(因为您不需要执行任何其他验证).
单击表单后,您将使用 Webdriverwait 等待元素显示。 您拉出当前 URL 并检查值并断言它。
submitBtn.click();
WebDriverWait(driver,10).until(ExpectedConditions.visibilityOfElementLocated(By.id("etc")));
expectedUrl = driver.getCurrentUrl();
Assert.assertTrue(expectedUrl.equals("something"));