第一次海报..
我刚开始使用Selenium的LoadableComponent模型,这听起来是个好主意。但是,我不太明白如何调用isLoaded方法。让我举个例子:
我有两个模型类MainPage和SignIn,当点击MainPage中的“登录”链接时,正在加载SignIn页面。这是MainPage的代码:
public class MainPage extends LoadableComponent<MainPage>
{
private final WebDriver driver;
@FindBy(linkText = "Sign In")
WebElement signIn;
public MainPage(WebDriver driver)
{
this.driver = driver;
PageFactory.initElements(driver, this);
}
@Override
public void isLoaded() throws Error
{
System.out.println("isLoaded main page being called");
assertEquals(
driver.getTitle(),
"Main page title",
"Invalid title for Home page");
}
@Override
public void load()
{
driver.get(Constants.URL_HOME);
}
public SignInPage signIn()
{
signIn.click();
return new SignInPage(driver);
}
以下是SignInPage的代码:
public class SignInPage extends LoadableComponent<SignInPage>
{
private final WebDriver driver;
@FindBy(xpath = "//a[@class='action-link registration']")
WebElement register;
public SignInPage(WebDriver driver)
{
this.driver = driver;
PageFactory.initElements(driver, this);
}
@Override
public void isLoaded() throws Error
{
System.out.println("Calling isLoaded()...");
String url = driver.getCurrentUrl();
assertTrue(url.endsWith("account/signIn"));
WebElement h1 = driver.findElement(By.xpath("//span[@data-label='SIGN_IN']"));
assertTrue(h1.getText().equals("THIS SHOULD REALLY FAIL"));
}
@Override
public void load()
{
driver.get(Constants.URL_SIGNIN);
}
public UserRegistrationPage register()
{
register.click();
return new UserRegistrationPage(driver);
}
public String getTitle()
{
return driver.getTitle();
}
}
现在测试非常基础,我正在加载MainPage并单击SignIn链接。我希望看到isLoaded()方法为MainPage调用一次,对SignIn页面调用一次。但是,isLoaded()正在为MainPage调用两次,而从不为SignIn页面调用。我不明白为什么...... 这是测试:
@Test
public void newRegistration()
{
MainPage mainpage = new MainPage(driver).get();
SignInPage signInPage = mainpage.signIn();
UserRegistrationPage userRegPage = signInPage.register();
}
答案 0 :(得分:1)
我明白了。测试中的代码应该是:
UserRegistrationPage userRegPage = signInPage.register();
userRegPage.get();
对我来说有点奇怪,但是......