How to Run Selenium Tests in Docker

In this post, we will cover how to run Selenium tests in Docker and execute them in Chrome and Firefox browsers. We will also understand why we should run Selenium tests in Docker?

Prerequisite

There are few prerequisites that you need to be aware of before we get started-

Why should we run Selenium tests in Docker?

Before we run our tests in Docker, it’s important to understand the problems Docker can solve for us.

Session creation issues

If you have been running your tests without Docker, you might have seen this kind of error before — session not created: This version of ChromeDriver only supports Chrome version 89

This error comes when the version of the ChromeDriver and the version of the local browser don’t match. In my case, my browser version was v91 and the ChromeDriver version was v89. Now, there are 2 options here for me -

  1. if I need to run tests in the latest version then I need to update the ChromeDriver to v91
  2. if I need to run tests in the older version then I need to downgrade my Chrome browser to v89

This becomes quite a common problem every time there’s a mismatch of versions. So to solve this problem we can use Docker images that come with a specific ChromeDriver as well as the browser installed which is compatible with each other. Despite what version you have in your local machine, you can still go ahead and execute your tests within Docker.

Multiple version support

Another advantage that we get with Docker is that we can have multiple Chrome versions set up which we can use to execute our tests. For example, I can have Chrome v91 running on port 4444 as well as Chrome v81 running on port 4446 and can execute the tests on both.

Docker provides you with a lot more flexibility in terms of choosing the different browsers as well as choosing the browser versions without having to worry about any kind of infrastructure setup.

#programming #test-automation #docker #javascript #selenium

How to Run Selenium Tests in Docker