java webdriver page object_浅析selenium的page object模式

Page Object Design Pattern

Page Object is a Design Pattern which has become popular in test automation for enhancing test maintenance and reducing code duplication. A page object is an object-oriented class that serves as an interface to a page of your AUT. The tests then use the methods of this page object class whenever they need to interact with the UI of that page. The benefit is that if the UI changes for the page, the tests themselves don’t need to change, only the code within the page object needs to change. Subsequently all changes to support that new UI are located in one place.

The Page Object Design Pattern provides the following advantages

1. There is a clean separation between test code and page specific code such as locators (or their use if you’re using a UI Map) and layout.

2. There is a single repository for the services or operations offered by the page rather than having these services scattered throughout the tests.

In both cases this allows any modifications required due to UI changes to all be made in one place. Useful information on this technique can be found on numerous blogs as this ‘test design pattern’ is becoming widely used. We encourage the reader who wishes to know more to search the internet for blogs on this subject. Many have written on this design pattern and can provide useful tips beyond the scope of this user guide. To get you started, though, we’ll illustrate page objects with a simple example.

First, consider an example, typical of test automation, that does not use a page object.

/***

* Tests login feature

*/

public class Login {

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");

}

}

There are two problems with this approach.

There is no separation between the test method and the AUT’s locators (IDs in this example); both are intertwined in a single method. If the AUT’s UI changes its identifiers, layout, or how a login is input and processed, the test itself must change.

The ID-locators would be spread in multiple tests, in all tests that had to use this login page.

Applying the page object techniques, this example could be rewritten like this in the following example of a page object for a Sign-in page.

/**

* Page Object encapsulates the Sign-in page.

*/

public class SignInPage {

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 a Home page could look like this.

/**

* Page Object encapsulates the Home Page

*/

public class HomePage {

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());

}

}

public HomePage manageProfile() {

// Page encapsulation to manage profile functionality

return new HomePage(selenium);

}

/*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*/

}

So now, the login test would use these two page objects as follows.

/***

* Tests login feature

*/

public class TestLogin {

public void testLogin() {

SignInPage signInPage = new SignInPage(selenium);

HomePage homePage = signInPage.loginValidUser("userName", "password");

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

"Login was unsuccessful");

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值