Navin Navin - 3 months ago 17
C# Question

How to add repeatable sections to page object model with selenium and c#?

I am trying to automate web tests using Selenium and C#.

I have a web application with certain sections like header and footer repeating across multiple pages.

Lets say I have sections SecA, SecB, SecC. In my pages it would appear as Page1 would have {SecA, SecB, SecC}, Page2 would have {SecA, SecB} and Page3 would have {SecB, SecC}.

I don't want to create duplicates in every page object. How do I define these sections and include it in to my page class?

It would be great if you could provide some example code.

Answer
//SecA:
public class SecA
    {
    [FindsBy(How = How.Id, Using = "Id")]
    private IWebElement SecAField;
    //Add Getters, setters, methods, constructors, etc..

}

//SecB:
public class SecB
{
    [FindsBy(How = How.Id, Using = "Id")]
    private IWebElement SecBField;
    //Add Getters, setters, methods, constructors, etc..
}

//SecC:
public class SecC
{
    [FindsBy(How = How.Id, Using = "Id")]
    private IWebElement SecCField;
    //Add Getters, setters, methods, constructors, etc..
}

//Page 1:

public class Page1
{
    public SecA secA { get; set; }
    public SecB secB { get; set; }
    public SecC secC { get; set; }

    //Add Getters, setters, methods, constructors, etc..
    public Page1()
    {
        secA = new SecA();
        secB = new SecB();
        secC = new SecC();
    }
}