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 NewOther

Select Maven Project and click on Next

Select ‘Create a simple project‘ check box and click Next

Fill 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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<dependencies>
  <! Selenium >
  <dependency>
     <groupId>org.seleniumhq.selenium</groupId>
     <artifactId>seleniumjava</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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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

1
2
3
4
5
6
7
8
<?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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<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.1SNAPSHOT</version>
  
  <dependencies>
 
<! Selenium >
<dependency>
  <groupId>org.seleniumhq.selenium</groupId>
  <artifactId>seleniumjava</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>mavencompilerplugin</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>mavensurefireplugin</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:

1
2
3
4
5
6
7
8
9
10
11
12
13
[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.

© 2021 bewegingstechnologie.net