Maven is the latest build testing tool. It has several new features as compare to Ant, like dependency, etc.
Maven is a project build or project management tool. It is used to check the compilation issues between framework components whenever multiple test engineer integrates their files into the same framework.
It always maintained the monitor, framework components, or build, and it provides build status modification, whenever modification happens in the framework.
It provides 'build success' message if no compilation issues in the framework or else provide 'build failure' message.
Maven has new features like dependency, which is used to download the dependency jar from the internet before the test execution.
With the help of Maven, we can execute the test scripts in the command line without an eclipse. And it always provides the framework folder structure.
For more information about Maven, refers to the below link:
https://www.rookienerd.com/maven-tutorial
There are two software available in Maven:
It is a default plug-in for the latest Eclipse versions like Mars, Luna, oxygen, which is used to create a Maven project through Eclipse.
Installing Maven plug-in for Eclipse and use it with Selenium TestNG
Most of the time, Maven plug-in is automatically installed in the Eclipse, but if it is not present, we will go to the Eclipse Market Place and search for Maven and download the M2E integrated version from there.
Steps to create Maven project
To create a Maven project, follow the below steps:
It is used to execute the Selenium test script in the command prompt without an Eclipse, and this software should be installed explicitly.
To install the Maven command line plug-in, follow the below steps:
Step1: Download Apache Maven
Step2: Add M2_Home in the System Variable
Step3: Add %M2_Home%\bin to the path
Step4: Verify
mvn- version
Before we start writing a Maven code, we need to add the general dependencies like TestNG and Selenium in the pom.xml file.
So for this, we will follow the below process:
<dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>6.8</version> <scope>compile</scope> </dependency>
Then, we will add the Maven dependency for Selenium, so for this, we will follow the same process as before:
<dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>6.8</version> <scope>compile</scope> </dependency> <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> <version>3.141.59</version> </dependency> </dependencies>
Note
getTitle(): This method is used to capture the title of the page.
getCurrentURL(): It is used to capture the current URL of the page.
For executing the above test script, we will create one package in the MavenProject.
To create a class in MavenProject, follow the below process:
testpackage → New → Class
After creating the package and class, we will start writing the code.
According to our code requirement, we will be adding multiple dependencies.
After adding the dependencies, our pom.xml file look like this:
<dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>6.8</version> <scope>compile</scope> </dependency> <dependency> <groupId>com.google.inject</groupId> <artifactId>guice</artifactId> <version>4.1.0</version> <classifier>no_aop</classifier> </dependency> <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> <version>3.141.59</version> </dependency> <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-chrome-driver</artifactId> <version>2.50.0</version> </dependency> <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>22.0</version> </dependency> <dependency> <groupId>com.google.api-client</groupId> <artifactId>google-api-client-appengine</artifactId> <version>1.23.0</version> <exclusions> <exclusion> <groupId>com.google.guava</groupId> <artifactId>guava-jdk5</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.2.4</version> </dependency> </dependencies>
And, here the sample code:
package testpackage; import java.util.concurrent.TimeUnit; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.testng.annotations.AfterTest; import org.testng.annotations.BeforeTest; import org.testng.annotations.Test; public class MavenTest1 { public String baseUrl = "https://www.rookienerd.com/"; String driverPath = "C://chromedriver_win321//chromedriver.exe"; public WebDriver driver ; @Test public void test() { // set the system property for Chrome driver System.setProperty("webdriver.chrome.driver", driverPath); // Create driver object for CHROME browser driver = new ChromeDriver(); driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS); driver.manage().window().maximize(); driver.get(baseUrl); // get the current URL of the page String URL= driver.getCurrentUrl(); System.out.print(URL); //get the title of the page String title = driver.getTitle(); System.out.println(title); } @BeforeTest public void beforeTest() { System.out.println("before test"); } @AfterTest public void afterTest() { driver.quit(); System.out.println("after test"); } }
We can run above code in multiple ways:
If we run the above code with the TestNG, we will follow the below process:
To run the same code through Maven, follow the below steps:
First, we need to convert the MavenTest1.java file into the TestNG File, for this follow the below process:
After that, we will run the testng.xml file, so for this, we need to add the Maven Plugins in the pom.xml files.
So, we will add the three different plugins, which are as follows:
Note:
The Maven compiler plugin is used to compile the source code of a Maven project. Maven test command will connect to the internet and download all the dependency jar into the .M2 folder local repository and then compile the entire selenium source code as we can see in the below image:
The Maven surefire plugin is used when we have to run the unit tests of the application.
The Maven source plugin is used to build the jars files that were having the .java source files.
After adding all the plugins, our pom.xml look like this:
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>1.6</source> <target>1.6</target> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.19.1</version> <configuration> <suiteXmlFiles> <suiteXmlFile>C:\Users\JTP\eclipse-workspace\MavenProject\testng.xml</suiteXmlFile> </suiteXmlFiles> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-source-plugin</artifactId> <executions> <execution> <id>attach-sources</id> <goals> <goal>jar</goal> </goals> </execution> </executions> </plugin> </plugins> </build>
If we are using some remote machine with the help of Maven commands, then we need to go to the command prompt.
For this, we will go to that particular directory of the Maven project, so for that right-click on the MavenProject and select the Properties options from the given pop-up menu.
cd eclipse-workspace\MavenProject
Once we are in the MavenProject, we will use some of the common commands of Maven, which are as follows:
Maven commands | Description |
---|---|
mvn clean install | This command is used to generate, compile, and execute the jars files. |
mvn test | We will use this command when we have to execute the tests against the compiled source code with the help of an appropriate unit testing framework. |
mvn compile | It is used to compile the source code of the Maven project. |
mvn package | It will pack the executed code in a different format like Jar. |
Example 2: In this example, we simply create two unit test cases where we will be adding and subtracting the two variables (p and q) and running the code through TestNG and Maven.
Here the sample code:
package testpackage; import org.testng.Assert; import org.testng.annotations.Test; public class demo1 { @Test public void sum() { System.out.print("Sum method"); int p=10; int q=20; Assert.assertEquals(30, p+q); } @Test public void sub() { System.out.print("Sub method"); int p=20; int q=10; Assert.assertEquals(10, p-q); } }
To run the code with the help of TestNG, follow the below process:
To run the same code with the help of Maven, follow the below steps:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.19.1</version> <configuration> <suiteXmlFiles> <suiteXmlFile>C:\Users\JTP\eclipse-workspace\MavenProject\testng1.xml</suiteXmlFile> </suiteXmlFiles> </configuration> </plugin>