How to run selenium 4 grid tests on Docker containers
By using Docker containers for running chrome/browser drivers and selenium grid, you can run your selenium tests without the need for any dependency such as chrome, chrome driver or webdrivermanager locally.
When you use a remote selenium grid, then it also makes your local debugging easy, you can point to remote URL and see why your tests are failing on remote without the need to access Linux server and running debugging commands on a remote machine, below is a step by step guidance on how you can set up docker containers to run selenium tests-
-update Linux system
sudo apt-get update
-install docker
sudo apt install docker.io
-check docker version
sudo docker -v
-create compose.yml for selenium grid and chrome containers
To execute this docker-compose yml file use docker-compose -f docker-compose-v3.yml up
Add the -d
flag at the end for detached execution
To stop the execution, hit Ctrl+C, and then docker-compose -f docker-compose-v3.yml down
version: “3”
services:
chrome:
image: selenium/node-chrome:4.8.3-20230328
shm_size: 2gb
depends_on:
– selenium-hub
environment:
– SE_EVENT_BUS_HOST=selenium-hub
– SE_EVENT_BUS_PUBLISH_PORT=4442
– SE_EVENT_BUS_SUBSCRIBE_PORT=4443
selenium-hub:
image: selenium/hub:4.8.3-20230328
container_name: selenium-hub
ports:
– “4442:4442”
– “4443:4443”
– “4444:4444”
yml reference- https://github.com/SeleniumHQ/docker-selenium/blob/trunk/docker-compose-v3.yml
-run containers using compose command (-d for detach mode)
-install compose and set permission
sudo apt install docker-compose
sudo docker-compose up
-see running containers info-
sudo docker ps
-see logs of the particular container
sudo docker logs containerid
-see performance stats of containers
sudo docker stats
-check grid is running
http://yourip:4444
–run selenium tests using remote web driver URL of selenium hub
in your tests“
@Test
public void test() {
WebDriver driver = null;
ChromeOptions options = new ChromeOptions();
options.addArguments("start-maximized"); // open Browser in maximized mode
options.addArguments("disable-infobars"); // disabling infobars
options.addArguments("--disable-extensions"); // disabling extensions
options.addArguments("--disable-gpu"); // applicable to windows os only
options.addArguments("--disable-dev-shm-usage"); // overcome limited resource problems
options.addArguments("--no-sandbox"); // Bypass OS security model
options.addArguments("window-size=1200,1100");// set display size of window
try {
driver = new RemoteWebDriver(new URL("http://ip:4444/wd/hub"), options);
driver.get("https://testautomasi.com");
Thread.sleep(20000);
assertEquals(driver.getTitle(), "Home - Welcome to automasi solutions private limited");
} catch (MalformedURLException | InterruptedException e) {
e.printStackTrace();
}
driver.quit();
}
Have any feedback? Please don’t hesitate to leave it in the comments section.
Best article ever read like this till now, Thankyou, the article is very good and i learned many things
please post more like this thankyou