Take the 2-minute tour ×
Software Quality Assurance & Testing Stack Exchange is a question and answer site for software quality control experts, automation engineers, and software testers. It's 100% free, no registration required.

I've been using Selenium off and on for the past two years. Recently I've been seeing more references to page objects. (I'm referring to page objects as an means of organizing UI automation code -- not an interface or implementation manifested in the Selenium API.) What are the advantages and disadvantages of using pages objects relative to using a mere collection of functions? Please attempt to justify the advantages and disadvantages.

share|improve this question

4 Answers 4

Advantages:

  • Clear separation between test code and navigation code in code base.
  • Easy to comprehend test, for example -

    Homepage.testLogin(username, password);
    

    is more succinct than

    selenium.type(username);
    selenium.type(password);
    selenium.click(submit);
    selenium.waitForpageToLoad(waitPeriod);
    
  • Ease of maintenance, when UI changes you need to change the navigation code and not test (provided application logic is still the same and only UI object/navigation is changed)

Disadvantage:

The only one I could think of is, initial design of page object might take more time but pain is definitely worth it

share|improve this answer
1  
You could do the same thing with a function, without going to the trouble of defining an object, so I don't think this answers the question. –  user246 May 14 '11 at 20:34
    
Tarun is it like page objects are methods or each page object will have its member function? –  Rakesh Prabhakaran May 16 '11 at 1:48
    
@user246 - True for the example Tarun has used, but not true generally. PageObjects ensure defining an object once, and reusing it everywhere else. If you define multiple methods around an object, the part of your code that identifies the object will be scattered around your entire code, which will create a maintenance nightmare. –  Suchit Parikh May 17 '11 at 1:19
2  
@Suchit Perhaps this is just a matter of our using different terms. When you say, "PageObjects ensure defining an object", what object are you referring to? Functions work just as well as objects for encapsulating logic. There is a stronger argument for using objects if you need inheritance or if you need to encapsulate state. However, it sounds to me like most PageObjects are basically stateless proxies -- just collections of functions. –  user246 May 17 '11 at 13:43
    
@user246 - Agreed. And yes, objects itself become more useful when there is inheritance and encapsulation i.e. actual OO implementation. –  Suchit Parikh May 17 '11 at 21:07

Advantages:

  1. (OOP) Separation of defining objects from implementing them in methods. Define once, use it multiple times.

  2. Clarity of tests / self-documentation

    loginPage.typeUsername;
    loginPage.typePassword;
    loginPage.submit;
    
  3. Easy to maintain - e.g. if the login page elements get updated, you can just go to your loginPage Page Object class and update the methods identifying the elements

Disadvantage:

  1. Takes time to build the infrastructure (as mentioned before)
  2. Requires good enough programming skills to design the automation suite (+ or - depending on the programming skills of the team)
share|improve this answer
    
+1 for "easy to maintain". I think both disadvantages are simply costs of investing into tests that are easy to maintain. There's never a free lunch, one just must make sure there is ROI/gain from this investment, i.e., costs of maintenance is lowered than cost of building page object model and employing skilled engineers. –  dzieciou Jun 2 at 21:35

Though there are many advantages of Page Project Model, some of them are :

  1. Simple and clear page classes with sensible method names.

  2. You can actually give the customize names to you methods. Like above so that you need not to keep anything in mind.

  3. Just looking into the method name gives you all idea about the capabilities of the method.

  4. Makes tests more readable. In comparison to above commands of selenium , where in you need to add all the commands in the tests scripts . in page object model you need to put method names. The methods which you have created according to your understanding of application so these methods name are more readable and easy to understand.

  5. Stay DRY. Page object model believes in the principle of Do not repeat yourself.

  6. Good support of tests, because everything is stored in one place.

  7. Easy creation of new tests. In fact, tests can be created by a person not knowing the features of automation tools.

As I have actually implemented it in my project so definitely there are some flaws:

  1. All locators should be kept in page class file.

  2. And this abstraction leads to some chaos with in the Page Class file. So you need to implement something like key word driven on top of Page Object Model so as to fully take the advantages.

share|improve this answer
    
+1 for mentioning DRY rule. –  dzieciou Jun 2 at 21:29

There are three main advantages to putting your functions into PageObjects rather than just having a bunch of static functions:

  1. Context. LoginPage.clickNext() vs ItemDetails.clickNext() immediately tells you what page you're on when looking at the test, even if they're identical methods inherited from a parent class.
  2. The PageObjectFactory can automatically initialize member variables of a PageObject so you don't have to do a find() every time you want to interact with something. This keeps your code cleaner, but assumes you use the PageObject paradigm and will work best if you do so.
  3. Ease of maintenance of code; if you're using Java or .NET, everything has to be in a class anyway, so it's easier to break it up into logical groupings by page rather than putting everything in the same static class. Naming your page classes well allows you to easily find the methods you need to update when a given page changes.

In actual practice, PageObjects are primarily a way of organizing code rather than anything too difficult or complicated to implement. They don't provide much in the way of performance improvements or enable much you can't do otherwise, but they make your code cleaner and easier to read and maintain.

share|improve this answer
    
+1 for PageObjectFactory. Annotating WebElements lets you separate completex locators for page elements from operations on them. –  dzieciou Jun 2 at 21:44

protected by user246 Jun 2 at 11:52

Thank you for your interest in this question. Because it has attracted low-quality answers, posting an answer now requires 10 reputation on this site.

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.