How To Create Selenium Maven Project In Eclipse IDE | Selenium Tutorial
Create Selenium Maven Project In Eclipse IDE:
In the earlier post, we have see “Installation of Maven in Eclipse“. In this post, we will see How To Create Selenium Maven Project in Eclipse IDE.
Make sure TestNG is already installed. Check this out to see “How To Install TestNG In Eclipse” and this check this link for “TestNG Complete Tutorial”
To create a project – Right click on Package Explorer and navigate through New – Other
Select Maven Project and click on Next
Select ‘Create a simple project‘ check box and click NextFill the Group Id, Artifact Id and click the Finish button
Structure of the project looks as shown in the below image
In the maven project, we use POM file to add the required dependencies. Double click on pom.xml file and click on pom.xml tab
We need to add some dependencies related to Selenium WebDriver, TestNG in our Maven Project.
<dependencies> <!-- Selenium --> <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> <version>2.53.1</version> </dependency> <!-- TestNG --> <dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>6.8</version> <scope>test</scope> </dependency> </dependencies>
Create a TestNG class and generate testng.xml file. Check the below link to do this step.
How to create a TestNG class and generate testng.xml file
Copy the below code and place it in your NewTest class
package tests; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.testng.annotations.Test; import org.testng.annotations.BeforeClass; import org.testng.annotations.AfterClass; public class NewTest { public WebDriver driver; @Test public void openMyBlog() { driver.get("https://www.softwaretestingmaterial.com/"); } @BeforeClass public void beforeClass() { System.setProperty("webdriver.gecko.driver", "D:\\Selenium\\Drivers\\geckodriver.exe"); driver = new FirefoxDriver(); } @AfterClass public void afterClass() { driver.quit(); } }
testng.xml: testng.xml file looks like below. Copy and place it in your testng.xml
<?xml version="1.0" encoding="UTF-8"?> <suite name="Suite" parallel="false"> <test name="Test"> <classes> <class name="tests.NewTest"/> </classes> </test> <!-- Test --> </suite> <!-- Suite -->
To run the project using testng.xml, right click on ‘testng.xml’ file and go to Run As – TestNG Suite
To run the project using pom.xml, we need to add some more entries in the pom.xml file. We need to add ‘maven-compiler-plugin’ and ‘maven-surefire-plugin’
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>mavenPackage</groupId> <artifactId>MavenProject</artifactId> <version>0.0.1-SNAPSHOT</version> <dependencies> <!-- Selenium --> <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> <version>2.53.1</version> </dependency> <!-- TestNG --> <dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>6.8</version> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.6.1</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.20</version> <configuration> <suiteXmlFiles> <suiteXmlFile>testng.xml</suiteXmlFile> <!-- <suiteXmlFile>src/main/resources/testng.xml</suiteXmlFile> --> </suiteXmlFiles> </configuration> </plugin> </plugins> </build> </project>
To run the project using pom.xml, right click on ‘pom.xml‘ file and go to ‘Run As‘ – ‘Maven test‘
Result:
[INFO] ------------------------------------------------------- [INFO] T E S T S [INFO] ------------------------------------------------------- [INFO] Running TestSuite [INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 13.332 s - in TestSuite [INFO] [INFO] Results: [INFO] [INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0 [INFO] [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------
This way we create Selenium Maven Project in Eclipse. If you have any queries, please mention in the comments section below.
Hi,
I created Maven project with TestNg. When I am trying to run it getting below error:
“java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory”
I added Selenium and TestNG dependencies in pom.xml
Please look into it and help me with this.
Thanks in advance!!!
Hi Vipul Shah, try to add dependency ‘commons-logging’ in your pom.xml.
I added “commons-logging” dependency in pom.xml. I am getting authorization error.
Hi Vipul, comment it and see.