Saturday, November 14, 2015

POM , Page Object Model design pattern without using page factory class (WORK IN PROGRESS)

POM is a design pattern that is implemented using selenium webdriver, where every webpage or a major functionality within a page is consider as a object. For every page there is a java class file.
The page object pattern simply abstracts your business logic away from the physical structure of your pages. What the PageFactory class gives you is the ability to use annotations to automatically find the elements on the page without resorting to explicit findElement calls.


I will give you example around the below application under test for implementing POM. The POM would be implemented not using PageFactory class.





Let me describe the application first. The user will first login to the application using the login page. Depending on user ID (authorization) application link and report for the application would be visible. For example lets say a user with user ID "test@abc.com" log into the application. For this user only application A and application C is visible on the application page, and this user can access all reports related to application A and C.


Test Scenario - 1. 
Verify all reports for users refresh without any error and find the report refresh time.

My first step is to create a java properties file (for example objectrepo.properties) and enter all the locator for the web element on the file
The naming convention followed is

  • locator prefix with L,
  • ID prefixed with I
  • Xpath prefixed with X
  • CSS selector prefixed with C
  • sendKeys parameters prefix with S. 
Object Repository Example

properties file


To use all the locators in every class of Page Object Model, I will create a ObjectRepository.class.
Within this class I will have a method which will find the "Value" for a corresponding "Key" in properties file. This method would have String return type, which is basically returning the property "Value" against a "Key". All the methods within this class would be class methods i.e. static methods instead of instance method. So that the methods can be called in other classes without creating object of the ObjectRepository.class class.

This is my locator objector repository. The locators in object repository can be accessed through java Properties class. The method getPROPERTIES method accepts an argument string i.e the KEY and return a VALUE.


Example:

      public static String getPROPERTIES(String KEY) {  
           try {  
                File file = new File("C:\\FRAMEWORK\\object.properties");  
                FileInputStream inStream = new FileInputStream(file);  
                Properties property = new Properties();  
                property.load(inStream);  
                VALUE = property.getProperty(KEY);  
           } catch (IOException e) {  
                // TODO Auto-generated catch block  
                e.printStackTrace();  
           }  
           return VALUE;  
      }  




* ObjectRepository Class

Now let's design the ObjectRepository.class. The main objective of this class is to have several methods which would return the value from the property file. For that I will have methods like

  • getURL ---> This will return me server_name:port/abc
  • getUserName---> This will return me username-id
  • getPassword---> This will give me pwd-id 


That's it we are done with the ObjectRepository.class. The more key value pair you add in the properties file either the WebElement locator or the sendKey argument or any other argument add a static field and a getmethod in the ObjectRepository.class.

Building the POM

* First Page

On the application under test my first page is a login page. Following the POM design pattern, I will create a class for the login  page. The AUT (application under test) opens a new window (pop) every time I click on any of the application (application -A and application -C in my test). For further navigation to next window which is a new window I have to get hold of the new window handle and switch to the new window. As every time the only way to go to the next screen is through a new window the login method within the login.class i will return the handles as list. I can further iterate through the list in other pages/ class and get the windows handles.


* Second Page
Now I have already done the login and clicked on application -A and opened reports for application -A. Here my task is to find all the reports name which are present inside a table.


No comments:

Post a Comment