Run your Crossbrowser Tests With Docker

Selenium Grid is the best way to run your Selenium based tests as Dockerized and parallel with wide range browser choice.Selenium Grid based on with Hub-Node concept.The one hub as a main machine creates one or one more node and runs test on these nodes.With that simply Selenium Hub provides you

  • Central entry point for all tests
  • Management and control of the nodes / environment where the browsers run
  • Scaling
  • Running tests in parallel
  • Cross platform testing
  • Load balancing

To run our tests with Selenium Grid we need to some dependencies on our maven project.

  • TestNg
  • WebDriverManager
  • Selenium-Java
  • Selenium-Remote-Driver

On our test we need to use RemoteWebDriver instead of WebDriver.And we can do it simply adding WebDriverManager in our “pom.xml”.WebDrivermanager is a library which allows to automate the management of the drivers required by Selenium WebDriver.

Our code will be like this on the Setup.

1
2
3
4
5
6
7
8
9
10
11
12
13
RemoteWebDriver driver;
@Parameters({"Port"})
@BeforeTest
public void setup(String Port) throws MalformedURLException {
if(Port.equalsIgnoreCase("9001"))
{
driver = new RemoteWebDriver(new URL("http:localhost:4444/wd/hub"), DesiredCapabilities.chrome());
driver.manage().window().maximize();
}
else if(Port.equalsIgnoreCase("9002")){
driver = new RemoteWebDriver(new URL("http:localhost:4444/wd/hub"), DesiredCapabilities.firefox());
driver.manage().window().maximize();
}

To setup our dockerized environment , we can use Dockerfile on our system.Below example we have a hub with port 4444 and nodes of these hub via Chrome node and Firefox node.These all nodes had their ports also.Save the below lines on your root path as a “Dockerfile.yaml”.

The simple command will create all of nodes and hub for us.

1
docker-compose up -d

And to connect and run our tests on the hub , simply we can create test sets named as “Chrome” and “Firefox”.With that we can classify which tests will run on which browser we wanted.Our testng.xml file will be like.

If you done everything correctly your test will run with this command

1
2
mvn clean test -Dmaven.repo.local=.m2/repository