How to set up a Selenium test on standalone Chrome in a Docker container?

201    Asked by ConnorPeake in QA Testing , Asked on Jun 21, 2024

 I am currently engaged in a particular task that is related to working as a QA engineer at a software company that can develop a web-based application. My team is responsible for automating the testing of the application to ensure it functions correctly across the different web browsers. Therefore, I used the selenium WebDriver for this particular purpose. Recently my team has decided to use a docker container with standalone chrome for running these tests in a continuous integration/continuous development pipeline. How can I do so? 

Answered by David

In the context of selenium, Here are the steps given:-

Configuring docker environment

You can pull the Selenium standalone Chrome image from the docker hub by using the command “docker pull Selenium/standalone Chrome”.

Modifying the selenium WebDriver code

You should update the WebDriver initialization coding to connect to the remote WebDriver service.

Try to ensure that the test scripts use this remote driver Instance to run the test on the standalone Chrome container.

Ensuring proper logging

You can implement logging with your test script to capture the test execution details. You can use Python’s logging module for this.

You can save the screenshot and HTML dumps for failed tests for the purpose of helping with debugging.

Here is the Python structure given:-
# Docker Setup (Terminal Commands)
# Pull Selenium standalone Chrome image
# docker pull selenium/standalone-chrome
# Run Docker container
# docker run -d -p 4444:4444 –name selenium-chrome selenium/standalone-chrome
# Selenium WebDriver Code
From selenium import webdriver
Import logging
# Configure logging
Logging.basicConfig(filename=’test.log’, level=logging.INFO)
# Chrome WebDriver options
Chrome_options = webdriver.ChromeOptions()
# Remote WebDriver connection
Driver = webdriver.Remote(
    Command_executor=’http://localhost:4444/wd/hub’,
    Options=chrome_options
)
# Example test script
Try:
    Driver.get(‘https://www.example.com’)
    Assert ‘Example Domain’ in driver.title
    Logging.info(‘Test Passed: Title contains “Example Domain”’)
Except AssertionError:
    Logging.error(‘Test Failed: Title does not contain “Example Domain”’)
    Driver.save_screenshot(‘screenshot.png’)
    With open(‘page_source.html’, ‘w’) as f:
        f.write(driver.page_source)
finally:
    driver.quit()

Here is the java coding structure given:-

Import org.openqa.selenium.WebDriver;
Import org.openqa.selenium.chrome.ChromeOptions;
Import org.openqa.selenium.remote.RemoteWebDriver;
Import java.net.URL;
Import java.util.logging.FileHandler;
Import java.util.logging.Logger;
Import java.util.logging.SimpleFormatter;
Public class SeleniumDockerExample {
    Public static void main(String[] args) {
        // Docker Setup (Terminal Commands)
        // Pull Selenium standalone Chrome image
        // docker pull selenium/standalone-chrome
        // Run Docker container
        // docker run -d -p 4444:4444 –name selenium-chrome selenium/standalone-chrome
        // Selenium WebDriver Code
        WebDriver driver;
        ChromeOptions options = new ChromeOptions();
        // Remote WebDriver connection
        Try {
            Driver = new RemoteWebDriver(new URL(http://localhost:4444/wd/hub), options);
            // Configure logging
            Logger logger = Logger.getLogger(“SeleniumTests”);
            FileHandler fh = new FileHandler(“test.log”);
            Logger.addHandler(fh);
            SimpleFormatter formatter = new SimpleFormatter();
            Fh.setFormatter(formatter);
            // Example test script
            Try {
                Driver.get(https://www.example.com);
                If (driver.getTitle().contains(“Example”)) {
                    Logger.info(“Test Passed: Title contains ‘Example’”);
                } else {
                    Throw new AssertionError(“Test Failed: Title does not contain ‘Example’”);
                }
            } catch (Exception e) {
                Logger.severe(“Exception: “ + e.getMessage());
                // Save screenshot and HTML dump for failed tests
                // driver.saveScreenshot(“screenshot.png”);
                // Files.write(driver.getPageSource(), new File(“page_source.html”), Charset.forName(“UTF-8”));
            } finally {
                Driver.quit();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}


Your Answer

Interviews

Parent Categories