.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.
You can include below things in the properties of the 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
readFileSync() it will fetch the content as text/string
, now we will have multiple pairs= Sign, which provides us an array. use for loop to go through all pairsdescribe('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"])
});
});
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]
}
});
I am Pavankumar, Having 8.5 years of experience currently working in Video/Live Analytics project.