5

What is the standard approach when applying the PageObjects pattern to components of a page?

For the sake of an example lets say I am writing tests for the Features on an Amazon product page.

That page contains a large number of separate features, Product Information, Customers who Viewed this, Other Customers Suggested etc etc.

Current examples I have seen for PageObjects really only cover how to deal with a single page that has limited functionality. What I am looking for is something along the line of a PageObject that would represent the Product page and then be composed of ComponentObjects that represent each component.

eg:

public class ProductPage
{
    [FindsBy(How = How.Id, Using = "productInformation")]
    public ProductInformationControl ProductionInformation{get;set}

    [FindsBy(How = How.Id, Using = "customersViewed")]
    public CustomersAlsoViewedControl CustomersAlsoViewed{get;set}

    [FindsBy(How = How.Id, Using = "customersSuggested")]
    public CustomersSuggestedControl CustomersSuggested{get;set}
}

public class ProductInformationControl
{
    //Ideally the FindsBy here would find the element based on the Controls context
    // So only resolving to an element that was within the context of this control.
    [FindsBy(How = How.ClassName, Using = "title")] 
    private IWebElement _title;

    public string Title
    {
        get
       { 
           return _title.Text; 
       }
    }
}

Then within a test I would access the control like so:

 var productPage = ScenarioContext.Current.Get<ProductPage>();
 Assert.That(productPage.ProductInformation.GetTitle(), Is.EqualTo("My Title"));

I've used this approach previously but with a custom framework built on top of Selenium that allowed for resolution of the child objects and their components. What I'm looking for is the approach to take with Selenium 2 and C# out of the box.

Grant Trevor
  • 1,052
  • 9
  • 23
  • HOpe this helps: I am using the next hierarchy 1. BasePage-> anyOtherPage (if a page holds a section it will implement a function to set focus on that section and call then you can continue working with that section's functions. or hold in the page a member of that section and use it's function directly but then you will have to implement that same functions in the page class) 2. baseSection->anyOtherSection implements all the functionality of the specific section (clicks, sendKeys, validations...) – Avishay May 01 '15 at 19:41

1 Answers1

0

There is no out of the box way to do this. Your approach is very good, you can implement it by calling PageFactory.InitElements() with custom implementation for IElementLocator and IPageObjectMemberDecorator See this answer: How to initialize SelectElements while using PageFactory / FindsBy in Selenium C#?

Community
  • 1
  • 1
Liraz Shay
  • 86
  • 4