Transcripted Summary

In this chapter, we will explore how to parameterize a build.

This means converting an existing job into one that accepts parameters as input and subsequently modifies its execution behavior based on this input. In your project you can define any number of job parameters.

For this next example I will demonstrate parameters used for browser object initialization.


public class OpenCartTests {
    //Before test
    @BeforeTest
    public void beforeTest() {
        //Instantiate browser based on user input

        if(browser != "" && browser != null) {
            if(browser.equalsIgnoreCase("Chrome")) {
                driver = new ChromeDriver();
                driver.manage().window().maximize();
            }
            else if(browser.equalsIgnoreCase("firefox")) {
                driver = new FirefoxDriver();
                driver.manage().window().maximize();
            }
            else {
                System.out.println("Invalid option Selected hence defaulting to Chrome");
                browser = "Chrome";
                driver = new ChromeDriver();
                driver.manage().window().maximize();
            }
        }
        else {
            browser = "Chrome";
            driver = new ChromeDriver();
            driver.manage().window().maximize();
        }
    }    
}


Here, we have our class "OpenCartTests" from Chapter 3.5. In the beforeTest() function we are setting up the browser.

Notice that the driver object is instantiated based on the value of the "browser" parameter.



Now let's take a look at our Jenkins job. Click on "FirstSeleniumTestJob" and click on "Configure".



In the "General" tab you will find the "This project is parameterized" option. Click on it, then click on "Add Parameter" and add a new "String Parameter".



Let's fill in the details here. The "Name" field value will be "browser" and let's set the "Default Value" as "chrome".

Also provide a detailed description of the parameter. This is just an example, we can use any browser that our framework or project supports.

All right, let's apply and save these changes.

Now Jenkins should pass this parameter to the project. We do this in the "Build" section under the "Build Environment" tab.



Add the following flag to the "Goals" field under "Build":


-Dbrowser="$browser"

$browser will take the value we are passing in the job parameter, browser. This will be passed to our class file and the specified browser will launch on execution.

Apply and save these changes and let's run this example.

For this execution I will change the value of the "browser" parameter from its default of "chrome" to "firefox" before starting the build.



The Firefox browser launches and the test will execute.



The execution looks good. Now I hope you have a better understanding of passing parameters in Jenkins and parameterising builds and jobs.


# OpenCartTests.java

public class OpenCartTests {
    //Before test
    @BeforeTest
    public void beforeTest() {
        //Instantiate browser based on user input

        if(browser != "" && browser != null) {
            if(browser.equalsIgnoreCase("Chrome")) {
                driver = new ChromeDriver();
                driver.manage().window().maximize();
            }
            else if(browser.equalsIgnoreCase("firefox")) {
                driver = new FirefoxDriver();
                driver.manage().window().maximize();
            }
            else {
                System.out.println("Invalid option Selected hence defaulting to Chrome");
                browser = "Chrome";
                driver = new ChromeDriver();
                driver.manage().window().maximize();
            }
        }
        else {
            browser = "Chrome";
            driver = new ChromeDriver();
            driver.manage().window().maximize();
        }
    }    
}


Here, we have our class "OpenCartTests" from Chapter 3.5. In the beforeTest() function we are setting up the browser.

Notice that the driver object is instantiated based on the value of the "browser" parameter.



Now let's take a look at our Jenkins job. Click on "FirstSeleniumTestJob" and click on "Configure".



In the "General" tab you will find the "This project is parameterized" option. Click on it, then click on "Add Parameter" and add a new "String Parameter".



Let's fill in the details here. The "Name" field value will be "browser" and let's set the "Default Value" as "chrome".

Also provide a detailed description of the parameter. This is just an example, we can use any browser that our framework or project supports.

All right, let's apply and save these changes.

Now Jenkins should pass this parameter to the project. We do this in the "Build" section under the "Build Environment" tab.



Add the following flag to the "Goals" field under "Build":


-Dbrowser="$browser"

$browser will take the value we are passing in the job parameter, browser. This will be passed to our class file and the specified browser will launch on execution.

Apply and save these changes and let's run this example.

For this execution I will change the value of the "browser" parameter from its default of "chrome" to "firefox" before starting the build.



The Firefox browser launches and the test will execute.



The execution looks good. Now I hope you have a better understanding of passing parameters in Jenkins and parameterising builds and jobs.


# OpenCartTests.java

package suite1;

import org.testng.annotations.Test;

import suite2.SauceDemoTests;

import org.testng.AssertJUnit;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class OpenCartTests {

    //For demonstrating parameterized builds
    String browser = System.getProperty("browser");

    private WebDriver driver;
    private String url = "http://opencart.abstracta.us/";
    String searchField = "//*[@id='search']/input";
    String result = "//*[@id=\"content\"]/div[3]/div/div/div[1]/a/img";
    String query = "Macbook Air";


    //Test to launch browser with url
    @Test
    public void launchSite() {
        driver.get(url);
        String title = driver.getTitle();
        //Your Store
        AssertJUnit.assertTrue(title.equals("Your Store"));
    }

    //Test to search for a product
    @Test
    public void searchForProduct() {
        driver.findElement(By.xpath(searchField)).sendKeys(query + Keys.ENTER);

        //sleep only when firefox as page loading takes some more time
        if(browser.equalsIgnoreCase("firefox")) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        AssertJUnit.assertTrue(driver.findElement(By.xpath(result)).isDisplayed());
    }


    //Before test
    @BeforeTest
    public void beforeTest() {
        //Instantiate browser based on user input

        if(browser != "" && browser != null) {
            if(browser.equalsIgnoreCase("Chrome")) {
                driver = new ChromeDriver();
                driver.manage().window().maximize();
            }
            else if(browser.equalsIgnoreCase("firefox")) {
                driver = new FirefoxDriver();
                driver.manage().window().maximize();
            }
            else {
                System.out.println("Invalid option Selected hence defaulting to Chrome");
                browser = "Chrome";
                driver = new ChromeDriver();
                driver.manage().window().maximize();
            }
        }
        else {
            browser = "Chrome";
            driver = new ChromeDriver();
            driver.manage().window().maximize();
        }
    }    

    //hooks - to tear down after test is executed
    @AfterTest
    public void afterTest() {
        driver.quit();   		 
    }    
}



© 2022 Applitools. All rights reserved. Terms and Conditions Privacy Policy GDPR