Sunday, January 2, 2011

Issues with Page Objects Pattern

From Selenium 2, comes the Page Objects Pattern, it is hailed as a way to solve the issues of code maintenance and duplication in test automation.

Let me describe the Pattern as I understood. Then criticism and why it can't solve the issues it promises.

Here are three sample test cases,

Case A.
1. Login to Blogger.com
2. Click Dashboard link
3. Click New Post

Case B

1. Login to Blogger.com
2. Click Dashboard link
3. Click New Post
4. Click Comments

If we write a cases in a traditional way, it will look like this,

public void test_caseA(){
     selenium.type("username","test");
     selenium.type("password","test");
     selenium.click("login");
     selenium.click("link=Dashboard");
     selenium.click("link=NewPost");
}

public void test_caseB(){
     selenium.type("username","test");
     selenium.type("password","test");
     selenium.click("login");
     selenium.click("link=Dashboard");
     selenium.click("link=NewPost");
     selenium.click("link=Comments");
}
Here we can move  same actions into a another function, like this,

In ClassA,

public ClassA() { 
     public void login(String username,String password){
          selenium.type("username",username);
          selenium.type("password",password);
          selenium.click("login");
     }
In ClassB,


public void test_caseA(){
     ClassA.login("test","test");
               selenium.click("link=Dashboard");
     selenium.click("link=NewPost");
}
However, this is an simple example. It is possible to have all the functions in related to one page in a class and call that using method names. In a nutshell, even if the page changes, change is needed in only one place. That is the purpose of this Page Objects Pattern. But, what is the issue with this?

This approach doesn't address the change in sequence steps. UI automation has two issues which needs maintenance of code.  

1. Element changes in UI: Changes such div element changed to span or radio button becomes a check box. 
2. Design or flow changes:  Let say, need to automate a three step sign up process, each step is a page. After releasing, all the three steps combined into one page. If the tester scripted for three step signup, it need to be changed. 

The first issue is addressed by Page Objects Pattern, but not the second. If the second issue occurs, then using this pattern will not solve the purpose. 

So, what can be done? I think, using Behavior driven development model can be answer to this issue. More on that later. 

Rajasankar. 

ShareThis

raja's shared items

There was an error in this gadget

My "Testing" Bundle