How To Create and Run Selenium Test In Maven Project From Command Prompt

Earlier we learnt how to DOWNLOAD AND CONFIGURE MAVEN In Windows, CREATE MAVEN PROJECT and IMPORT MAVEN PROJECT IN ECLIPSE. All three previous steps should be executed by you before writing selenium test In maven project. Once you complete all three previous steps, You are ready to create and run selenium test In maven project.

As you know, Maven manages local jar files repository It self and download required jar files from central repository based on your project dependency configuration In POM.xml file. You can read more about maven on THIS PAGE.

Step 1 : Set Selenium WebDriver and TestNG dependencies
We will create and run our selenium test using testng so we need to set required jar files of selenium and testng In our project's build path. As you know, We have not set required jar files In our project's build path till now. And also we do not need to worry about It as this task will be done by maven Itself. Only we need to set testng and selenium webdriver dependencies In POM.xml file with their latest versions.

Update MavenProject's POM.xml file with bellow given xml code. Selenium adn TestNG version may change In future so you need to updated them accordingly In bellow given file.
<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.demopack</groupId>
  <artifactId>MavenProject</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>MavenProject</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
  <groupId>org.testng</groupId>
  <artifactId>testng</artifactId>
  <version>6.8.8</version>
 </dependency>
    <dependency>
  <groupId>org.seleniumhq.selenium</groupId>
  <artifactId>selenium-java</artifactId>
  <version>2.44.0</version>
 </dependency>
  </dependencies>
</project>

Step 2 : Compile project to download required jar files In local repository
Now we need to compile project to download required dependency jar files. Follow steps given bellow.
  1. Open command prompt.
  2. Set MavenProject folder as working directory In command prompt.
  3. Run mvn compile command In command prompt as bellow. It will compile project and download required jar files of selenium and testng from maven central repository to local repository folder called M2. It can take 10 to 20 minutes If you are downloading It first time. For me, All files are already available In local repository so maven not downloaded any jar file.


Step 3 : Adding required jar files under project's build path from command prompt
You not need to add required jar files manually under project's build path. Run mvn eclipse:eclipse command In command prompt as bellow. Selenium WebDriver and TestNG Jar files will be added automatically under project's build path and It will show message BUILD SUCCESS.


Now If you check your project's build path, all selenium and TestNG jar files will be there.

Step 4 : Create selenium webdriver test cases
First of all delete existing AppTest.java file from src/test/java package and create two java files as bellow under same package.

GoogleTest.java
package com.demopack;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class GoogleTest {
 WebDriver driver;

 @BeforeTest
 public void StartBrowser_NavURL() {
  driver = new FirefoxDriver();
  driver.manage().window().maximize();
 }

 @AfterTest
 public void CloseBrowser() {
  driver.quit();
 }

 @Test
 public void testToCompareDoubles() {
  driver.get("http://www.google.com");
 }
}

BingTest.java
package com.demopack;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class BingTest {
 WebDriver driver;

 @BeforeTest
 public void StartBrowser_NavURL() {
  driver = new FirefoxDriver();
  driver.manage().window().maximize();
 }

 @AfterTest
 public void CloseBrowser() {
  driver.quit();
 }

 @Test
 public void testToCompareDoubles() {
  driver.get("http://www.bing.com");
 }
}


Step 5 : Run Maven-Selenium WebDriver tests from command prompt
For running selenium test cases, run mvn package command In command prompt. It will validate and compile maven project and then run selenium webdriver test cases.

At the end of test execution, Refresh "MavenProject" project folder and expand target folder. Go to surefire-reports -> Open index.html file. It will open testng execution report as shown bellow.


This way we can create and run selenium test cases In maven project using testng and generate testng reports.

2 comments:

  1. Thanks again for this helpful tutorial

    ReplyDelete
  2. run mvn package -> creating class files, but not printing the SOP values. Will it be saved in any other file?

    ReplyDelete