假设我想测试我的LoginPage功能。因此,如果我输入正确的用户名和密码,那么我将被重定向到welcomepage.jsp。
我使用了容器管理的安全性,因此您将在URL中看到一些j_security_check。
Feature: MyApp Login Feature
Validate MyApp Login Features
Scenario Outline: User logs to MyApp
Given I navigate to MyApp login page
And I enter <username> and <password>
And I click on login button
Then user entered correct username and password then they should be redirected to the proper <url>
Examples:
| username | password | url |
| correctuser | correctpassword | welcome.jsp |
我的问题是如何处理302重定向。 如果我在浏览器中查看我的网络选项卡,我会看到以下序列
GET welcome.jsp
@Then("^Then user entered correct username and password then they should be redirected to the proper ([^\"]*)$")
public void user_should_be_redirected_to_the_proper(String expectedURL) throws Throwable {
System.out.println("expectedURL :: " + expectedURL );
if (driver.getCurrentUrl().contains(expectedURL)) {
System.out.println("MyApp Test Pass");
} else {
System.out.println("MyApp Test Failed");
Assert.fail("MyApp Test failed!");
}
}
我在这里看到一些问题就像这样问 JavaScript Executor in Selenium WebDriver
但我认为这不是一个AJAX调用我们正在等待DOM Ready
答案 0 :(得分:0)
在断言之前尝试使用其中一个ExpectedConditions
,urlMatches,urlContains,urlToBe等待重定向完成。
答案 1 :(得分:0)
@ Grasshopper的答案是正确的,你需要诱导WebDriverWait与ExpectedConditions联系。但正如你要用getCurrentUrl()
而不是 urlMatches , urlContains 或 urlToBe 来声明expectedURL
你需要使用titleContains()
方法传递最终 url 的页面标题的参数。 ExpectedConditions titleContains()
,当标题根据您的应用标题匹配时,它将返回true。此转换将确保最终的 url 已设置。您可以使用以下代码块,我假设最终的 url 以 welcome.jsp结尾将页面标题作为欢迎 - Mark Estrada - 主页:
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.support.ui.ExpectedConditions;
// other lines of code
@Then("^Then user entered correct username and password then they should be redirected to the proper ([^\"]*)$")
public void user_should_be_redirected_to_the_proper(String expectedURL) throws Throwable
{
new WebDriverWait(driver, 20).until(ExpectedConditions.titleContains("Welcome - Mark Estrada - Home Page"));
System.out.println("expectedURL :: " + expectedURL );
if (driver.getCurrentUrl().contains(expectedURL)) {
System.out.println("MyApp Test Pass");
} else {
System.out.println("MyApp Test Failed");
Assert.fail("MyApp Test failed!");
}
}
答案 2 :(得分:0)
您可以等到URL包含或匹配某些字符串:
import org.openqa.selenium.support.ui.ExpectedConditions;
WebDriverWait wait5s = new WebDriverWait(driver,5);
wait5s.until(ExpectedConditions.urlContains("welcome.jsp"));
等待更好地加载DOM以等待元素可点击:
wait5s.until(ExpectedConditions.elementToBeClickable(locator);
或者可选:
wait5s.until(ExpectedConditions.attributeToBe(locator, attribute, value);
在错误的凭据方案中,仅使用if/else
。