質問

I am using LoadableComponent<T> in a Selenium testing project. I have an application with a main page (modeled as MainPage.java) from which I go to login page (LoginPage). I enter some credentials and the login form is redirecting me (if credentials are valid) to the MainPage again, enabling some extra functionalities. Because I use MainPage as starting point for many tests, I had to implement load() method for MainPage with a valid URL. So if I am doing something like:

MainPage mainpage=new MainPage();
mainpage.get();
LoginPage loginPage=mainpage.clickLogin();
loginPage.setUser("whatever").setPassword("totally_secret");
mainpage = loginPage.clickJoin();
mainpage.get();

I cannot be sure if the action which directs me to the mainpage was succesful or not, because anyway, the last call will load the page. On the other hand if I call clickJoin() method without assigning the returned object to a variable, I cannot validate if the correct page is loaded and I cannot get a reference to its methods in order to continue the test. I could also make clickJoin() method return void, but then how do I get the reference to the new page in the test? How should I deal with it?

役に立ちましたか?

解決

Why even call mainpage.get() after loginPage.clickJoin()? Wouldn't the interaction with elements on the page take you to the next/previous one?

In general, you should be able to interact with the mainPage object returned from clickJoin() right away. You should not have to call get() again. If you're getting errors without it, it's probably because of the code executing too fast, without actually waiting for the page to load properly.

To avoid this, you can have the loginPage.clickJoin() method make sure that the main page has loaded again.

public MainPage clickJoin() {

    joinLink.click(); //clicking some link/button or whatever should take you to the page

    new WebDriverWait(driver, timeToWait).until(
       //here you can use the ExpectedConditions class to wait for the title 
       //to change to a specified value or something similar. There's a wide
       //variety of methods provided. Alternatively, you can implement the
       // ExpectedCondition interface yourself
    );
    return new MainPage(driver);
}

If the page fails to load, you'll get an exception and the test will fail.

You don't have to do it the exact same way as in the pseudocode above but a WebDriverWait seems like a way to make sure the right pages load when navigating between them.

他のヒント

Not sure I'm 100% clear on exactly what you're asking, but here's what I think I would do. Your public methods on page objects should be more representative of user operations (logging in successfully), rather than individual actions (filling in a single field).

I have added a loggedIn flag to your MainPage constructor, which may be helpful in determining which actions are/should be available

class MainPage {   
    public MainPage(bool loggedIn) {
      ...
    } 
}

class LoginPage {   
    public LoginPage(){
       ...
    }

    public MainPage loginExpectSuccess(string username, string password) {
        setUser(username);
        setPassword(password);
        clickJoin();

        return new MainPage(true);
    }

    public LoginPage loginExpectFailure(string username, string password) {
        setUser(username);
        setPassword(password);
        clickJoin();

        return this;
    } 

    private void setUser(string username) {
        ...
    }

    private void setPassword(string password) {
        ...
    }

    private void clickJoin() {
        ...
    }
}

This way, your test can be simplified to just:

MainPage mainpage = new MainPage(false);
LoginPage loginPage=mainpage.clickLogin();
mainpage = loginPage.loginExpectSuccess("whatever", "totally_secret");
...
mainpage.nextUserAction(); //Do whatever a logged in user should be able to do, if user is not actually logged in successfully this will fail

Check out this; http://sellotapetest.blogspot.co.uk/2012/04/navigation-in-page-objects-returning.html?m=1

It explains how to dynamically return different page objects from the same method.

Although it has C# examples, a similar pattern can be achieved in Java

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top