Properties file in Protractor

.properties is a file extension for files mainly used in Java related technologies to store the configurable parameters of an application. Though it is majorly used for Java, we can use the .properties file along with javascript technologies.

Properties files stores simple parameters as key-value pairs, outside of compiled code. Key and Values are separated using '=' sign

Each parameter in properties file is stored as a pair of strings, in key-value pair format, where each key is on one line. You can easily read properties from some files using different ways, like with packages.

properties-file-protractor

Reading a JSON File

What is the use of config.properties file

  • Properties are used to externalize the data, which is configurable, and if you put that data in your code (test script), you have to build the code each time you want to change the value of the property. The main advantage of properties is that they are outside your source code, and you can change them anytime.
  • Each parameter is stored as a pair of strings, one storing the name of the parameter (called the key), and the other storing the value.
  • Whatever constant and generic entities are there in our project, we can define in config.properties file

Things to remember about the properties file format

  • [key] = [value] or [key] : [value]
  • For comments, statements start with either ! or #.
  • White space at the beginning or before and after of = or : are ignored.
  • Blank lines are ignored.
  • If the value is written in multi-line, then each line should be ended with �? (backslash).

What can we include in the properties file

You can include below things in the properties of the protractor.

  • AUT URL - Application Test URL where you are going to Run the Protractor code.
  • Username & Password - If your application uses the login, you can provide the credentials of Login as well.
  • Selenium Grid Hub URL - Selenium Hub URL where your scripts are running (You will need when you will running scripts remotely, default it is localhost)
  • Selenium Grid Hub Port - Selenium Hub Port where your scripts are running (You will need when you will running scripts remotely, default it is 4444)
  • Browser - You will need a browser name to run the script, you can mention it as well in properties.
  • Test Data - You can pass the test data files like CSV and Excel path here to run the data-driven script.
  • Locators - You can store the locators in the properties file so that we can change locator without compiling the code

Read CSV file using Papaparse

Handle Properties File in Protractor

We can use different packages to handle the properties file in protractor, but let's handle it without using any packages.

By this time you know that properties file has key and value pairs, each pair is separated by a new line, below are the steps to read the file

  • Read the file and store the content using fs module
  • Create an empty map, which will store the keys and values as pairs
  • When you read the file content using readFileSync() it will fetch the content as text/string
  • Split this string using a new line , now we will have multiple pairs
  • Split each and every pair with = Sign, which provides us an array. use for loop to go through all pairs
  • The Array contains the first element as a key and second element as value.
  • Push your keys and values into the already created array
describe('Protractor Typescript Demo', function() {
	it('Properties file Operations', function() {
		var fs = require("fs")
		var rawContent = fs.readFileSync("./data.properties")
		var propertyMap = {}
		var fullContent = rawContent.toString()
		var allPairs = fullContent.split("
")
		for(var i = 0; i<allPairs.length; i++){
			var keyValue = allPairs[i].split("=")
			propertyMap[keyValue[0]] = keyValue[1]
		}
		console.log("****Full content  : ")
		console.log(propertyMap)
		console.log("****Spcific content  : "+propertyMap["place"])
	});
});

Excel File handling in Protractor

Properties file as Object Repository in Protractor

As I mentioned earlier, we can use the properties file to store the locators; this usually called an Object Repository. Example Object Repository:

searchBar = //input[@name='q']
searchButton = //input[@name='btnK']


For moment sake, I have created test cases and object parsing methods in the same file, but they usually will be different files.

var fs = require("fs")
describe('Protractor Typescript Demo', function() {
	it('Properties file Operations', function() {
		browser.get('https://google.com');
		element(by.xpath(getXpathfromProperty("searchBar"))).sendKeys("hello")
		element(by.xpath(getXpathfromProperty("searchButton"))).click()

	});
	function getXpathfromProperty(key:string){
		var rawContent = fs.readFileSync("./data.properties")
		var propertyMap = {}
		var fullContent = rawContent.toString()
		var allPairs = fullContent.split("
")
		for(var i = 0; i<allPairs.length; i++){
			var keyValue = allPairs[i].split("=")
			propertyMap[keyValue[0]] = keyValue[1]
		}
		return propertyMap[key]
	}
});

Errors in Protractor

Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions
  • Srinivas
    I am getting below error.
    
    F.(node:33872) UnhandledPromiseRejectionWarning: NoSuchElementError: No element found using locator: By(css selector, *[id="'username'd "])
    Reply
  • Jeremy Kahan
    This is helpful as is. It might be a nice touch to ignore lines starting with #. It looks to me like it would be a relatively easy change.
    Reply
    • Jeremy Kahan
      Or am I missing something and it already does?
      Reply