In this post, I'd like to share some fundamental coding techniques for the following questions. These techniques are useful when working on selenium based QA projects or generally in java based projects. It is pretty handy to play with excel read/write while working in tools, for instance Eclipse but what if the project needs a distribution to lab environments.
Problem statement :
1) How to read excel test file/content data from eclipse project folder structure ?
2) How to bundle and read the excel test data file/content within the executable jar file ?
3) How to read the excel test data file from the location where jar is generated ?
Google search helped me to achieve the question-1, the latter ones are achieved through a java expert folk(thank you for the inputs and explanation). I'd like to keep it open for the community folks who are beginners with selenium frameworks.
Here we go.!
Maven based eclipse project structure:
Common code in all the 3 scenarios above: Problem statement :
1) How to read excel test file/content data from eclipse project folder structure ?
2) How to bundle and read the excel test data file/content within the executable jar file ?
3) How to read the excel test data file from the location where jar is generated ?
Google search helped me to achieve the question-1, the latter ones are achieved through a java expert folk(thank you for the inputs and explanation). I'd like to keep it open for the community folks who are beginners with selenium frameworks.
Here we go.!
Maven based eclipse project structure:
Read the excel data using getResourceAsSteam
publicstatic InputStream getResource(String filename){1) How to read excel test file/content data from eclipse project folder structure ?
InputStream inputStream = ExcelUtils.class.getResourceAsStream(filename);
if(null== inputStream){
//fail("Resource not found: `" + filename + "`");
Log.error("Resource not found: `"+ filename +"`");
}
return inputStream;
}
This is useful to feed the forms with excel test data from the eclipse project.
a) Create a "data" folder in src/main/resources and
add "Accounts_Test_Data_Automation.xlsx" file.
b) Below piece of code does the trick.
fileName ="/data/"+ fileName;
ExcelFile = getResource(fileName);
2) How to bundle and read the excel test data file/content within the executable jar file ?
This piece of code does the trick of bundling excel file into jar. Same as in question-1, look at the setExcelfile method.
//This code ships the excel file into the jar
ExcelFile = getResource("/data/"+fileName);
//Instead of above one case use blow direct project path for the excel file to be bundled
//ExelFile = getResource("/data/Accounts_Test_Data_Automation.xlsx");
pom.xml should be included with the maven-assembly-plugin
<plugin>
<!-- NOTE: We don't need a groupId specification because the group is
org.apache.maven.plugins ...which is assumed by default.
-->
<artifactId>maven-assembly-plugin</artifactId>
<version>3.1.1</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>com.sadakar.selenium.common.BasePage</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
Use below command while building the Maven project
clean compile assembly:single install
It will generated the jar file in .m2 repository. Extract of jar should contain the excel file within the "data" folder. While running the command use DtestEnv environment variable. By passing this variable from the command prompt, the code checks for the logic(above jar code) written to read the excel path.
String testEnv = System.getProperty("testEnv");
C:\Users\sadakar\.m2\repository\SADAKAR_POC\SADAKAR_POC\0.0.1-SNAPSHOT>java -DtestEnv=local -jar SADAKAR_POC-0.0.1-SNAPSHOT-jar-with-dependencies.jar3) How to read the excel test data file from the location where jar is generated ?
Every time when you run scenario(s) or run the jar you CAN NOT edit the excel data file that is bundled inside the jar and it become cumbersome to manually update the excel then bundle it.
This may be tedious job if the test data is huge. To overcome this, it is always a good practice to keep the excel file externally where the jar is going to be generated or where the jar is going be deployed, so I file kept in such location can be accessible with the following snippet in the method.
//This piece of code search for the excel file where the jar is generated or deployed
fileName ="./"+ fileName;
ExcelFile =new FileInputStream(fileName);
Complete picture of these 3 logic(s) are implemented in ExcelUtils.java where the file name is being sent from AddAccount.java with the Sheet name.
To get the Cell data look at readAccountsData method.
ExcelUtils.java
1 | package com.sadakar.excel.utilities; |
AddAccount.java
(This class extends BasePage class that can be removed if you want to use the same code)
1 | package com.sadakar.cucumber.stepdefinitions; |
I hope this helps, someone in community.!
Cheers.!
- Sadakar Pochampalli