Featured Posts
Friday, October 11, 2019
Monday, May 29, 2017

Parsing JSON data values present at any level in GROOVY
Monday, May 29, 2017
-
No comments
Whenever we deal with JSON we need to parse it at a specific level. What if I don't have a location to parse to parse specific value, how we will do that?.
e. g if have below JSON and we want to parse it to get values of analytics Id
e. g if have below JSON and we want to parse it to get values of analytics Id
{
"buildInfo": {
"application": "RepCatalog",
"version": "1.0.0",
"buildDate": "Thu Oct 13 17:01:48 IST 2016"
},
"status": "success",
"data": {"analytics": [ { "status": "success",
"catalogRep": {
"analyticsId": 48961,
"ReportType": "REPORT",
"title": "REPORT1476371357505",
"rights": [ {
"availability": "PUBLIC",
"isDefault": true
}],
"initialCreation": "10/13/2016 20:39:11",
"lastModified": "10/13/2016 20:39:11",
"lastAccessed": "10/13/2016 20:39:11",
"status": "OPEN"
},
"cascadeCount": 0
}]},
"time": 6674
}
I struggle alot the and find the working solution around it
I have written following function to get all values of analyticId
def extractRepIds (def tree, def ids = []) {
switch (tree) {
case Map:
tree.each { k, v ->
if (k == "AnalyticsId") { ids << v }
extractRepIds(v, ids)
}
return ids
case Collection:
tree.each { e -> extractRepIds(e, ids) }
return ids
default :
return ids
}
}
Now I have to call that function for values
def extractRepIdsFromJson(def jsonString) {
def tree = new JsonSlurper().parseText(jsonString)
extractRepIds(tree)
}
Friday, May 26, 2017

Running chrome with extension while executing test cases using PROTRACTOR
Friday, May 26, 2017
-
No comments
Many of the guys have tried running Chrome browser with plugin or extension using protractor but couldn't do that. I did some researched and found solution for it. Google Chrome always run extension with uncompressed version which is present on Users\userName\AppData\Local\Google\Chrome\User Data\Default\Extensions The typical solutions we found on the internet to set arguments in Protractor.conf.js as below
The typical solutions we found on the internet to set args in Protractor.conf.js as below
capabilities: {
'browserName': 'chrome',
'chromeOptions': {
'args': ['--load-extension=' + '/Users/prashanthsams/Downloads/addon.crx']
}
}
but above solution will not work with the latest version of Chrome, here we need to use uncompressed version plugin. so how will do that?.
We need to set the path as below
capabilities: {
'browserName': 'chrome',
'logName': 'Chrome',
'count': 1,
'chromeOptions': {
'args': [ '--lang=en', '--window-size=1366,768','--load-extension=' + 'C:/Users/ahusain/AppData/Local/Google/Chrome/User Data/Default/Extensions/idgpnmonknjnojddfkpgkljpfnnfcklj/2.1.2_0' ]
}
}
Below is the snapshot running protractor test on Chrome with extension using above solution
How you like this post. Please provide you input on this.This issue is very common during automation but hardly find this solution on the net as of now. If it's present there is couldn't find it.
Please leave your comment and let me know if any additional details required around it.
The typical solutions we found on the internet to set args in Protractor.conf.js as below
capabilities: {
'browserName': 'chrome',
'chromeOptions': {
'args': ['--load-extension=' + '/Users/prashanthsams/Downloads/addon.crx']
}
}
but above solution will not work with the latest version of Chrome, here we need to use uncompressed version plugin. so how will do that?.
We need to set the path as below
capabilities: {
'browserName': 'chrome',
'logName': 'Chrome',
'count': 1,
'chromeOptions': {
'args': [ '--lang=en', '--window-size=1366,768','--load-extension=' + 'C:/Users/ahusain/AppData/Local/Google/Chrome/User Data/Default/Extensions/idgpnmonknjnojddfkpgkljpfnnfcklj/2.1.2_0' ]
}
}
Below is the snapshot running protractor test on Chrome with extension using above solution
How you like this post. Please provide you input on this.This issue is very common during automation but hardly find this solution on the net as of now. If it's present there is couldn't find it.
Please leave your comment and let me know if any additional details required around it.
Thursday, May 25, 2017
Reading Properties file in java
Thursday, May 25, 2017
-
No comments
You can find many ways to read properties file in java, here I am explaining to read properties file using java.io.File and java.util.Properties.
Let say we have below properties file example dataFile.properties which we need to read
Here each variable is for values from each property. Hope you like it. Now this variable can be used anywhere in your code where u need this properties values .
Let say we have below properties file example dataFile.properties which we need to read
Now we have to read this properties in below manner
import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.util.Properties; public class ReadDataFile {public static String USER; public static String PASSWORD; public static String BROWSER; public static String DBURL; public static String DBUSER; public static String DBPWD; public static String DASHBOARDURL; public static void ReadPropertyFile() throws IOException { File file = new File("dataFile.properties"); FileInputStream fileInput = null; try { fileInput = new FileInputStream(file); } catch (FileNotFoundException e) { e.printStackTrace(); } Properties prop = new Properties(); try { prop.load(fileInput); } catch (IOException e) { } USER = prop.getProperty("USER"); PASSWORD = prop.getProperty("PASSWORD"); BROWSER = prop.getProperty("BROWSER"); DBURL=prop.getProperty("DATABASEURL"); DBUSER=prop.getProperty("DB_USER"); DBPWD=prop.getProperty("DB_PWD"); DASHBOARDURL = prop.getProperty("DASHBOARDURL"); } }
Here each variable is for values from each property. Hope you like it. Now this variable can be used anywhere in your code where u need this properties values .
Writing Custom HTML report when building Keyword Driven Framework
Thursday, May 25, 2017
-
No comments
When we build a keyword driven framework, we put more focus on actions which we want to execute. When we use standard reporting using TestNG , its require the multiple test methods which are executed and shown in the report.When its comes to Keyword Driven framework, we have single test method which runs all the actions coming from test cases keywords. to handle this we required custom reporting of each action execution.Now, how to handle it. Here I have created two classes Reported and result.
Before doing all we required a basic HTML template which is required to create a report. Following is template I have created
<html>
<head>
<title>Report generated on #DATETIME#</title>
<head>
<body>
<h3>Test results</h3>
<table>
<tr>
<th width="20%">Test Case</th>
<th width="10%">Step</th>
<th width="20%">Description</th>
<th width="10%">Result</th>
<th width="10%">Actions</th>
</tr>
<!-- INSERT_RESULTS -->
</table>
</body>
It looks like a below
Now created Reporter class as follows
package frameWorkUtility;
import config.Constants;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import static config.Constants.reportTemplatePath;
public class Reporter {
private static List<Result> details;
private static final String resultPlaceholder = "<!-- INSERT_RESULTS -->";
private static final String templatePath = reportTemplatePath;
public Reporter() {
}
public static void initialize() {
details = new ArrayList<Result>();
}
public static void report(String actualValue,String expectedValue,String Keyword, String testCaseId,String testStepId,String testDescription) {
if(actualValue.equals(expectedValue)) {
if(Keyword.contains("="))
{
Result r = new Result("====",Keyword,testCaseId,testStepId,testDescription);
details.add(r);
}
else
{ Result r = new Result("Pass",Keyword,testCaseId,testStepId,testDescription);
details.add(r);
}
} else {
if(Keyword.contains("="))
{
Result r = new Result("====",Keyword,testCaseId,testStepId,testDescription);
details.add(r);
}
else {
Result r = new Result("Fail", Keyword, testCaseId,testStepId,testDescription);
details.add(r);
}
}
}
public static void showResults() {
for (int i = 0;i < details.size();i++) {
System.out.println("Result " + Integer.toString(i) + ": " + details.get(i).getResult());
}
}
public static void writeResults() {
try {
String reportIn = new String(Files.readAllBytes(Paths.get(templatePath)));
for (int i = 0; i < details.size();i++) {
if(details.get(i).getTestCaseId().contains("Scenario"))
{
reportIn = reportIn.replaceFirst(resultPlaceholder, "<tr><td style=\"text-align: left;\">" + details.get(i).getTestCaseId() + "</td><td style=\"text-align: center;\">" + details.get(i).getTestStepId() + "</td><td style=\"text-align: center;\">" + details.get(i).getTestDescription() + "</td><td style=\"text-align: center;\">" + "</td><td style=\"text-align: center;\">"+details.get(i).getResultText() + "</td></tr>" + resultPlaceholder);
}
else {
reportIn = reportIn.replaceFirst(resultPlaceholder, "<tr><td style=\"text-align: center;\">" + details.get(i).getTestCaseId() + "</td><td style=\"text-align: center;\">" + details.get(i).getTestStepId() + "</td><td style=\"text-align: center;\">" + details.get(i).getTestDescription() + "</td><td style=\"text-align: center;\">" + details.get(i).getResult() + "</td><td style=\"text-align: center;\">" + details.get(i).getResultText() + "</td></tr>" + resultPlaceholder);
}
}
String currentDate = new SimpleDateFormat("dd-MM-yyyy_HHmmss").format(new Date());
String reportPath = Constants.reportOutputPath+"\\report_" + currentDate + ".html";
Files.write(Paths.get(reportPath),reportIn.getBytes(),StandardOpenOption.CREATE);
} catch (Exception e) {
System.out.println("Error when writing report file:\n" + e.toString());
}
}
}
And result class aas follows
package frameWorkUtility;
public class Result {
private String result;
private String resultText;
private String testCaseId;
private String testStepId;
private String testDescription;
public Result(String result,String resultText,String testCaseId,String testStepId,String testDescription) {
this.result = result;
this.resultText = resultText;
this.testCaseId=testCaseId;
this.testStepId=testStepId;
this.testDescription=testDescription;
}
public void setResult(String result) {
this.result = result;
}
public String getResult() {
return this.result;
}
public void setResultText(String resultText) {
this.resultText = resultText;
}
public String getResultText() {
return this.resultText;
}
public void setTestCaseId(String resultText) {
this.testCaseId = testCaseId;
}
public String getTestCaseId() {
return this.testCaseId;
}
public void setTestStepId(String testStepId) {
this.testStepId = testStepId;
}
public String getTestStepId() {
return this.testStepId;
}
public String getTestDescription() {
return this.testDescription;
}
public void setTestDescription(String description) {
this.testDescription = testDescription;
}
}
Two add values in reports we need to call report class present under-reported as
Reporter.report("true", "true", sActionKeyword, testId,testStepId,testDescription);
and finally after all execution call
Reporter.writeResults();
the above method responsible for creating the HTML report
Final reports look like as below
Let me know you inputs on it
Subscribe to:
Posts (Atom)