Setting up WebDriver with Maven

This is Part 6 of the Selenium WebDriver Tutorial Series. Check out Part 5 to learn how to handle alerts and frames.

In previous posts about Selenium WebDriver, we have set up the WebDriver project as simple Java projects in eclipse. In this tutorial, we will look into setting up webdriver with Maven. You can find the completed project here.

WebDriver With Maven

Setting up WebDriver with Maven

This tutorial assumes that your Eclipse setup with Maven and TestNG plugin. If not, you can check this StackOverFlow answers for testng and maven to set it up.

Create Maven Project

  • Right click on Package Explorer and select New > Maven Project.
  • Check “Create a Simple Project (skip archetype selection)” and click Next. Provide the group id, artifact id, name and click Finish to create the project.
  • The project will get created with the below structureProjectStructure
  • You can change the JRE System Library to an alternate library by Right-clicking on JRE System Library and select the Properties option and selecting any Alternate JRE.AlternateJRESettings
  • Right click on pom.xml file from Package Explorer and select pom.xml tab to see the pom.xml file. You can see the name, group id and artifact id you have given during project setup.
  • This file maintains all the dependencies in it so that when we give the project to someone else, we don’t have to provide the dependent jar files. Maven will automatically download the jars based on the values given in the pom file.
  • We have to add the selenium dependencies to the pom file as given below. You can get the latest value from maven repository and search for ‘selenium-java’ and get the latest version.
SEE ALSO:   WebDriver Tutorial 3 : Mastering WebDriver API
  • In a similar way, add TestNG dependency as well. The pom file would look like this nowWebDriver with Maven
  • Now that Selenium and TestNG dependencies are added, let us start coding.

Creating WebDriver test using TestNG

  • Right click on  src/main/java folder and Right click to select New > TestNG class
  • Give a suitable name for the class and select required methods and testng.xml file as shown below
    WebDriver With Maven
  • You can move the testng.xml file to the root of the project if required. The project structure looks as shown belowWebDriver With Maven
  • For running in chrome, you can add a res folder with the chromedriver.exe file inside it. Refer this tutorial to know more.
  • Now write the tests as shown below
  • Now, right click on testng.xml file and select Run As > Testng suite. The script will get executed and the result will be obtained.
SEE ALSO:   3 Things to Avoid in Test Automation

Executing Tests with Maven

  • For running the scripts with maven, we have to add few plugins to the pom file – maven-compiler-plugin and maven-surefire-plugin as shown below. You can find the final file here.
  • Right click on pom.xml and select Maven > Run As > Maven test to execute the test using Maven.

You can find the completed project in this GitHub repository.

Next Post : Setting up Script using RemoteWebDriver

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *