P. 1
Selenium Documentation

Selenium Documentation

|Views: 848|Likes:
Publicado porDivyesh Dave

More info:

Published by: Divyesh Dave on Mar 16, 2011
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See more
See less

04/21/2011

pdf

text

original

Page Object Design Pattern models the page specific behaviour in a corresponding class which repre-
sents services (public methods) offered by page object. Selenium tests use page object to interact with
application and their own verification/assertion to validate page. Page Object Design Pattern brings
following advantages -

1. There is clean separation between automation code which knows about application html and the one
which carries out actual tests.

2. There is single repository of services offered by page, instead of application html scattered through
out the tests. Hence improved maintainability and reduction in code duplication.

Let us see these problems in more detail by considering a selenium test -

/***

* Tests login feature
*/

public class Login extends SelTestCase {

public void testLogin() {
selenium.type( "inputBox" , "testUser" );

selenium.type( "password" , "my supersecret password" );

selenium.click( "sign-in" );

selenium.waitForPageToLoad( "PageWaitPeriod" );

Assert.assertTrue(selenium.isElementPresent( "compose button" ),

"Login was unsuccessful" );

}

}

7.8. Page Object Design Pattern

109

Selenium Documentation, Release 1.0

There are two primary problems with this approach -

1. There is no separation between test method and application html as both of them are intertwined in
one method.

2. Application html would be spread in multiple tests, which induces unwarranted redundancy and
makes test maintenance difficult.

Page Object may represent an entirely new page or just part of page. If user action causes control to be
directed to new page then it is represented by new page else same page object could be returned.

For example login feature on sign-in page could be represented by HomePage class for user as sign in
operation directs user to home page of logged in user. While auto suggest window of a search engine
could be represented by same page as auto suggest window appears on the same page on which user is
types in search term.

Applying the page object techniques, above mentioned example could be rewritten as

Page Object for Sign-in page -

/**

* Models sign-in page for user
*/

public class SignInPage extends SelTestCase {

private Selenium selenium;

public SignInPage(Selenium selenium) {
this.selenium = selenium;
if(!selenium.getTitle().equals( "Sign in page" )) {

throw new IllegalStateException( "This is not sign in page, current page is: "
+selenium.getLocation());

}

}

/**

* Login as valid user
*
* @param userName
* @param password
* @return HomePage object
*/

public HomePage loginValidUser(String userName, String password) {
selenium.type( "usernamefield" , userName);

selenium.type( "passwordfield" , password);

selenium.click( "sign-in" );

selenium.waitForPageToLoad( "waitPeriod" );

return new HomePage(selenium);

}

}

and page object for Home page would look as -

/**

*Models features presented by Home page
*/

public class HomePage extends SelTestCase {

110

Chapter 7. Test Design Considerations

Selenium Documentation, Release 1.0

private Selenium selenium;

public HomePage(Selenium selenium) {
if (!selenium.getTitle().equals( "Home Page of logged in user" )) {

throw new IllegalStateException( "This is not Home Page of logged in user, current page" +

"is: " +selenium.getLocation());

}

}

/*More methods offering the services represented by Home Page
of Logged User. These methods in turn might return more Page Objects
for example click on Compose mail button could return ComposeMail class object*/

}

and updated login test looks as -

/***

* Tests login feature
*/

public class TestLogin extends SelTestCase {

public void testLogin() {

SignInPage signInPage = new SignInPage(selenium);
signInPage.loginValidUser( "userName" , "password" );

Assert.assertTrue(selenium.isElementPresent( "compose button" ),

"Login was unsuccessful" );

}

}

Page objects themselves should never be used to make verification/assertion and it is responsibility of
tests to validate application state. Though there is an exception to it. While instantiating a page object
it could be checked whether control is really on expected page or not. Hence in the examples illustrates
above both SignInPage and HomePage constructors check if control is on right page.

You're Reading a Free Preview

Descarga
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->