Transcripted Summary

In this chapter we will look at how to execute parallel tests from Jenkins.

Let's have a look at our project which contains two suites, "suite1" and "suite2".

Each suite implements a single class containing two tests. "OpenCartTest.java" and "SourceDemoTests.java" are the source files.

Class "OpenCartTest" contains two methods. launchSite() launches a URL and verifies the header. searchForProduct() searches for a product on the OpenCart website.

Similarly, class "SourceDemoTests" contains two test methods. open() launches a URL and verifies the header. login() tests a login action and verifies whether or not users can see products.

If we wish to execute these in parallel we know that TestNG manages multiple suite execution through "testng.xml".



We have completed this configuration to run these suites in parallel. There is nothing else we need to do from Jenkins as we have already configured this behavior through TestNG.

Let us see how it gets executed in Jenkins.

In Jenkins I'm triggering the build of our project. We have already configured Jenkins to checkout from Git so it pulls the latest changes and begins execution.



Here we see it launching two browsers and both of the test cases will execute in parallel. All good. So the execution is done.

The source code for the test classes as well as the "testng.xml" configuration are included below.


# 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();   		 
    }    
}

# SauceDemoTests.java

package suite2;

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;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.AssertJUnit;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import org.openqa.selenium.JavascriptExecutor;
import org.testng.Assert;


public class SauceDemoTests {
    private WebDriver driver;
    private String url = "https://www.saucedemo.com/";
    private String header = "Swag Labs";

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


    public void waitForPageLoaded() {
        ExpectedCondition<Boolean> expectation = new ExpectedCondition<Boolean>() {
            public Boolean apply(WebDriver driver) {
                return ((JavascriptExecutor) driver).executeScript("return document.readyState").toString().equals("complete");
            }
        };
        try {
            Thread.sleep(1000);
            WebDriverWait wait = new WebDriverWait(driver, 30);
            wait.until(expectation);
        } catch (Throwable error) {
            Assert.fail("Timeout waiting for Page Load Request to complete.");
        }
    }

    //Test to launch browser with url
    @Test(enabled = true, priority = 0)   			 
    public void open() {
        driver.get(url);
        waitForPageLoaded();
        String title = driver.getTitle();
        System.out.println(title);
        //My Store
        AssertJUnit.assertTrue(title.equals(header ));
    }    

    //Login

    @Test(enabled = true, priority = 1)
    public void login() {
        driver.findElement(By.id("user-name")).sendKeys("standard_user");
        driver.findElement(By.id("password")).sendKeys("secret_sauce");
        driver.findElement(By.className("btn_action")).click();
        AssertJUnit.assertTrue(driver.findElement(By.className("product_label")).getText()
            .equals("Products"));
    }

    //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();   		 
    }
}

# testng.xml

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">    

<suite thread-count="2" verbose="1" name="Parallel Test Suite"
annotations="JDK" parallel="tests">
    	 
  <test name="OpenCartTest">
     <classes>
   	<class name="suite1.OpenCartTests"/>
     </classes>
   </test>
   
  <test name="SaceDemoTest">
 	<classes>
   	<class name="suite2.SauceDemoTests"/>
 	</classes>
   </test>
</suite>



Quiz

The quiz for this chapter can be found in section 3.7

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