Selenium is an open-source framework system for building the web user interface(UI) tests that can execute on Chrome, Edge, Firefox, and IE. When performing Selenium automation testing, it is essential to test a run in one execution runtime environment. These execution tests can also run in a multi-test run in a single performance environment. We should avoid running the automation tests in an isolation environment.

Post Graduate Program: Full Stack Web Development

in Collaboration with Caltech CTMEEnroll Now
Post Graduate Program: Full Stack Web Development

Selenium Tests in Docker 

Running Selenium tests in Docker containers supports executing code reviews at a more agile speed with the more conventional quality code, archives high test coverage, and generates an excellent quality product with the great quality standard code. 

Docker gives the versatility to perform tests with containers or divide the tests in development and deployment. There are many reasons to practice Selenium with Docker for web-based application testing. In this article, we will deep dive into how to execute Selenium tests in Docker to stimulate the Selenium test automation activity.

We can run the Selenium using these below environments.

  • Python, bound using Selenium WebDriver.
  • PyTest is a testing structure. Here we can use its framework structures per the user preference, like Nose or Junit). 
  • GeckoDriver (Firefox) or, ChromeDriver,  Headless Chrome
  • IntelliJ IDEA as IDE (Integrated development environment)
  • CI tool, like Jenkins or TeamCity. Add the plugins as per the user’s requirement, for example, GitHub repository connection, BlazeMeter for performance testing, Allure for reporting, etc.

Why Should We Run Selenium Tests in Docker?

  • Scalable and Reliable: One of the most important advantages of Docker is its capability to scale. It’s vital because operating a Selenium grid on virtual machines and separate devices takes up additional computing expenses. Docker images take part in some custom resources. Using this, we can quickly review various nodes in a separate instance. Docker and Compose also manage the networking part efficiently.
  • Selenium supports you in running your tests locally, and you can easily configure your Windows manager. As expected, protecting the tests with Docker will allow you to migrate them to a distinctive workspace easily.
  • SeleniumHQ manages the entire range of Docker images. These images can be extracted down quickly and applied right away. We can find the catalog of images at Docker Hub or GitHub repository as per the below standards.
  1. Base - Here, we can develop our custom images.
  2. Hub - This is cloud-based registry assistance. Here we can connect to code repositories and design images, test, and collect.
  3. Node - Images practiced in correlation with Hub image and global Selenium grid. It is a workable solution to begin several node containers using a Hub image.
  4. Standalone - We can find Standalone images on the Selenium server. These images should be run on different ports (e.g., 4444, 4445, etc.) else it will result in port conflicts. Here we no need to maintain the Selenium server jar as it is automatically controlled using the Selenium hub container.
  • It provides the downloading of browser drivers and setting the classpaths.
  • You can designate and maintain the Selenium Grid, and it is simple to record nodes on the hub.
  • The differences between the environments are low, and multiple team members can practice identical containers, unlike the operating system.
  • Less burden of installations.

New Course: Full Stack Development for Beginners

Learn Git Command, Angular, NodeJS, Maven & MoreEnroll Now
New Course: Full Stack Development for Beginners

Run Selenium Standalone Chrome Docker Image Locally

ChromeDriver: ChromeDriver 2.32 works with Chrome 61 and performs well with headless Chrome.

Install:

npm i --save-dev selenium-webdriver chromedriver

Example 1:

const fst = require('fst');

const webdriver1 = require('selenium-webdriver1 );

const chromedriver = require('chromedriver');

const chromeCapabilities = webdriver1.Capabilities.chrome();

chromeCapabilities.set('chromeOptions', {args: ['--headless']});

const driver = new webdriver1.Builder()

  .forBrowser('chrome')

  .withCapabilities(chromeCapabilities)

  .build();

// For Navigating google.com, please enter a search.

driver.get('https://www.google.com/');

driver.findElement({name: 'q'}).sendKeys(webdriver1);

driver.findElement({name: 'btnG'}).click();

driver.wait(webdriver1.until.titleIs(webdriver1 - Google Search'), 1000);

// Take a screenshot of the results page. Save to disk.

driver.takeScreenshot().then(base64png => {

  fst.writeFileSync('screenshot.png', new Buffer(base64png, 'base64'));

});

driver.quit();

WebDriverIO is a critical level API on the top standard of Selenium WebDriver.

Install:

npm i --save-dev webdriverio chromedriver

Example 2:

filter CSS characteristics on chromestatus.com

const webdriverio = require('webdriverio');

const chromedriver = require('chromedriver');

const PORT = 9515;

chromedriver.start([

  '--url-base=wd/hub',

  `--port=${PORT}`,

  '--verbose'

]);

(async () => {

const opts = {

  port: PORT,

  desiredCapabilities: {

    browserName: 'chrome',

    chromeOptions: {args: ['--headless']}

  }

};

const browser = webdriverio.remote(opts).init();

await browser.url('https://www.chromestatus.com/features');

const title = await browser.getTitle();

console.log(`Title: ${title}`);

await browser.waitForText('.num-features', 3000);

let numFeatures = await browser.getText('.num-features');

console.log(`Chrome has ${numFeatures} total features`);

await browser.setValue('input[type="search"]', 'CSS');

console.log('Filtering features...');

await browser.pause(1000);

numFeatures = await browser.getText('.num-features');

console.log(`Chrome has ${numFeatures} CSS features`);

const buffer = await browser.saveScreenshot('screenshot.png');

console.log('Saved screenshot...');

chromedriver.stop();

browser.end();

})();

Full Stack Web Developer Course

To become an expert in MEAN StackView Course
Full Stack Web Developer Course

Run Selenium Tests on Docker

We can execute Selenium on the standalone or single container and a Grid of many containers.

  • Configure a Selenium Grid using Docker Compose: For instance, Zalenium is extensively utilized in this section as it implements a manageable and scalable container-based Selenium Grid, including video recording, live show, primary authentication, and dashboard.
  • We can configure Selenium Grid using Kubernetes. We can get the detailed documentation in Run Selenium UI tests using Docker containers.

Run Selenium Tests on Firefox Inside the Docker Container

For running the Selenium tests on Firefox inside the Docker container, use the below command:

docker pull selenium/standalone-firefox

Output:

SeleniumDocker_1.

We can download the container image for the particular version from the navigation to Docker Hub and can locate the particular image. For instance, the command docker will pull using selenium/standalone-chrome:87.0 and can download version 87.0 of Chrome as shown below.

/SeleniumDocker_2.

After downloading the images, run the command docker images to check if the downloading was performed successfully. As shown below, we have the newest versions of Chrome and Firefox containers available for usage.

SeleniumDocker_3

Advance your career as a MEAN stack developer with the Full Stack Web Developer - MEAN Stack Master's Program. Enroll now!

Conclusion

We hope this article helped you understand Selenium Docker. In this article, we discussed the concept of various repositories and selenium running the automated test cases using Google and Firefox browsers, along with examples that will be helpful to professional developers from Java and .net backgrounds, application architectures, cloud specialists, testers, and other learners looking to learn more about performing tests using the Selenium framework.

Besides pursuing varied courses provided by Simplilearn, you can also sign up on our SkillUp platform. This platform, a Simplilearn initiative, offers numerous free online courses to help with the basics of multiple programming languages, including cloud docker hub. You can also opt for our Full-Stack Web Development Certification Course to improve your career prospects.

About the Author

SimplilearnSimplilearn

Simplilearn is one of the world’s leading providers of online training for Digital Marketing, Cloud Computing, Project Management, Data Science, IT, Software Development, and many other emerging technologies.

View More
  • Disclaimer
  • PMP, PMI, PMBOK, CAPM, PgMP, PfMP, ACP, PBA, RMP, SP, and OPM3 are registered marks of the Project Management Institute, Inc.
  • *According to Simplilearn survey conducted and subject to terms & conditions with Ernst & Young LLP (EY) as Process Advisors