Apache Maven is a software project management and comprehension tool. It is formally known as Build tool. This article will help you to setup project and integrate selenium with maven.Based on the concept of a project object model (POM), Maven can manage a project’s build, reporting and documentation from a central piece of information that is called
Based on the concept of a project object model (POM), Maven can manage a project’s build, reporting and documentation from a central piece of information that is called Central repository. Maven has its own repository where it keeps all plugin, jars etc. in commonplace in .m2 repository.
Relation with Selenium-
We can create Maven project for writing script and create dependency-using POM.xml once dependency is set Maven will download all the dependent jar files automatically and in future if any update comes from Selenium or TestNG side it will simply update all the required changes.
Integrate selenium with maven
I have already published video on this which will cover integrate selenium with maven
Eclipse should be Juno(4.2) or Kepler(4.3) and If you are using Eclipse mars or neon then Maven comes by default so you can skip initial steps 🙂
Follow below steps for Eclipse Juno and Kepler
- Step 1- Open any browser and Copy below URL
http://download.eclipse.org/technology/m2e/releases
2.Step 2- Open Eclipse and Go to Help section then Click on install new software.
3. Step 3– Now enter the url which we copied from Eclipse site and follow below steps.
4. Step 4– Now select Maven check box and click next and accept agreement and finish.
Step 5– Installation will take some time and it may be asked for restart the eclipse.
After the restart, you can create Maven projects.
Integrate Selenium with maven
Step1- Open Eclipse then go to File then Click on New and select general project
Step 2– Now here you will get so many categories so from General Select Maven then Maven Project.
Step 3– Now click on create a simple project checkbox and click on Next button
Step 4– Please enter Group id (depends on you I have taken selenium.maven) and Artifact ID > Next
Step 5- Click Finish and wait for some time for project creation. Once the project is created you can see the maven project in left project explorer.
Step 6– Click on pom.xml and click on Source tab you will get the source code of pom.xml
Step 7- Now if want to integrate Maven with Selenium or any other tools then we need to add a dependency with Selenium.
So navigate to http://docs.seleniumhq.org/download/maven.jsp
Step 8– Selenium guys has provided maven dependency so we need to copy the below dependency
Step 9– Open pom.xml file and create <dependencies> tag and paste the dependency code inside <dependencies> tag
Note – Each tag should be closed like <dependencies> —-</dependencies>
Step 10- Let’s integrate the TestNG also in Maven project and Repeat the same steps as we did for Selenium
http://testng.org/doc/maven.html
After paste, your pom.xml will look like
Note- Once you will update pom.xml then inside your project you will get maven dependency.
Now we are done with Installation + project creation so let us create some sample scripts and will see how to execute the same
Selenium Scripts with Maven Project
Step 1- Click on project select src>test>java and right click then create a class
Step 2– Give package and class name and finish.
Maven Integration with Selenium
package MavenDemo; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.testng.annotations.Test; public class Testfacebook { @Test public void TestFireFox(){ WebDriver driver=new FirefoxDriver(); driver.manage().window().maximize(); driver.get("http://www.facebook.com"); driver.quit(); } }
Step 3- Right your Java program and Run
Output- you will get the same output, which you will get if you run java project but now we have the central repository where all jars, is placed and if in future some new jars updated it will download automatically.
In next post, we will see how to create maven build and how to execute run the build via maven command prompt and from Eclipse also.
Manikanth says
Could you please add me for mail updates
Mukesh Otwani says
Hi Manikanth,
Please subscribe to my blog and youtube channel
Muthukumar says
Thanks ji, and one more help , can you Please Update the video for Hybrid Framework ?
Mukesh Otwani says
Hi Muthu,
More videos about this topic are in pipeline…:)
Muthu says
Could you please share the video for Windows Pop handles up using with 3rd party tool like autoIT?
Mukesh Otwani says
Hi Muthu,
Please refer this link http://learn-automation.com/handle-windows-authentication-using-selenium-webdriver/
Shanu says
Hi when i am creating maven project i am getting some error in pom.xml
” ” but when i changed this line , error got resolved but i am able to get Jre library file and maven dependency folder inside src folder
Mukesh Otwani says
Hi Shanu,
I am not able to see line which you removed from pom.xml.
Yokesh says
Mukesh, you are doing a great job! so organized & detailed steps and making it damn easy to follow. Pls continue 🙂
Mukesh Otwani says
Hi Yokesh,
Thanks for your positive comments…:)
Soumyaranjan Sahu says
Hi Mukesh,
During creation of Maven project, I am getting “Could not calculate build plan: Plugin”
Mukesh Otwani says
Hi,
You can check proxy settings.
Gaurav Khurana says
It worked but as a TestNg.. How we know it ran as a maven project ?
Mukesh Otwani says
Hi Gaurav,
Please follow below videos https://youtu.be/nasdb9QhFqg
Santhoshkumar Muralikrishnan says
Hi mukesh,
I am not able to create a maven project successfully. Followed the exact steps as in the video. Kindly help me to sort this.
Getting the below error message.
Could not calculate build plan: Plugin org.apache.maven.plugins:maven-resources-plugin:2.6 or one of its dependencies could not be resolved: Failed to read artifact descriptor for org.apache.maven.plugins:maven-resources-plugin:jar:2.6
Plugin org.apache.maven.plugins:maven-resources-plugin:2.6 or one of its dependencies could not be resolved: Failed to read artifact descriptor for org.apache.maven.plugins:maven-resources-plugin:jar:2.6
Mukesh Otwani says
Hi Santhosh,
if you are running behind proxy then only this error comes. Kindly use without proxy.
Santhoshkumar Muralikrishnan says
Hi Mukesh,
I dont used any proxy so far. Don’t know how to fix this. Kindly help me.
Mukesh Otwani says
Have you tried with Eclipse Neon. I guess some issue with your maven plugin then
subhendu malu says
Hi Mukesh,
i am not able install Maven in mac.Also after installation i am not able to find .m2 folder.plz guide me.
Mukesh Otwani says
for MAC open terminal and run brew install maven
Nikhil says
Hi Mukesh,
Tried freshly configuring the Maven project. Following steps were done.
1. Downloaded 3.3.1 maven files.
2. Path is set for MAVEN_HOME and in path variable.
3. Maven Eclipse plugin is installed.
4. When I click new->Project no option for Maven
Just to add Im on company laptop where proxy is enabled. I tried with No proxy it didnt help at all.
Mukesh Otwani says
Then last option download Eclipse Neon it comes by default with Maven
Muthubala says
Hi Mukesh ,
May I know the steps for configure the more site in selenium web driver ?
Mukesh Otwani says
HI Muthubala,
What do you mean by more site? Can you explain in brief?
Sumitra says
Hi Mukesh,
I went through ur videos for reading the excel.
I am facing an issue and tried a lot. Could you please help me?
my pom.xml looks like below:
4.0.0
EaasWebHybrid
EaasWebHybrid
0.0.1-SNAPSHOT
EaasWebHybrid
EaasWebHybrid
org.seleniumhq.selenium
selenium-java
2.53.1
org.testng
testng
6.9.10
org.apache.poi
poi
3.9
org.apache.poi
poi-ooxml
3.9
org.apache.poi
poi-ooxml-schemas
3.9
org.apache.poi
poi-scratchpad
3.9
org.apache.poi
ooxml-schemas
1.1
org.apache.poi
openxml4j
1.0-beta
org.apache.poi
poi-ooxml
3.10-FINAL
org.apache.maven.plugins
maven-surefire-plugin
2.19.1
src\test\resources\RegressionSuite.xml
Still in my code i get cannot instantiate XSSFWorkbook on the right side. (Highlighted part)
XSSFWorkbook wb = new XSSFWorkbook(fis);
Waiting for your reply.
Mukesh Otwani says
Hi you need only two dependency for Apache POI
Prabhakar says
Thanks Mukesh..Your videos and data is simply awesome for selenium learners..Have below queries after gone through maven tutorial…
1.Do we need to add selenium jars,TestNG and other JARS after creating MAVEN Project in selenium to run test scripts in Maven Project..??
2.How can Maven project knows there is latest version of TestNG/Selenium JARS/etc .. released recently ..??
Mukesh Otwani says
Hi Prabhakar,
Maven will take care of adding jars you just have to specify the jars dependency.
If latest version comes then you can change the version of the same and it will take care of updating the jars.
Nikhil says
Mukesh Im getting on POM.xml, but what error it doesn’t show. I created testng class under test folder but when I execute testng.xml it gives error. Im pasting my POM.xml here. Its been 3 days and still struggling.
4.0.0
com.learnautomation
com.learnautomation.selenium
0.0.1-SNAPSHOT
Demo Video Maven Build
Demo Video Maven Build
src/main/resources/testng.xml
org.testng
testng
6.1.1
org.apache.maven.plugins
maven-compiler-plugin
3.5.1
1.8
1.8
1.8
org.apache.maven.plugins
maven-surefire-plugin
2.17
Mukesh Otwani says
Hi Nikhil,
Send me zip file of project I will look into it
Nikhil says
In command prompt, when I type mvn -version. It says bad command. I have installed Maven, provided path in path variable. Also MAVEN_HOME variable is also set
Mukesh Otwani says
Hi Nikhil,
Restart will help in this.
Pragyan says
Hi mukesh, please let me know how to add the dependency. As I am getting an error on
Mukesh Otwani says
just add in pom.xml file
Amit Chaudhary says
Hi Mukesh,
I’m geting below error, when trying to create a maven project as per the steps provided by you –
Error – Could not calculate build: Plugin org.apache.maven.plugins:maven-resource-plugin:2.6 or one of it’s dependencies could not be resolved: Failed to read artifact descriptor for org.apache.maven.plugins:maven-resource-plugin:jar:2.6
Mukesh Otwani says
Hi Amit,
is it fixec?
suvankar says
hi,
mukesh you are realy super star
Mukesh Otwani says
Hi Suvankar,
Your comment made my day. Thank you 🙂
Sooraj Hat says
Hi Mukesh, i keep getting “java.net.SocketException: Broken pipe” error message for a simple test case on Maven even though test case passes. do u know how to overcome this?
Mukesh Otwani says
Hi Sooraj,
I did not get any kind of issue till now. Can you tell me the steps to reproduce it?
Manjunath says
Hi,
In the project structure four sections are there test/java, test/resources and main/java, main/resources. Can you please explain the four sections what to write in those sections.
I guess in test/java section we are writing testcases and remaining can you please explain.
Mukesh Otwani says
main folder used by developer and test/resource folder can be used if you want to keep any file like chromedriver,iedriver etc.
Hope its clear now 🙂
Raja says
Hi dude,
After installing the maven plug-in, I have created a java project. But here I am getting ony Src, target and pom.xml files only. I am not getting remain all folders (test,java..etc). Did I made any mistake while installing. Can you please help me dude.
Mukesh Otwani says
Hi Raja,
Please update the Eclipse version to Eclipse Luna
Thanks
Mukesh
hanumanthappa says
Hi Mukesh,
I followed your pom.xml file in my project and mentioned xslt report dependecy also but unable to generate the reports.generating the normal report not giving the erro.
Please find the my pom.xml file
4.0.0
com.learnautomation
com.learnautomation.sel
0.0.1-SNAPSHOT
mavensample
mavensaml
jar
org.testng
testng
6.3.1
org.seleniumhq.selenium
selenium-java
2.47.1
io.appium
java-client
3.2.0
net.sf.saxon
saxon
8.7
src
org.apache.maven.plugins
maven-surefire-plugin
2.14.1
testng.xml
maven-compiler-plugin
iso-8859-1
1.7
1.7
org.reportyng
reporty-ng
1.2
<!– /target/testng-xslt-report- –>
true
FAIL,SKIP,PASS,CONF,BY_CLASS
true
reporty-ng
https://github.com/cosminaru/reporty-ng/raw/master/dist/maven
Mukesh Otwani says
Thanks try with Extent report https://www.youtube.com/watch?v=dBj7pxhZXhY