https://www.browserstack.com/guide/run-selenium-tests-in-docker
This tutorial uses the selenium/standalone-chrome image hosted by selenium on DockerHub.
Step 1: Pull the docker image
To get a list of all the already existing images on the system, run the following command in the command prompt:
docker images
If you do not already have the selenium standalone-chrome docker image, run the following command to download a copy of the image onto the system.
docker pull selenium/standalone-chrome
Upon rerunning the command docker images, selenium/standalone-chrome image appears in the list.
Step 2: Running the Selenium Webdriver Docker container
Upon pulling the selenium/standalone-chrome image onto the system, start the container by running the following command:
docker run -d -p 4444:4444 -v /dev/shm:/dev/shm selenium/standalone-chrome
The above command starts a container from the image specified in detached mode (background mode). It also maps Port 4444 on the container to Port 4444 on your local browser.
The command, when run, will return the ContainerID.
Open the browser and navigate to http://localhost:4444/. It reflects Selenium Grid UI, as shown below.
Step 3: Creating a sample test file
Selenium supports tests written in different languages of which Java and Python are most popularly used. In this example, using Python to create Selenium Test.
from selenium import webdriver
To open file in chrome browser, Chromedriver is necessary. Therefore, initializing ChromeOptions to declare any connection and driver options necessary while instantiating a browser instance.
options = webdriver.ChromeOptions() options.add_argument('--ignore-ssl-errors=yes') options.add_argument('--ignore-certificate-errors')
Creating an instance of the Remote webdriver and passing the selenium endpoint and chrome options defined in the previous step.
driver = webdriver.Remote( command_executor='http://localhost:4444/wd/hub', options=options )
Navigate to the BrowserStack website and click on the Get Started for Free button. Inducing wait time between the two actions allows viewing the execution of tests.
driver.get("https://www.browserstack.com/") driver.find_element_by_link_text("Get started free").click()
seleniumDockerTest.py
from selenium import webdriver import time print("Test Execution Started") options = webdriver.ChromeOptions() options.add_argument('--ignore-ssl-errors=yes') options.add_argument('--ignore-certificate-errors') driver = webdriver.Remote( command_executor='http://localhost:4444/wd/hub', options=options ) #maximize the window size driver.maximize_window() time.sleep(10) #navigate to browserstack.com driver.get("https://www.browserstack.com/") time.sleep(10) #click on the Get started for free button driver.find_element_by_link_text("Get started free").click() time.sleep(10) #close the browser driver.close() driver.quit() print("Test Execution Successfully Completed!")
Step 4: Executing the test case
Python test case file can be run using either an IDE or command prompt. To run it from the command prompt, open a command prompt and run the following command:
python <filename>
Navigate to the sessions tab on the Selenium Grid UI upon running the command. It would reflect an active session. Click on the video icon to check how automation works.
Note: If prompted for a password while opening the video, “secret” should be the default password.
The above steps allow running Selenium tests in Docker seamlessly.
Effective alternatives for testing in CI/CD
Testing on Real Devices helps eliminate the bottlenecks by testing under real user conditions, thus, delivering better results. Running parallel tests on real devices and browser versions, speeds up the development cycle. At the same time, it also widens the scope of test coverage.
Testing on a platform like BrowserStack, which provides access to a fleet of desktop browsers and real mobile devices is a good choice for Cross Browser & Platform Testing. One can also leverage the power of automation testing using BrowserStack Automate and App Automate to check cross-browser compatibility over the BrowserStack’s real device cloud, saving both time and cost incurred.
Run Tests on Real Devices for Free
However, BrowserStack does not support Docker, which is why one can explore alternatives for a seamless Selenium testing experience in DevOps.
To run Selenium Tests in CI/CD pipeline, integration with cloud-based testing platforms like Browserstack along with CI/CD tools like Jenkins, CircleCI, Azure Pipelines, TravisCI, Bamboo, etc. can be efficient. This will help devs and testers build high-quality applications to retain and delight users through its seamless user experience.
Read More: How to run parallel tests with CircleCI
Best Practices of Running Selenium Tests in Docker
Running multiple containers for various tests on the same server can get uncoordinated if not managed properly. The underlying hardware will be blocked unnecessarily, thus defeating the whole purpose of using containers due to rampant wastage of resources.
Here are a few best practices to follow for the best use of containers while testing:
- Use containers as microservices: Setting up mini docker containers having a single application/instance will help in reducing the size of the containers. It also brings about the possibility of mixing and matching various containers together to achieve maximum coverage. For example, Instead of creating a single container for Web Application, Selenium WebDriver, and the Operating System, it would be helpful to have each of them as a separate container.
- Setup and tear down test environment for each run: This helps maintain the sanity of the environment and avoids cache and temp files from previous runs to affect the results of the current run.
- Parallel testing: Running tests parallelly can help save time and make the best use of the underlying hardware.
To conclude, delivering high-quality software in record time is the need of the hour, due to fast-growing user expectations and strong market competition. CI/CD has been pivotal in setting up software development lifecycles, where new features are constantly updated at maximum levels of speed and efficiency.
This allows end-users to access new features, without the need to download software updates, giving them a seamless experience. Given the short span of the release cycle, testing has to be scaled for delivering bug-free software in the said time frame. This is where Selenium plays a pivotal role of providing test automation in the CI/CD pipeline.
Through Selenium automation testing, parallel tests can be performed at the required scale, while Docker helps maintain the sanity of the test environment with its ephemeral containers.
标签:run,Selenium,selenium,How,command,driver,options From: https://www.cnblogs.com/alamZ/p/16789558.html