Uziii
Uziii

Reputation: 841

Selenium WebDriver Page object model description

I am new to selenium webdriver automation. I am using Java programming language, and started to follow page object model. I would like to know the basic file structure, and how to carry it forward.

Practical examples will be very helpful. If anyone can share their sample projects, that would be highly appreciated. Thanks

Upvotes: 1

Views: 995

Answers (3)

vins
vins

Reputation: 15400

Page objects is a well known design pattern, widely accepted by the automation engineers, to create separate class file for each page of the application to group all the elements as properties and their behaviors / business functionalities as methods of the class. But it has few issues in creating a class for a page - especially when the page has more / different sets of elements / complex element like a grid / calendar widget / a HTML table etc.

The class might contain too many responsibilities to handle. It should be restructured and broken into smaller classes. Ie, following the Single Responsibility Responsible.

Check the image here for the idea.

Page Fragments

That is, create reusable page fragments & let the main page object serve the page fragments.

Check here for more info.

Upvotes: 0

sunny bhatia
sunny bhatia

Reputation: 41

You can follow my answer on this URL: Automation Testing Framework

i tried to explain it in a very simple manner.

Also for your reference, you can follow below URL which explains the same. http://www.guru99.com/page-object-model-pom-page-factory-in-selenium-ultimate-guide.html

Upvotes: 1

Rupesh Shinde
Rupesh Shinde

Reputation: 1956

Below is small demo how you can go ahead with the page object model using Java and Selenium.

I have created two classes. One class will be used to store all the web elements on the page and other class will be used to access the web elements of that class to perform intended operations.

Note: Homepage is the class created to store the web elements.

public class Homepage {
    private static WebElement element = null;
    public static WebElement Generic_Search(WebDriver driver){

        element = driver.findElement(By.id("searchTerm"));
        return element;
        }

    public static WebElement Generic_Search_Count(WebDriver driver){

        element = driver.findElement(By.xpath("//div[@class='facet-count']/strong"));
        return element;
        }
    public static WebElement Generic_Search_Submit(WebDriver driver){

        element = driver.findElement(By.className("search-submit"));
        return element;
        }
}

Note: Simple_User_Flow class will be used to access the web elements of Homepage

public class Simple_User_Flow {
    private static WebDriver driver = null;

     public static void main(String[] args) {

    driver = new FirefoxDriver();
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    driver.get("http://materials.springer.com/");
    driver.manage().window().maximize();
    Homepage.Generic_Search(driver).click();
    Homepage.Generic_Search(driver).sendKeys("Mercury");
    Homepage.Generic_Search_Submit(driver).click();
    WebElement Search_Count = Homepage.Generic_Search_Count(driver);
    String Count = Search_Count.getText();
    System.out.println(Count + " Results found for your Search");
    driver.close(); 
}
}

Remember this is very simple demo, You can add multiple classes and use them in your main class. we can group the classes as per their functionality or structural flow.

Upvotes: 0

Related Questions