Sunday 21 July 2019

Running multiple selenium scripts in parallel mode with Grid and Docker Linux containers

It is quite common to run selenium automated scripts from windows machine, but sometimes management request us to run the same scripts in Linux with different browsers.

In conventional way to fulfill above requirement, usually we move whole selenium scripts with framework in to linux machine and run the same script which is quite time consuming process and also we have to worry about infrastructure cost.

To achieve this, we have various options available. we could connect to saucelabs or browserstack tools and get this done. But these are licensed tools/interfaces

If you are just doing limited testing on linux browser then I would recommend to go docker Linux containers which is quite easy and super fast.

In this post I am going to explain how we can run selenium scripts with grid in parallel way by running those scripts in Docker containers. This can be done in 3 below mentioned steps.

1.Create selenium sample script to be executed in grid format
following is one of the sample script

package practiceTestingGroupID.PracticeProject;

import org.testng.annotations.Test;
import java.net.MalformedURLException;
import java.net.URL;
import org.openqa.selenium.By;
import org.openqa.selenium.Platform;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Parameters;

public class TestNG_Grid {


public static RemoteWebDriver driver;

  @BeforeTest
  @Parameters({"strPlatform", "strBrowserName", "strHubURL", "strAppURL"})
  public void beforeTest(String strPlatform, String strBrowserName, String strHubURL, String strAppURL) throws MalformedURLException {
  DesiredCapabilities cap = null;
if (strBrowserName.equalsIgnoreCase("firefox"))
  {
  cap = new DesiredCapabilities().firefox();
  }
  else if  (strBrowserName.equalsIgnoreCase("chrome"))
  {
cap = new DesiredCapabilities().chrome();
  }
cap.setBrowserName(strBrowserName);
cap.setPlatform(Platform.LINUX);
driver = new RemoteWebDriver(new URL(strHubURL), cap);
driver.get(strAppURL);
  }

  @Test
  public void Navigate2018Posts() throws InterruptedException {
Thread.sleep(2000);
driver.findElement(By.xpath("//a[contains(text(),'2018')]")).click();
Thread.sleep(2000);
System.out.println("Browser title is: "+ driver.getTitle());
String strPath = UtilityFuns.CaptureScreenShotMethod(driver);
System.out.println("capture screenshot path: "+strPath);
driver.quit();
  }


}

2. Create docker Linux containers by installing hub and selenium/node-firefox-debug, selenium/node-chrome-debug images and linking these nodes with hub.


Copy the  path of the hub and pass this in the testng.xml file


3. Created testng.xml file to allow selenium script execution in parallel way
following is the sample testng.xml file

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" thread-count="4" parallel="tests">
<test verbose="2" name="Linux1">
  <parameter name="strPlatform" value = "LINUX" />
  <parameter name="strBrowserName" value = "chrome" />
  <parameter name="strHubURL" value = "http://localhost:4446/wd/hub" />
  <parameter name="strAppURL" value = "http://easy2autotest.blogspot.com" />
    <classes>
    <class name="practiceTestingGroupID.PracticeProject.SeleniumScriptOnGrid" />
    </classes>
</test> <!-- Test -->

<test verbose="2" name="Linux2">
  <parameter name="strPlatform" value = "LINUX" />
  <parameter name="strBrowserName" value = "firefox" />
  <parameter name="strHubURL" value = "http://localhost:4446/wd/hub" />
  <parameter name="strAppURL" value = "http://easy2autotest.blogspot.com" />
    <classes>
    <class name="practiceTestingGroupID.PracticeProject.SeleniumScriptOnGrid" />
    </classes>
</test> <!-- Test -->
</suite> <!-- Suite -->

When you execute this testng.xml file, it first connects to Selenium Grid which will connect to Docker container with the given path and with the help of RemoteWebDriver it triggers execution in browsers located in docker containers.

you can observe the execution in docker containers log or if you have VNS viewer you can connect to docker linux machine and see it.



No comments:

Post a Comment