SlideShare a Scribd company logo
1 of 14
Download to read offline
Integrating Maven with Eclipse and using it with Selenium
By ā€“Nikhil Bharati
This is for the people who havenā€™t used maven, or the people who are using it but donā€™t know much
about it and want to implement it in their project from scratch. We all know about advantages of Java
and thatā€™s why we are using it. Being an open source and platform independent Java is ruling in the
world of tech. When it comes to testing, it plays a vital role too. In the recent days we have seen huge
demand of Selenium and other open source tools over QTP/UFT, Test Complete, Ranorex and other
licensed tool. This shows the demand of open source tool and the days are not far when commercial
tools will become the past.
In this article Iā€™ll show you how easily we can implement maven with selenium and eclipse. To start with
Maven is a build tool which is used to define project structure, dependencies, build, and test
management. For working with Java you need to use several APIā€™s in order to do your work easier. For
us Maven takes care of all of it, we just need to write our code and execute them using Maven.
What you should know?
To have a clear understanding you should have eclipse installed in your system, you should have basic
knowledge of selenium using Java. Even if you donā€™t know, itā€™s not rocket science, itā€™s pretty simple, just
follow the steps and you should be good.
Why Maven?
In a traditional way, we need to add the external jars every time when we need to use them. In real time
projects, as the time progress it may become difficult to maintain them, they might look clumsy as well.
This can work if you are working alone but if you are working as a team you wonā€™t know who is using
what Jar and what the version of that jar is. Letā€™s say you send an email to update the team every time
you update your jar files or add a new jar. Donā€™t you think it will still be a new task for everyone to do it
and if someone misses then the code will break in their system? Wouldnā€™t it be nice if we share a file
and ask everyone to replace it with their old one? Sounds great right. This is what I like most about the
Maven. Just one XML and the whole team is good to go.
To cut it short:
1) Using Maven, we can define dependencies, project structures, build and Test Management.
2) Using pom.xml, the dependencies are configured which are needed during the run time which
downloads the API and keep local copy of them in the maven repository.
Nowā€™s letā€™s go through how we can install Maven and use it in our project.
Steps to install Maven and create a Project using Eclipse in Selenium
Iā€™m using Eclipse Java Mars and will show how we can download it using Eclipse. Same process can be
followed for any Eclipse version. For using Maven, m2eclipse eclipse plugin will be required and should
be added to eclipse. This will help to facilitate the build process and create pom.xml file. Follow the
following steps to add m2eclipse plugin.
1) In Eclipse IDE, select Help | Install New Software from Eclipse Main Menu.
2) Fill the Work with edit box with http://download.eclipse.org/technology/m2e/releases/
3) Select the Maven Integration for Eclipse checkbox and click on Next button to finish installation.
Maven installation is now completed and can start a new project. Before that how should we
check whether the Maven is installed or not?
How to check the Maven installation?
Follow the following steps to check the maven installation.
1) Open Eclipse and click on Windows -> Preferences.
2) Choose Maven from left panel, and select installations.
3) Click on Maven -> "User Settings" option form left panel, to check local repository location.
Now, we are all set up. Letā€™s create a new Maven project and start working on it.
Steps for creating Maven project and executing it
1) Open Eclipse IDE, Go to File ļƒ  New ļƒ  Other ļƒ  Maven ļƒ  Select Maven Project and click Next >
2) Select your workspace and click Next .
3) Select the Archetype based on your requirement, Maven provides varieties of Archetype, for my
requirement Iā€™ll select ā€œquickstartā€ and click Next .
4) Provide the group id, artifact id and package name as you want and click on Finish. Itā€™s good to
give the same name to all three to avoid any confusion.
5) Eclipse will create the maven project named ā€œSeleniumMavenā€ as given below.
The M symbol at the top of Folder icon represents a Maven project.
6) Right-click on JRE System Library and select the Properties option from the menu.
Select Workspace default JRE and click on OK.
7) Now double click on pom.xml file in the project explorer. The file shown below should open by
default. Make sure you are on the pom.xml tab.
8) Add all the dependencies you would need in your project. You would be probably thinking how I
would know the group id, artifact id and version for the dependencies. Donā€™t get afraid, this is
quite simple. Google the API you are looking for followed by maven dependency. Ex. poi 3.15
maven dependency, Click on the first link that comes in search and copy.
You should get the dependency like below:
Copy this and paste it in your pom.xml as shown
The project explorer looks like this.
In order to execute anything using Maven make sure you have these 3 dependencies in your pom.xml
ļ‚· maven-compiler-plugin
ļ‚· maven-surefire-plugin
ļ‚· testng.xml
9) Now select your project from the project explorer, do right click ļƒ  Maven ļƒ  Update Project.
This will update your project with the all the required dependencies, download them and keep
them in the local repository. The project explorer will look like as below.
All the poi dependencies were downloaded. You can also check the local copy in the location
ā€œC:Userschq-nikhilb.m2repositoryā€
Inside the poi folder you will find the jar file which was mentioned in pom.xml
All the configuration has been done, now letā€™s create a class and run it using Maven. We will
create a testng class and then convert the project to testng project in order to generate testng.xml. Letā€™s
move to the next step and start writing code.
10) Select your Maven project ļƒ src/test/javaļƒ right click on SeleniumMaven package ļƒ New
ļƒ Otherļƒ Select TestNG Class and click Next ļƒ  Provide the class name and click on Finish.
I have named my class as ā€œReadXlTestā€. In this class Iā€™ll use apache poi to open an excel file, read
its contents and display it on the console.
11) In this step, Iā€™ll convert this project to Testng inorder to generate the testng.xml, which will help
us to define the annotations in selenium and using it we will execute our tests.
For doing this, select your project ļƒ  Right Click ļƒ  TestNG ļƒ  Convert to TestNG ļƒ  Finish. Now
a basic xml file will be generated below your pom.xml in the project explorer named as
testng.xml.
12) Go to ReadXlTest.java and copy paste this code,
package SeleniumMaven;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.testng.annotations.Test;
public class ReadXlTest {
@Test
public void readWrite() throws IOException {
String strFilepath = "C:WorkspaceUpload";
String strFile = "ticTactoe.xlsx";
File file = new File(strFilepath+strFile);
FileInputStream inputstream = new FileInputStream(file);
String strFileExtension = strFile.substring(strFile.indexOf("."));
Workbook xlWorkbook = null;
if (strFileExtension.equals(".xlsx")){
xlWorkbook =new XSSFWorkbook(inputstream);
}
else if (strFileExtension.equals(".xls")){
xlWorkbook = new HSSFWorkbook(inputstream);
}
Sheet xlSheet = xlWorkbook.getSheet("AdjTypeByJuris");
int iRowCount = xlSheet.getLastRowNum()-xlSheet.getFirstRowNum();
System.out.println(iRowCount);
System.out.println("Rowcount is : "+ iRowCount);
for(int i = 0; i<iRowCount+1;i++){
Row row = xlSheet.getRow(i);
for (int j=0;j<row.getLastCellNum();j++){
String strData = row.getCell(j).toString();
System.out.println(strData + " # # ");
}
}
}
}
In this class I am using poi api to read the excel files which Iā€™m getting local .m2 repository
downloaded using pom.xml.
Save your class. Go to pom.xml and verify if you the dependencies for maven-compiler-plugin,
maven-surefire-plugin and testng. Without these dependencies, maven wonā€™t work.
The maven-compiler-plugin is used to help in compiling the code and using the particular JDK
version for compilation.
The maven-surefire-plugin is used to configure and execute tests. Here plugin is used to
configure the testing.xml for TestNG test and generate test reports.
Now we are all set to go, letā€™s run the test and see.
13) Select your project, perform right click ļƒ  Run as ļƒ Maven Test . In the console you can see the
build successful message and the required test will execute.
I hope this was useful. Feel free to reach me @ bharati.nikhil@gmail.com for any issues, Iā€™ll be
happy to assist. See you soon again, till then happy coding!!

More Related Content

What's hot

Apache maven, a software project management tool
Apache maven, a software project management toolApache maven, a software project management tool
Apache maven, a software project management tool
Renato Primavera
Ā 
Selenium Tutorial
Selenium TutorialSelenium Tutorial
Selenium Tutorial
prad_123
Ā 

What's hot (18)

Top trending selenium interview questions
Top trending selenium interview questionsTop trending selenium interview questions
Top trending selenium interview questions
Ā 
Apache maven, a software project management tool
Apache maven, a software project management toolApache maven, a software project management tool
Apache maven, a software project management tool
Ā 
Selenium Training in Chennai
Selenium Training in ChennaiSelenium Training in Chennai
Selenium Training in Chennai
Ā 
BDD using Cucumber JVM
BDD using Cucumber JVMBDD using Cucumber JVM
BDD using Cucumber JVM
Ā 
Maven: Managing Software Projects for Repeatable Results
Maven: Managing Software Projects for Repeatable ResultsMaven: Managing Software Projects for Repeatable Results
Maven: Managing Software Projects for Repeatable Results
Ā 
Maven basics
Maven basicsMaven basics
Maven basics
Ā 
Selenium Automation Testing Interview Questions And Answers
Selenium Automation Testing Interview Questions And AnswersSelenium Automation Testing Interview Questions And Answers
Selenium Automation Testing Interview Questions And Answers
Ā 
Selenium Training | TestNG Framework For Selenium | Selenium Tutorial For Beg...
Selenium Training | TestNG Framework For Selenium | Selenium Tutorial For Beg...Selenium Training | TestNG Framework For Selenium | Selenium Tutorial For Beg...
Selenium Training | TestNG Framework For Selenium | Selenium Tutorial For Beg...
Ā 
OSDC 2017 - Julien Pivotto - Automating Jenkins
OSDC 2017 - Julien Pivotto - Automating JenkinsOSDC 2017 - Julien Pivotto - Automating Jenkins
OSDC 2017 - Julien Pivotto - Automating Jenkins
Ā 
Selenium Handbook
Selenium HandbookSelenium Handbook
Selenium Handbook
Ā 
Selenium webdriver interview questions and answers
Selenium webdriver interview questions and answersSelenium webdriver interview questions and answers
Selenium webdriver interview questions and answers
Ā 
Selenium Tutorial
Selenium TutorialSelenium Tutorial
Selenium Tutorial
Ā 
San Jose Selenium Meet-up PushToTest TestMaker Presentation
San Jose Selenium Meet-up PushToTest TestMaker PresentationSan Jose Selenium Meet-up PushToTest TestMaker Presentation
San Jose Selenium Meet-up PushToTest TestMaker Presentation
Ā 
Android Meetup Slovenija #3 - Testing with Robolectric by Ivan Kust
Android Meetup Slovenija #3 - Testing with Robolectric by Ivan KustAndroid Meetup Slovenija #3 - Testing with Robolectric by Ivan Kust
Android Meetup Slovenija #3 - Testing with Robolectric by Ivan Kust
Ā 
Top 25 Selenium Interview Questions and Answers 2018
Top 25 Selenium Interview Questions and Answers 2018Top 25 Selenium Interview Questions and Answers 2018
Top 25 Selenium Interview Questions and Answers 2018
Ā 
Automation Testing by Selenium Web Driver
Automation Testing by Selenium Web DriverAutomation Testing by Selenium Web Driver
Automation Testing by Selenium Web Driver
Ā 
Eclipse IDE, 2019.09, Java Development
Eclipse IDE, 2019.09, Java Development Eclipse IDE, 2019.09, Java Development
Eclipse IDE, 2019.09, Java Development
Ā 
Selenium notes
Selenium notesSelenium notes
Selenium notes
Ā 

Similar to Integrating Maven with Eclipse

Maven TestNg frame work (1) (1)
Maven TestNg frame work (1) (1)Maven TestNg frame work (1) (1)
Maven TestNg frame work (1) (1)
QA Programmer
Ā 
Maven TestNg frame work (1) (1)
Maven TestNg frame work (1) (1)Maven TestNg frame work (1) (1)
Maven TestNg frame work (1) (1)
Gopi Raghavendra
Ā 
Selenium Testing
Selenium Testing Selenium Testing
Selenium Testing
Shreshtt Bhatt
Ā 
Steps to write Selenium
Steps to write Selenium  Steps to write Selenium
Steps to write Selenium
Rohit Thakur
Ā 
How java works
How java worksHow java works
How java works
thiruvenkatz
Ā 

Similar to Integrating Maven with Eclipse (20)

Testing Java Web Apps With Selenium
Testing Java Web Apps With SeleniumTesting Java Web Apps With Selenium
Testing Java Web Apps With Selenium
Ā 
A-Z_Maven.pdf
A-Z_Maven.pdfA-Z_Maven.pdf
A-Z_Maven.pdf
Ā 
Maven 2 features
Maven 2 featuresMaven 2 features
Maven 2 features
Ā 
Maven Introduction
Maven IntroductionMaven Introduction
Maven Introduction
Ā 
Maven
MavenMaven
Maven
Ā 
Exploring Maven SVN GIT
Exploring Maven SVN GITExploring Maven SVN GIT
Exploring Maven SVN GIT
Ā 
Selenium Training in Chennai Demo Part-2
Selenium Training in Chennai Demo Part-2 Selenium Training in Chennai Demo Part-2
Selenium Training in Chennai Demo Part-2
Ā 
Class 1 blog
Class 1 blogClass 1 blog
Class 1 blog
Ā 
Maven TestNg frame work (1) (1)
Maven TestNg frame work (1) (1)Maven TestNg frame work (1) (1)
Maven TestNg frame work (1) (1)
Ā 
Step by step - Selenium 3 web-driver - From Scratch
Step by step - Selenium 3 web-driver - From Scratch  Step by step - Selenium 3 web-driver - From Scratch
Step by step - Selenium 3 web-driver - From Scratch
Ā 
Maven TestNg frame work (1) (1)
Maven TestNg frame work (1) (1)Maven TestNg frame work (1) (1)
Maven TestNg frame work (1) (1)
Ā 
Selenium Testing
Selenium Testing Selenium Testing
Selenium Testing
Ā 
Steps to write Selenium
Steps to write Selenium  Steps to write Selenium
Steps to write Selenium
Ā 
Selenium
SeleniumSelenium
Selenium
Ā 
Selenium and JMeter Testing
Selenium and JMeter TestingSelenium and JMeter Testing
Selenium and JMeter Testing
Ā 
Selenium and JMeter
Selenium and JMeterSelenium and JMeter
Selenium and JMeter
Ā 
Sel
SelSel
Sel
Ā 
Java, Eclipse, Maven & JSF tutorial
Java, Eclipse, Maven & JSF tutorialJava, Eclipse, Maven & JSF tutorial
Java, Eclipse, Maven & JSF tutorial
Ā 
How java works
How java worksHow java works
How java works
Ā 
How java works
How java worksHow java works
How java works
Ā 

Recently uploaded

Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
Ā 

Recently uploaded (20)

Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Ā 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
Ā 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
Ā 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
Ā 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
Ā 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Ā 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
Ā 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Ā 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
Ā 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Ā 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
Ā 
šŸ¬ The future of MySQL is Postgres šŸ˜
šŸ¬  The future of MySQL is Postgres   šŸ˜šŸ¬  The future of MySQL is Postgres   šŸ˜
šŸ¬ The future of MySQL is Postgres šŸ˜
Ā 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Ā 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
Ā 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
Ā 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
Ā 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Ā 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
Ā 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
Ā 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
Ā 

Integrating Maven with Eclipse

  • 1. Integrating Maven with Eclipse and using it with Selenium By ā€“Nikhil Bharati This is for the people who havenā€™t used maven, or the people who are using it but donā€™t know much about it and want to implement it in their project from scratch. We all know about advantages of Java and thatā€™s why we are using it. Being an open source and platform independent Java is ruling in the world of tech. When it comes to testing, it plays a vital role too. In the recent days we have seen huge demand of Selenium and other open source tools over QTP/UFT, Test Complete, Ranorex and other licensed tool. This shows the demand of open source tool and the days are not far when commercial tools will become the past. In this article Iā€™ll show you how easily we can implement maven with selenium and eclipse. To start with Maven is a build tool which is used to define project structure, dependencies, build, and test management. For working with Java you need to use several APIā€™s in order to do your work easier. For us Maven takes care of all of it, we just need to write our code and execute them using Maven. What you should know? To have a clear understanding you should have eclipse installed in your system, you should have basic knowledge of selenium using Java. Even if you donā€™t know, itā€™s not rocket science, itā€™s pretty simple, just follow the steps and you should be good. Why Maven? In a traditional way, we need to add the external jars every time when we need to use them. In real time projects, as the time progress it may become difficult to maintain them, they might look clumsy as well. This can work if you are working alone but if you are working as a team you wonā€™t know who is using what Jar and what the version of that jar is. Letā€™s say you send an email to update the team every time you update your jar files or add a new jar. Donā€™t you think it will still be a new task for everyone to do it and if someone misses then the code will break in their system? Wouldnā€™t it be nice if we share a file and ask everyone to replace it with their old one? Sounds great right. This is what I like most about the Maven. Just one XML and the whole team is good to go. To cut it short: 1) Using Maven, we can define dependencies, project structures, build and Test Management. 2) Using pom.xml, the dependencies are configured which are needed during the run time which downloads the API and keep local copy of them in the maven repository. Nowā€™s letā€™s go through how we can install Maven and use it in our project.
  • 2. Steps to install Maven and create a Project using Eclipse in Selenium Iā€™m using Eclipse Java Mars and will show how we can download it using Eclipse. Same process can be followed for any Eclipse version. For using Maven, m2eclipse eclipse plugin will be required and should be added to eclipse. This will help to facilitate the build process and create pom.xml file. Follow the following steps to add m2eclipse plugin. 1) In Eclipse IDE, select Help | Install New Software from Eclipse Main Menu. 2) Fill the Work with edit box with http://download.eclipse.org/technology/m2e/releases/ 3) Select the Maven Integration for Eclipse checkbox and click on Next button to finish installation. Maven installation is now completed and can start a new project. Before that how should we check whether the Maven is installed or not? How to check the Maven installation? Follow the following steps to check the maven installation. 1) Open Eclipse and click on Windows -> Preferences.
  • 3. 2) Choose Maven from left panel, and select installations. 3) Click on Maven -> "User Settings" option form left panel, to check local repository location. Now, we are all set up. Letā€™s create a new Maven project and start working on it.
  • 4. Steps for creating Maven project and executing it 1) Open Eclipse IDE, Go to File ļƒ  New ļƒ  Other ļƒ  Maven ļƒ  Select Maven Project and click Next > 2) Select your workspace and click Next . 3) Select the Archetype based on your requirement, Maven provides varieties of Archetype, for my requirement Iā€™ll select ā€œquickstartā€ and click Next .
  • 5. 4) Provide the group id, artifact id and package name as you want and click on Finish. Itā€™s good to give the same name to all three to avoid any confusion. 5) Eclipse will create the maven project named ā€œSeleniumMavenā€ as given below.
  • 6. The M symbol at the top of Folder icon represents a Maven project. 6) Right-click on JRE System Library and select the Properties option from the menu. Select Workspace default JRE and click on OK. 7) Now double click on pom.xml file in the project explorer. The file shown below should open by default. Make sure you are on the pom.xml tab.
  • 7. 8) Add all the dependencies you would need in your project. You would be probably thinking how I would know the group id, artifact id and version for the dependencies. Donā€™t get afraid, this is quite simple. Google the API you are looking for followed by maven dependency. Ex. poi 3.15 maven dependency, Click on the first link that comes in search and copy.
  • 8. You should get the dependency like below: Copy this and paste it in your pom.xml as shown The project explorer looks like this. In order to execute anything using Maven make sure you have these 3 dependencies in your pom.xml
  • 9. ļ‚· maven-compiler-plugin ļ‚· maven-surefire-plugin ļ‚· testng.xml 9) Now select your project from the project explorer, do right click ļƒ  Maven ļƒ  Update Project. This will update your project with the all the required dependencies, download them and keep them in the local repository. The project explorer will look like as below. All the poi dependencies were downloaded. You can also check the local copy in the location ā€œC:Userschq-nikhilb.m2repositoryā€
  • 10. Inside the poi folder you will find the jar file which was mentioned in pom.xml All the configuration has been done, now letā€™s create a class and run it using Maven. We will create a testng class and then convert the project to testng project in order to generate testng.xml. Letā€™s move to the next step and start writing code. 10) Select your Maven project ļƒ src/test/javaļƒ right click on SeleniumMaven package ļƒ New ļƒ Otherļƒ Select TestNG Class and click Next ļƒ  Provide the class name and click on Finish. I have named my class as ā€œReadXlTestā€. In this class Iā€™ll use apache poi to open an excel file, read its contents and display it on the console.
  • 11. 11) In this step, Iā€™ll convert this project to Testng inorder to generate the testng.xml, which will help us to define the annotations in selenium and using it we will execute our tests. For doing this, select your project ļƒ  Right Click ļƒ  TestNG ļƒ  Convert to TestNG ļƒ  Finish. Now a basic xml file will be generated below your pom.xml in the project explorer named as testng.xml.
  • 12. 12) Go to ReadXlTest.java and copy paste this code, package SeleniumMaven; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.testng.annotations.Test; public class ReadXlTest { @Test public void readWrite() throws IOException { String strFilepath = "C:WorkspaceUpload"; String strFile = "ticTactoe.xlsx"; File file = new File(strFilepath+strFile); FileInputStream inputstream = new FileInputStream(file); String strFileExtension = strFile.substring(strFile.indexOf(".")); Workbook xlWorkbook = null; if (strFileExtension.equals(".xlsx")){ xlWorkbook =new XSSFWorkbook(inputstream); } else if (strFileExtension.equals(".xls")){ xlWorkbook = new HSSFWorkbook(inputstream); } Sheet xlSheet = xlWorkbook.getSheet("AdjTypeByJuris"); int iRowCount = xlSheet.getLastRowNum()-xlSheet.getFirstRowNum(); System.out.println(iRowCount); System.out.println("Rowcount is : "+ iRowCount); for(int i = 0; i<iRowCount+1;i++){ Row row = xlSheet.getRow(i); for (int j=0;j<row.getLastCellNum();j++){ String strData = row.getCell(j).toString(); System.out.println(strData + " # # "); } } } }
  • 13. In this class I am using poi api to read the excel files which Iā€™m getting local .m2 repository downloaded using pom.xml. Save your class. Go to pom.xml and verify if you the dependencies for maven-compiler-plugin, maven-surefire-plugin and testng. Without these dependencies, maven wonā€™t work. The maven-compiler-plugin is used to help in compiling the code and using the particular JDK version for compilation. The maven-surefire-plugin is used to configure and execute tests. Here plugin is used to configure the testing.xml for TestNG test and generate test reports. Now we are all set to go, letā€™s run the test and see. 13) Select your project, perform right click ļƒ  Run as ļƒ Maven Test . In the console you can see the build successful message and the required test will execute.
  • 14. I hope this was useful. Feel free to reach me @ bharati.nikhil@gmail.com for any issues, Iā€™ll be happy to assist. See you soon again, till then happy coding!!