Most of the times, many project demands start of best fit automation framework from scratch. Page Object Model (POM) is most popular design pattern for the Selenium automation.
In this post, lets try to figure out the details. Before diving in its better to look at this post to get insight of what we are talking here.
Lets us look at the following different packages for the purpose of the understanding.
pages contains the test pages for the site, where as each page contains the objects corresponding to that page.
In any UI testing we typically do two things with objects, either we retrieve properties for it or we perform actions on it.
In the start section we write necessary code to start the browser. Driver session creation, extent test creation, logger creation etc.
The tests section contains the test classes we are intended to write.
The utilities section contains the common actions class and the reusable action classes.
Also, its better use config properties for the entire test setup along with the test data sheets. Also maintain drivers in different folder for better readability.
As shown above utilities package contains the excel manipulations such as get and set data operations.
Lets deep dive into start class and what it is doing.
Start class has all declarations and webdriver creation depending the config settings.
Create config file with browser type and other details so that we can use them in the code.
Start class will have the entire code for reading the config file and depending on the browser type mentioned in the config it creates the web driver session.
If we look at the declarations, the entire project is using extent reports as it will generate beautiful reports for the test execution.
Also, all other variables are declared as static where as driver is non static. The report logger is also static, since once we create start instance driver will be recreated but not the report logger. This will help to run the same test in multiple iterations keeping the same report.
If we look at the declarations, we are declaring the xldriver, extent report and logger properties as static as they dont need to be re initialized for loop execution. (Static variables are created once, they are not recreated for each instance of class)
the launch_browser method returns the pagefactory object with created driver. This will flow to subsequent pages.
Will see why it is returning the page factory object.
If we look at the pagefactory class, it has definitions of all pages.
We are using pagefactory constructor to send the driver to all the sub subsequent classes.
And, the home and other pages will inherit this class so that they can get the latest driver created by the start class.
Each page class is contains the deification of elements and initialization of elements.
We will use PageFactory class (Selenium specific class) along with ajaxelemetlocator since it has advantage of creating element only when it is being used, we can also define the time duration for the locating element. We are also using reference class object for the common functions.
The reference, and the home class gets the driver created in the start class.
Also the each class in the page returns the same page for better creation of tests.
And finally coming to writing test cases.
Starting with method name, we are using the data sheet name as test method name.
So to avoid hard coding of the methodname passing to excel setExcelSheet method , we are programatically getting method name and assigning it.
Since xldriver and set_logger method is static, we dont have create start class object to use them.
And looking at the loop, we are getting rows in the excel sheet and running the required actions for each row. That means creating driver doing all actions and logging out.
We can restrict this for certain actions if we dont want to quit the browser, by keeping the creating of start object outside the for loop.
And by keeping (.) at the launch_browser(), we get access to the methods in pagefactory class, and since all other classes are extending pagefactory class, all of them gets access to pagefactory class methods. Thats the reason for writing page navigation methods in the pagefactory class.
This way we can write single line test cases, this will ease the reading of test case and writing and debugging them.
the output for the test looks like below.
This is gist of implement page object model with one sample testcase.
In this post we have not included the excel data manulations, hash maps and extent report creation.
We will try to address them in subseqnet posts.
Thank you.