My Page Object Approach


So I have been using a page object (PO) approach for a while now, but not been blogging for long, so am going to mark the below as "My Page Object Mach 1", and will continue the theme as my PO's involve.

So to start with I have abstract class which we are going to call DefaultPage, this is essentially my default PO, but it started by just handling WebDriver, like so:


It has since evolved into having several methods for interactions I found myself repeating, such as populated and reading fields, and dealing with Select boxes.

Then each of my page objects inherits the DefaultPage.

The basic structure of my page objects is:
  • Initiate locators.
  • Wait for a specific element
  • Assert that the application has taken you to the correct page, using a specific attribute of an element or could use the URL depending on your application.
Edit: I no longer agree with Assertions in PageObjects. The PageObjects job/role is to allow a test/check to interact with a page, a test/check would soon tell you there is an issue when the PO fails to serve the request. And if you are always running all your tests/checks, which you should be otherwise why bother to create them, you know soon enough if the page header isn't correct!

Which looks like:


I parameterise the timeout value, as I required to increase it for specific tests / environments, so made sense to make it default page of my approach.

I have heard people not recommend putting asserts in your page objects, however I believe that should be the rule for the methods but serves a good purpose in the constructor.

Then objects are instantiated like so:



Perhaps for another post, but I believe this is a good approach, and as mentioned above you can add common methods to the default page.

Appreciate all comments.

3 comments:

  1. When it comes to functional testing, Page Objects are a natural thing and I've been using them way back when I was doing desktop app testing. This pattern is applicable for any kind of app that has some UI, but how you implement Page Objects, depends on testing framework that you use.

    When it comes to Webdriver tests, I also have a root type such as DefaultPage, though I call it BasePageObject. It encapsulates just some common methods and properties.

    Concrete Page Objects contain properties that map to UI elements and common behavior as methods. However, when it comes to instantiating PO, I usually have some factory that creates my PO. Also, I don't make assertions in constructor, I usually have some public method that verifies if PO is displayed.

    ReplyDelete
  2. It would be nice to see how you interact with elements on a Page Object. Can show an example of that?

    ReplyDelete
  3. Hey Ivan,

    I just create methods as required within my object like:

    public string ReadErrorMessage()
    {
    return WebDriver.FindElement(_locators["errorMessage"]).Text
    }

    public bool PopulateDescriptionField(string description)
    {
    WebDriver.FindElement(_locators["txtDescription"]).sendKeys(value);
    if(WebDriver.FindElement(_locators["txtDescription"]).getAttribute("value") == description)
    {
    return true;
    }
    return false;
    }

    Then call them from my tests.

    BlogObject testBlog = new BlogObject(webDriver, 60);
    Assert.isTrue(testBlog.PopulateDescriptionField("This is a test blog"));

    Regards

    ReplyDelete