In our earlier posts Maven in eclipse – part1 and Maven in eclipse – part2, we have seen how to setup and use maven in eclipse.
in this article, we will look into how to run selenium tests using maven build in eclipse.
NOTE: we need not to download any of the jar file needed to run selenium tests, maven POM.xml will take care of that, so when we move our selenium tests to any of the machines, we can run tests irrespective of the version of jar files or downloaded jar files.
- Create selenium test as testNG class – InitialTest.java under src/test/java
public class InitialTest { @Test public static void setup() { System.out.println( "Start Test Case - Launch Google" ); WebDriver driver = new FirefoxDriver(); driver.get("http://google.com"); driver.quit(); } }
- testng.xml – place under project folder
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"> <suite name="Suite"> <test name="Test"> <classes> <class name="com.qavalidation.maven.InitialTest"/> </classes> </test> <!-- Test --> </suite> <!-- Suite -->
- pom.xml – place under project folder
<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>com.qavalidation.maven</groupId> <artifactId>MyMavenProj</artifactId> <version>1-SNAPSHOT</version> <packaging>jar</packaging> <name>Maven build</name> <description>Run Sel tests using maven</description> <properties> <suiteXmlFile>testng.xml</suiteXmlFile> <skipTests>false</skipTests> </properties> <dependencies> <dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>6.9.10</version> </dependency> <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> <version>2.53.1</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.14</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.0</version> <configuration> <compilerVersion>1.8</compilerVersion> <source>1.6</source> <target>1.6</target> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.17</version> <configuration> <suiteXmlFiles> <suiteXmlFile>${suiteXmlFile}</suiteXmlFile> </suiteXmlFiles> </configuration> </plugin> </plugins> </build> </project>
Under dependency tags, you can change the version of the jar files that you want to download, respective jar files will be downloaded under local repository before running the maven test.
now, right click on pom.xml -> Run As -> Maven test to run test.