简体   繁体   中英

LoadableComponent in Selenium: isLoaded method is not used when class object comes from another class

First time poster..

I just started using LoadableComponent model from Selenium and it sounds like a great idea. However, I don't quite understand how isLoaded method is being called. Let me give you my example:

I have two model classes MainPage and SignIn, SignIn page is being loaded when clicking "Sign In" link in MainPage. Here is the code for 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);
}

Here's the code for 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();
}
}

Now the test is quite basic, I am loading the MainPage and clicking on SignIn link. I would expect to see that isLoaded() method is called once for MainPage and once for SignIn page. However, isLoaded() is being called twice for MainPage and never for SignIn page. I fail to understand why... Here's the test:

@Test
public void newRegistration()
{
    MainPage mainpage = new MainPage(driver).get();
    SignInPage signInPage = mainpage.signIn();
    UserRegistrationPage userRegPage = signInPage.register();

}

I got it. The code in test should be:

 UserRegistrationPage userRegPage = signInPage.register();
 userRegPage.get();

A bit odd to me, but...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM