Skip to content
This repository was archived by the owner on Mar 10, 2020. It is now read-only.

Selenide Comparison

Stefan Ludwig edited this page Nov 25, 2015 · 1 revision

On this page we will compare WebTester's approach to page objects with Selenide's. More information about Selenide can be found on the official website.

Page Object Code

Lets take a look at how page objects are defined:

Selenide

public class LoginPage {

	// NOTE: you could static import all Selenide.XXX static methods

    /* workflows */

    public WelcomePage login (String username, String password) {
        return setUsername(username).setPassword(password).clickLogin();
    }

    /* actions */

    public LoginPage setUsername (String username) {
        Selenide.$("#input_un").setValue(username);
        return this;
    }

    public LoginPage setPassword (String password) {
        Selenide.$("#input_p").setValue(password);
        return this;
    }

    public WelcomePage clickLogin () {
        Selenide.$("#submit").click();
        return Selenide.page(WelcomePage.class);
    }

}

WebTester

public class LoginPage extends PageObject {

    @IdentifyUsing ( "input_un" )
    private TextField usernameField;
    @IdentifyUsing ( "input_p" )
    private PasswordField passwordField;
    @IdentifyUsing ( method = Method.ID, value = "submit", name = "login button" )
    private Button loginButton;

    @PostConstruct
    public void assertThatCorrectPageIsDisplayed () {
        assertThat(getBrowser().getPageTitle(), is("TestApp: Login"));
    }

    /* workflows */

    public WelcomePage login (String username, String password) {
        return setUsername(username).setPassword(password).clickLogin();
    }

    /* actions */

    public LoginPage setUsername (String username) {
        usernameField.setText(username);
        return this;
    }

    public LoginPage setPassword (String password) {
        passwordField.setText(password);
        return this;
    }

    public WelcomePage clickLogin () {
        loginButton.click();
        return create(WelcomePage.class);
    }

}

The main differences between WebTester and Selenide are:

  • WebTester follows the "everything is a page object" principle which allows us to nest page objects within another. (read more about that here)
  • WebTester's out of the box elements only offer methods which are appropriate for the given elements type (cant set the value of a Button).
  • Multi-Threading behavior is not hidden behind static classes, instead we make the Browser instances the centers of control over page creation and configuration management. (at the cost of leaving thread management to the developer / tester)
  • Our EventSystem for firing and reacting to events within the framework and your extensions. (read more about that here)
  • Our debug markings for highlighting every object the framework has interacted with.
  • Our very conservative use of static classes / methods in order to be as customizable and integrateable as possible.

Test Code

Now lets take a look at how this impacts your test code:

Selenide

public class LoginTest  {

    // NOTE: you could static import all Selenide.XXX static methods

    LoginPage startPage;

    /* life cycle*/

    @Before
    public void initStartPage () {
        Selenide.open("http://localhost:8080/login");
        startPage = Selenide.page(LoginPage.class);
    }

    /* tests */

    @Test
    public void testValidLogin () {
        WelcomePage page = startPage.login("username", "123456");
        assertThat(page.getWelcomeMessage(), is("Hello World!"));
    }

    @Test
    public void testInvalidLogin_Password () {
        LoginPage page = startPage.loginExpectingError("username", "bar");
        assertThat(page.getErrorMessage(), is("Wrong Credentials!"));
    }

}

WebTester

@RunWith(WebTesterJUnitRunner.class) 
public class LoginTest  {

    // This example uses the webtester-support-junit module
    // in order to get rid of boilerplate life cycle management code.

    @Resource
    @CreateUsing(ProjectBrowserFactory.class)
    @EntryPoint("http://localhost:8080/login")
    static Browser browser;

    LoginPage startPage;

    /* life cycle*/

    @Before
    public void initStartPage () {
        startPage = browser.create(LoginPage.class);
    }

    /* tests */

    @Test
    public void testValidLogin () {
        WelcomePage page = startPage.login("username", "123456");
        assertThat(page.getWelcomeMessage(), is("Hello World!"));
    }

    @Test
    public void testInvalidLogin_Password () {
        LoginPage page = startPage.loginExpectingError("username", "bar");
        assertThat(page.getErrorMessage(), is("Wrong Credentials!"));
    }

}
Clone this wiki locally