How to run testng.xml from POM.xml (maven)

In this article i will talk about how to run testng.xml from maven’s POM.xml file. This is very useful feature of maven when we need to run multiple testng.xml files.

run testng.xml from pom.xml maven

The very first thing is, install maven in your eclipse IDE. Refer below article to know about how to install maven in eclipse ide.

How to download and Install Maven in Eclipse IDE?

Now, create a maven project.  Refer below article to understand how to create new project in eclipse IDE.

How to create Maven Project for Selenium in Eclipse IDE?

Now, write one sample test case in one of the folder created by maven, preferably test folder.  I have created below sample code –

package kdf1;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;

public class Sample {
  
  @Test
  public void test(){
    System.out.println("Test Print");
    System.setProperty("webdriver.chrome.driver", "C:\\chromedriver.exe");
    WebDriver driver = new ChromeDriver();
    
    driver.navigate().to("http://demowebshop.tricentis.com/");
    driver.manage().window().maximize();
    
    driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
  }

}

Now, create testng.xml file for executing above test case. My testng.xml files looks like –

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Default suite">
  <test verbose="2" name="Default test">
    <classes>
      <class name="kdf1.Sample"/>
    </classes>
  </test> <!-- Default test -->
</suite> <!-- Default suite -->

Now, create a POM.xml file, add required dependencies. My POM.xml looks like –

<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</groupId>
  <artifactId>kdf1</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  
  <dependencies>
  <dependency>
  <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-java</artifactId>
    <version>2.53.0</version>
  </dependency>
  
  <!-- https://mvnrepository.com/artifact/org.testng/testng -->
<dependency>
    <groupId>org.testng</groupId>
    <artifactId>testng</artifactId>
    <version>6.9.9</version>
    <scope>test</scope>
</dependency>
  
  </dependencies>
    
</project>

Now, My job is to execute above given testng.xml file from above created POM.xml file via maven commands.

To achieve this, we need to use Suite XML files. Add below lines of code in POM.xml under <build> tags.

Make sure that you give the full testng.xml path.

<plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.20.1</version>
        <configuration>
          <suiteXmlFiles>
            <suiteXmlFile>testng.xml</suiteXmlFile>
          </suiteXmlFiles>
        </configuration>
      </plugin>
</plugins>

After adding above piece of code in to my POM.xml (under <build> tags), my POM.xml looks like –

<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</groupId>
  <artifactId>kdf1</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  
  <dependencies>
  <dependency>
  <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-java</artifactId>
    <version>2.53.0</version>
  </dependency>
  
  <!-- https://mvnrepository.com/artifact/org.testng/testng -->
<dependency>
    <groupId>org.testng</groupId>
    <artifactId>testng</artifactId>
    <version>6.9.9</version>
    <scope>test</scope>
</dependency>
  
  </dependencies>
  <build>
  <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.20.1</version>
        <configuration>
          <suiteXmlFiles>
            <suiteXmlFile>C:\Users\Prakash\workspace\kdf1\testng.xml</suiteXmlFile>
          </suiteXmlFiles>
        </configuration>
      </plugin>
</plugins>
  </build>
  
</project>

Thats it, you are done.

You are good to execute your testng.xml file from maven’s POM.xml. The main advantage of using this is we can execute multiple testng.xml files.

You can execute the project now by right clicking on project, Run As –> Maven Test  (another way to execute test case is from command prompt).

This was all about how to run testng.xml from pom.xml.

To know more about how to execute maven commands from command prompt, refer below link –

Maven Build Life Cycle – How to execute maven project

 

Hope this helps !!!!! Do provide your valuable comments !!!

Maven Build Life Cycle – Build process in Maven

In this article i will talk about maven build life cycle, that is maven build process.

In my last tutorial. i talked about maven introduction, how to install maven & other things.

Introduction to Maven (A Build Tool) – What is Maven?

How to download and Install Maven in Eclipse IDE?

How to create Maven Project for Selenium in Eclipse IDE?

Okay, so let us talk about maven build life cycle. Maven build process is via command prompt or in eclipse IDE.

Every build follows the life cycle (Write code -> compile -> run test cases -> package -> install).  In the similar way maven has its own build life cycle.

Maven-Build-Life-Cycle consist of below phases –

  1. Validate – (Check if everything is at proper place like POM.xml, if configuration is proper etc)
  2. Compile – (Takes all .java files & create .class files)
  3. Test –  (Where our actual test cases runs)
  4. Package –  (Packages the code, all class files either in jar / war files, depending on what we specify in POM.xml)
  5. Install –  (Install the package in to local maven repository. Note that – Install does not mean installation in to your server like install war in to server. Here it is maven specific. It just put in local maven repository. So, Install is something that helps you to publish your jars in to your local repository so if you have dependency of this jar in another project, it can pull that jar from local repository).
  6. Deploy –  (Deploy does not mean deploying on webserver. This means publish this artifact to remote repository so that other can pull the same. This is more or less same as install phase. We do not use deploy phase mostly until and unless your projects are linked and they have dependency of newly created jar file on another project)

How to run Maven commands?

Maven commands can be run via command prompt or can be via eclipse IDE.

  1. Run Maven Commands from eclipse IDE

Once you have created a maven project & updated you POM.xml with required testng.xml (watch below video to understand how), we can run maven project from eclipse ide. refer below screenshot.

maven build process - Run maven commands from eclipse IDE

You can right click on maven project & then click on “Run As”, You will find multiple options to run your maven projects. Will talks about multiple options in below few para.

2. Another way to run maven commands is from Command prompt.

Once your maven project is ready, Go to command prompt & go to the directory where your maven project is located at.

My maven project is present at – C:\Users\Prakash\workspace\kdf1

So, go to this location & enter the maven command here. Execution should start. Refer below screenshot.

maven build process - Run maven commands from command prompt

So, above is the way how to execute maven commands.

Now what are the maven commands?

  1. mvn clean –> This command is used to clean results from previous build
  2. mvn clean test –> this command is used to clean & then run the test cases (So this include compile as well)
  3. mvn compile –> this will only compile the project (creates .class files only). No Execution.
  4. mvn package –> this will compile, execute & it will creates a package (.jar or war file as specified)
  5. mvn deploy –> this will compile, execute, creates package & will deploy in repository.

.  .  .  .  .  there can be few more commands as well .   .   .   .

This is all about maven build life cycle.

Refer below video for the same –

 

Do post you comments / improvement suggestions. Thanks !!!!!

Introduction to maven – What is maven (Build tool)

Introduction to maven?

Maven is basically a project management tool, where we manage our projects, we can  build projects, deploy projects & many more features like reporting & documentation. It refers concept of Project Object Model (POM). In a short term, it is a build tool.

If you are using build tool like mavn, it simplifies your build process. everything becomes very simple because it follows some life-cycle what it has. we will talk about mavn life-cycle in couple of next para.

Why do we need maven project?

Earlier project who does not have mavn, they need to add jar files manually & if multiple persons are working on the same project, they have to use same jar files. If one has changed, it cause issues. Also earlier to mavn, we used to create project folder structure by our own. Where as after mavn, this is not manually required, because –

1. In case of maven project, you need not add any jar files in to the project. just add all your dependency in POM.xml file
2. Maven project creates a very good project folder structure for coding purpose.

So, in Short, Maven has two main purposes
  1. Project management (Includes creating folder structure, compiling, packaging etc.)
  2. Dependency management (update dependencies in POM.xml file to download jar automatically)

As i said earlier, maven is a build tool, now let us try and understand what us build tool.

Build tool has a set of processes, certain outcomes like compile source code, copying resources, compiling and running tests, packaging project(in jar or zip file based on selected option), deploying project & finally cleanup. So when you compile your source code using maven, it will create a jar or war file for further use.

All settings of maven project are POM.xml file

What is POM.xml?

POM is an xml file that contain an information about project and configuration details used by mavn to build project. Information is about name, version, artifact it, dependencies, plugin information & profiles. We will talk about POM.xml in more details in coming sessions.

What are important maven objectives?

  1. Making the build process easy.
  2. Provides the uniform build system.
  3. Provide quality project information.
  4. Providing guidelines for best practices development.
  5. Allowing transparent migration to new feature.]

Maven Build Lifecycle –

For the person building a project, this means that it is only necessary to learn a small set of commands to build any Maven project, and the POM will ensure they get the results they desired.

There are three built-in build lifecycles: default, clean and site. The default lifecycle handles your project deployment, the clean lifecycle handles project cleaning, while the site lifecycle handles the creation of your project’s site documentation.

Each of these build lifecycles is defined by a different list of build phases, wherein a build phase represents a stage in the lifecycle.

maven lifecycle

For example, the default lifecycle comprises of the following phases (for a complete list of the lifecycle phases, refer to the Lifecycle Reference):

  • validate – validate the project is correct and all necessary information is available
  • compile – compile the source code of the project
  • test – test the compiled source code using a suitable unit testing framework. These tests should not require the code be package or deploy.
  • package – take the compiled code and package it in its distributable format, such as a JAR.
  • verify – run any checks on results of integration tests to ensure quality criteria are met
  • install – install the package into the local repository, for use as a dependency in other projects locally
  • deploy – done in the build environment, copies the final package to the remote repository for sharing with other developers and projects.

These lifecycle phases (plus the other lifecycle phases not shown here) are executed sequentially to complete the default lifecycle. Given the lifecycle phases above, this means that when the default lifecycle is used, Maven will first validate the project, then will try to compile the sources, run those against the tests, package the binaries (e.g. jar), run integration tests against that package, verify the integration tests, install the verified package to the local repository, then deploy the installed package to a remote repository.

I will talk about maven in more detail in next articles. Refer below links –

How to download and Install Maven in Eclipse IDE?

How to create Maven Project for Selenium in Eclipse IDE?

 

How to Install Maven in Eclipse IDE ? – Build Tool for java

In this article i will talk about how to install maven in eclipse IDE. Maven is basically a build tool for java based projects.

There are two ways to install maven in eclipse IDE. First one is to install directly in Eclipse IDE via Eclipse MarketPlace / Install new software from help menu of Eclipse IDE & second one is, Install it on your machine manually & then configure the installation details in Eclipse IDE.

Let us talk about both methods.

1.  Install maven directly in eclipse IDE via eclipse marketplace or install new software from help menu

Follow below steps one by one

Open Eclipse, Go to Help Menu on Top right hand side & click on install new software.

Install maven 1

Add depository for downloading M2e as shown in below image.

Install maven 1

Then select work with as the current URL that you have added. It will show you the list of Maven Integration tools for Eclipse IDE. Select the desired one & click on next. Shown in below image

Install maven 1

Click on the next, below screen will show you the current installation items that you have selected.

Install maven 1

Now accept the Terms and conditions of license agreement & click on finish button so as to start with installation of maven plugin inside eclipse IDE. Shown in below image.

Install maven 1

Finally, You can see that software installation has begin. Shown in below image

Install maven 1

Once installation is finished, do restart your machine.

This is how we can install maven inside Eclipse IDE.

 

2. Another way to install maven is Install it in your machine & then configure it in Eclipse IDE.

Follow below steps to get the installation done using above method.

Download Maven files from : https://maven.apache.org/download.cgi

or click here apache-maven-3.5.2-bin.zip

Then Extract it to some folder. Let us say i am extracting it in my C:/ drive. So path of maven files is : C:\apache-maven-3.5.2

Now, Setup Environment Variables.

Go to start program, type “env“, in search result you can find option to edit environment variables. Click on it. Refer below image –

install maven

Once you click on it, System properties will open. Click on Environment variables. As shown in below image.

install maven

Now add below variables by clicking on new button for system variables.

JAVA_HOME=C:\Program Files\Java\jdk1.8.0_111
M2_HOME=C:\apache-maven-3.5.2
M2=%M2_HOME%\bin

Once above variable setup is done, Edit the “Path” variable. Enter below path at the end of current path (Do not modify anything other than this unless you are sure about it)

Path=;%M2%;%JAVA_HOME%\bin

install maven

Once this is done, click on OK. Now you are done with environment variable setup. Now you can verify if maven is installed correctly on your machine or not.

To check this, open command prompt & type below command.

mvn -version

Result of above command should show the currently installed maven version (This means maven is installed correctly) Refer below image.

install maven

Next part is now, to configure this installed maven in to your eclipse IDE.

Before we update anything in Eclipse IDE, first we need to modify setting.xml file from maven installation directory. Here we need to add the path of current maven repository at correct position. refer below image.

Location of settings.xml file: C:\apache-maven-3.5.2\conf\settings.xml

install maven

In the above image, the highlighted line need to be inserted at correct position, as shown above. Save the file.

Now let us update Eclipse IDE. To do this, Click on Window & go to preferences of your Eclipse IDE.

Go to maven –> Installations. Window will looks like below.

install maven

Now click on Add, and add the path of your current maven directory here & Click on OK. refer below image.

install maven

Now go to user Settings in the same menu & add path of your settings.xml file (Located in conf folder of your maven installation directory)

install maven

Click on OK.

That’s it, now done.

Clean your project & start with new maven project. You should be good to start with creation of maven project.

Refer below video for the same –

Refer below article to know about how to create new maven project.

How to create maven project in Eclipse IDE

 

Put your comments if you have any issues !!!! Thanks !!