.properties File Example

Whenever you talk about repository, by the name itself you deduce that it is kind of storage. Object repository is the collection of object and object here is locator. Here locator means web element id, name, CSS, XPath, class name etc.

Using Object repository is highly recommended rather than hard coding the objects and their properties directly into the code as it reduces the maintenance effort and provides positive ROI. For example, if any of the object property changes within AUT and this particular object is referenced in 20 tests, we can easily change it in external object repository file, rather than searching and doing updates for that object individually in the code at 10 different places.

The major drawbacks of traditional OR are:
  1. If there are 100 objects/ properties, which is not a stretch for semi-big to big projects, it can become tedious to comb thru and find the element to update and is not easy to maintain.
  2. Code is a bit more cumbersome as compared to the other options - namely - POM and Page Factory
I kinda did a twist and instead of creating OR properties files, I created classes with names that would make sense and add the objects/ elements to those classes ... something very similar to POM or Page Factory. I will be providing examples of both - Properties file and my funky OR Classes.

You can create .properties file like so: 
  1.  Right click on the Package in the solution Explorer of Eclipse-> New ->File
  2. Give name to the file with .properties extension (eg.: gmailLogin.properties) ->Click Finish
Now we will create individual .properties file for every single page and capture all the UI elements present on the page and use it as per the needs. We can add the values as well in the .properties file.

Sample .property file:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
/*
 * I will be listing By methods (without using pageFactory) that I have used
 * For some methods I am using gmail example.
*/
# gmail_URL
gmail.URL=https://www.gmail.com
# gmail_SignIn
gmail.SignInButton = //*[@data-g-label = 'Sign in']
# gmail_UN
gmail.Email=//*[@id = 'Email']
emailValue = username
# gmail_Next
gmail.Next = //*[@name= 'signIn']
# gmail_PW
gmail.PW = //*[@type= 'password']
passwordValue = password


You can now call the objects from the above .properties file like so:


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// Sample to call the reusable Properties file  
 @Test
public void propFileLoginSample()
{
 //Creating the File Object
 File file = new File("path to .properties file");

 //Creating properties object
 Properties prop = new Properties();
 
 //Creating InputStream object to read data
 FileInputStream objInput = null;
 
 try {
   objInput = new FileInputStream(file);
   
   //Reading properties key/values in file
   prop.load(objInput);
   
   //Closing the InputStream
   objInput.close();
   
  } catch (Exception e) {
    System.out.println(e.getMessage());   
  } 
 browserFactory.startBrowser("chrome", prop.getProperty("gmail.URL"));
 driver.findElement(By.xpath(prop.getProperty("gmail.SignInButton"))).click();
 driver.findElement(By.xpath(prop.getProperty("gmail.Email"))).clear();
 driver.findElement(By.xpath(prop.getProperty("gmail.Email"))).sendKeys(prop.getProperty("emailValue"));
 driver.findElement(By.xpath(prop.getProperty("gmail.Next"))).click();
 driver.findElement(By.xpath(prop.getProperty("gmail.PW"))).clear();
 driver.findElement(By.xpath(prop.getProperty("gmail.PW"))).sendKeys(prop.getProperty("passwordValue"));
}


As you can see this method is better than hard-coding, but is inefficient in comparison to Creating OR Class or POM or Page Factory methods.


No comments:

Post a Comment