Pass Parameters from Command Line to a Selenium Project using Maven Command

Hola Testors,

Today we will look at a little cool trick we can use for selenium automation “How to pass values from command line to a selenium project through maven commands”. This trick is very important if you have to test your project on different parameters to your projects. Suppose you have to test your project on different URLs/ environments (Dev/CI/QA/Production) or you have to test on different browsers on different time etc. You can use this trick for that kind of scenarios.

First, you need to create a selenium project from maven.

Then include all the needed dependancies to pom.xml

After that you can create a test file. Here, I’m creating a sample java class to test different URLs with different browsers.


package com.daz.passparameters.passparameters;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.safari.SafariDriver;
import junit.framework.TestCase;
class AppTest extends TestCase
{
private String URL=System.getProperty("url");
private String BROWSER=System.getProperty("browser");
static WebDriver driver;
static String CHROME_PATH="drivers/chromedriver";
    @org.junit.Test
    public void testparam()
    {
    if(BROWSER.equalsIgnoreCase("Firefox"))
    {
    driver=new FirefoxDriver();
    }
    else if(BROWSER.equalsIgnoreCase("Safari"))
    {
    driver=new SafariDriver();
    }
    else if(BROWSER.equalsIgnoreCase("Chrome"))
    {
    System.setProperty("webdriver.chrome.driver", CHROME_PATH);
    driver=new ChromeDriver();
    }
    driver.get(URL);
    driver.quit();
    }

}

Here I included two lines to capture values from command line

private String URL=System.getProperty("url");
private String BROWSER=System.getProperty("browser");

We can use System.getProperty() to capture values from command line.

Then save to program.

When you are running your application with maven you have to give all the mentioned parameters with command.

For above example you can give parameters like

mvn clean install -Durl="http://www.google.lk" -Dbrowser="Chrome"

Hope you enjoyed this cool trick.

PS: You can download the sample code from my GitHub repo by clicking here

Advertisements

One comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s