Run_Parallel_Tests_In_Chrome_Using_DockerCompose

Tutorial 32​​ ​​ Run​​ Parallel Selenium Tests In​​ Chrome Browser using Docker Compose

Please read the previous article before reading this one.

In this article, we will​​ learn​​ how to run selenium tests in parallel against multiple docker containers.

Let us cd to directory where we have placed .yml file

Start docker engine by double clicking docker desktop icon

Wait for the docker engine to start,​​ the below command shows that​​ no containers are running​​ at present

We still have 4 docker images in our local

Let us​​ create 2 chrome containers
docker-compose up --scale chrome=2 -d

Notice below that 2 chrome containers are created

Also​​ make sure that​​ 2 chrome containers are running​​ by executing below command

Launch​​ http://localhost:4444​​ 

Notice below that 2 chrome browsers are running

Let​​ us create 2 TestNG classes (Test1.java and Test2.java) as shown below

Next, we will create testng.xml file to enable us to run our tests in parallel.

To​​ create testng.xml, right click​​ project > TestNG > Convert to TestNG

​​ 

Click Next​​ and click Finish, the testng.xml gets created as shown below

​​ 

Replace the default contents of testng.xml with below content and save it. Notice that both the tests are pointing to Test1.java (this means that the 2 chrome sessions would execute the same Test1.java)

Note: Sometimes when we specify thread count, the test might not​​ run​​ properly, that’s why haven’t used thread count in the above xml.

Execute the testng.xml

Notice below that 2 concurrent (2 parallel tests) chrome sessions are started, shown by​​ 

Notice below 2 chrome session running​​ 

Notice below that the 2 Tests pass

 

Let us now point the second test to Test2.java​​ (this means that one chrome session would execute Test1.java and another chrome session would execute Test2.java)

Save the xml

Execute the testng.xml

Notice below that 2 concurrent (2 parallel tests) chrome sessions are started, shown by​​ 

 

That is how we can execute our tests in parallel using docker compose.

Snapshot (testng xml​​ to execute​​ Test1​​ in 2 parallel chrome sessions)

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

 ​​​​ 

<suite​​ name="suite"​​ parallel="tests">

 ​​​​ <test​​ name="Chrome1">

 ​​ ​​ ​​​​ <classes>

 ​​ ​​ ​​ ​​ ​​ ​​​​ <class​​ name="sel4scripts.Test1"​​ /> ​​ ​​ ​​ ​​ ​​ ​​​​ 

 ​​ ​​ ​​​​ </classes>

 ​​​​ </test> ​​​​ 

 ​​​​ 

 ​​​​ <test​​ name="Chrome2">

 ​​ ​​ ​​​​ <classes>

 ​​ ​​ ​​ ​​ ​​ ​​​​ <class​​ name="sel4scripts.Test1"​​ /> ​​ ​​ ​​ ​​ ​​ ​​​​ 

 ​​ ​​ ​​​​ </classes>

 ​​​​ </test>

</suite>

Snapshot (testng xml to execute Test1 and Test2 in 2 parallel chrome sessions)

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

 ​​​​ 

<suite​​ name="suite"​​ parallel="tests">

 ​​​​ <test​​ name="Chrome1">

 ​​ ​​ ​​​​ <classes>

 ​​ ​​ ​​ ​​ ​​ ​​​​ <class​​ name="sel4scripts.Test1"​​ /> ​​ ​​ ​​ ​​ ​​ ​​​​ 

 ​​ ​​ ​​​​ </classes>

 ​​​​ </test> ​​​​ 

 ​​​​ 

 ​​​​ <test​​ name="Chrome2">

 ​​ ​​ ​​​​ <classes>

 ​​ ​​ ​​ ​​ ​​ ​​​​ <class​​ name="sel4scripts.Test2"​​ /> ​​ ​​ ​​ ​​ ​​ ​​​​ 

 ​​ ​​ ​​​​ </classes>

 ​​​​ </test>

</suite>

Code snippet (Test1.java​​ for chrome browser)

package sel4scripts;

import java.net.MalformedURLException;

import java.net.URL;

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeOptions;

import org.openqa.selenium.remote.RemoteWebDriver;

import org.testng.annotations.Test;

public class Test1 {

  @Test

  public void TestParallel() throws MalformedURLException, InterruptedException​​ 

{

  ChromeOptions opt = new ChromeOptions();

  WebDriver driver = new RemoteWebDriver(new URL("https://localhost:4444"),opt);

  driver.get("http://google.com");

  driver.findElement(By.name("q")).sendKeys("Learning Standalone grid");

  System.out.println("Test executed successfully");

 

}

 

}

Code snippet (Test2.java​​ for chrome browser)

package sel4scripts;

import java.net.MalformedURLException;

import java.net.URL;

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeOptions;

import org.openqa.selenium.remote.RemoteWebDriver;

import org.testng.annotations.Test;

 

public class Test2 {

@Test

public void grid2() throws MalformedURLException, InterruptedException

{

  ChromeOptions opt = new ChromeOptions();

  WebDriver driver = new RemoteWebDriver(new URL("https://localhost:4444"),opt);

  driver.get("http://google.com");

  driver.findElement(By.name("q")).sendKeys("Learning Standalone grid");

  System.out.println("Test executed successfully");

 

}

 

}

 

Thank you for reading!

Share On

Share on facebook
Share on twitter
Share on linkedin
Share on whatsapp
Share on tumblr
Share on email

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top

Lifetime Membership Club

LIFETIME MEMBERSHIP BIG SALE - ALL LIVE COURES JUST - 7000 RS/ 99 USD
Attend All Live courses in just 7000 rs / $99 - offer ends 31st DEC 2024