Run Test Automation Scripts in Command Prompt With MAVEN

While developing the Selenium WebDriver test automation scripts, the scripts are executed within Eclipse.

But as soon as the scripts work as expected, you may want to make the scripts available to other team members.

Like other manual testers.

Or developers.

You may also want to run the test scripts automatically as scheduled jobs.

Or use them in a continuous integration process.

All these require the ability of running the test scripts in the Command Prompt.

Let`s see how you can do this.

We will use an example project that automates the following test case:

  1. opens the home page of the Vancouver Public Library site
  2. checks if the home page title is correct

The project uses 2 classes: test class and home page class.

TEST CLASS

import static org.junit.Assert.*;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class TestClass {

  WebDriver driver;

  @Before
  public void setUp() {
    driver = new FirefoxDriver();
  }

  @After
  public void tearDown() {
    driver.quit();
  }

  @Test
  public void testScript() {
    HomePage homePage = new HomePage(driver);
    homePage.open();
    assertTrue(homePage.isTitleCorrect());
  }

}

HOMEPAGE class

import org.openqa.selenium.WebDriver;
public class HomePage {
  WebDriver driver;
  String url = "http://www.vpl.ca";
  String title = "Vancouver Public Library - Home";

  public HomePage(WebDriver driver) {
    this.driver = driver;
  }

  public void open()  {
    driver.get(url);
  }

  public Boolean isTitleCorrect() {
    return driver.getTitle().equals(title);
  }

}

The following phases are needed for running this code from command prompt.

Everything that follows assumes that Maven is installed on your computer.

If you dont have it installed, use this link for instructions.

If you want to learn more about Maven, this is a very good tutorial.

  1. Create the Maven Project

This can be done manually without Eclipse.

Or you can use Eclipse for generating the project:

  • Open Eclipse
  • Click File, New, Project
  • In the New Project window, expand the Maven category
  • Select Maven project and click Next

1. select maven project

  • Check the Create A Simple Project checkbox
  • Click Next

2. maven - create a simple project

  • Configure the Maven project by providing the Group ID, Artifact ID and Name

3. configure maven project

  • Click Finish

The project is created and it shows in the Navigator panel of Eclipse.

You can see the project structure by expanding the project node:

4. maven project structure

The project folder includes

  • SRC folder; includes source code for the test and page object classes.
  • TARGET folder; includes the resources generated by compiling the source code.
  • POM.XML file; it defines the project dependencies

Under the SRC folder, you will find 2 folders:

  • main; the page objects go here
  • test; the test classes go here

When the project is created, the POM.XML file has just a few lines:

5. initial pom file

The project does not have any dependencies added yet.

6. no maven dependencies

2. Add dependencies to the POM.XML file

2 dependencies are needed for our project:

  1. JUNIT
  2. SELENIUM

They are added to POM.XML by editing the file:

8. add junit and selenium dependencies to pom file

The project has now 2 dependencies added to it:

9. junit and selenium are added as dependencies

3. Add the test and page object classes to the project folders

Next, we need to add the test class to the SRC – TEST – JAVA folder.

The page object class is added to the SRC – MAIN – JAVA folder.

10. add class files

4. Execute the Maven Project in Eclipse

The Maven project can be executed by

  • right clicking on the project name
  • clicking Run As
  • clicking Maven Test

11. run maven test

The test script is being executed and the results are displayed in the Console panel.

12. build success from eclipse

After the project execution, you can see the .class files in the TARGET folder of the project:

13. class files are created

5. Run the Maven Project in Command Prompt

First, we need to get the project folder path.

This can be done by

  • right clicking on the project name
  • clicking Properties
  • getting the value of the Location field

14. get project path

The last steps of the process are:

  • close Eclipse
  • open Command Prompt
  • go to the project folder path
  • execute the project using the mvn test command

15. test runs in command prompt

Try to create your own project using this short tutorial.

If you have any questions, please leave them in the Comments section.


5 Comments

    1. Thanks, Shrikant.

      The only change to do is to add the TestNG dependency to the pom.xml file instead of the Junit dependency.

      Like

      Reply

  1. Hey Alex, how do we Pass Parameters from Command Line to a Selenium Project using Maven Command with pom.xml ? I mean I want to pass username and password through maven command line and my java method should pick and run.
    Thanks.

    Like

    Reply

Leave a comment