首页 > 其他分享 >How to run Selenium Tests in Docker

How to run Selenium Tests in Docker

时间:2022-10-13 20:48:00浏览次数:93  
标签:run Selenium selenium How command driver options

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

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

Running Selenium Tests on Docker

Upon rerunning the command docker images, selenium/standalone-chrome image appears in the list.

Selenium Tests on Docker

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.

Starting Docker Container

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.

Selenium Grid UI

 

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.

Running Selenium Tests in Docker

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 JenkinsCircleCIAzure PipelinesTravisCIBamboo, 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

相关文章

  • linux用户权限su sudo chmod shown
    linuxshell主要分为如下几类:   bashshell配置文件介绍(文件主要保存用户的工作环境)  登录系统后,华宁变量配置文件的应用顺序是?  说了这么多预备知识,这些和su命令切......
  • 实现Runnable接口
    packagedemo1;//创建线程方法2:实现Runnable接口,重写run方法,执行线程需要丢入Runnable接口实现类,调用start方法publicclassTestThread3implementsRunnable{@Ov......
  • pip install -r requirements.txt报错python setup.py egg_info did not run successf
    requirements.txt中内容:certifi==2018.1.18Django==2.0.2django-cors-headers==2.1.0djangorestframework==3.7.7idna==2.6pytz==2017.3requests==2.18.4django-rest-swa......
  • Scheduler.Run
    Scheduler.Run(procedure(constAScheduledEvent:IkbmMWScheduledEvent)beginend).SynchronizedAfterRun(procedure(constAScheduledEvent:IkbmMWSch......
  • 记录下linux命令关于chown
    题目:关于对其他用户(自己创建的user1)下的目录test及其下的所有文件的所有者改成bin,所属组改成daemon。一开始切换到user1下却操作不了修改文件为bin用户,chown-R也没用......
  • Android自动测试之monkeyrunner工具
    ​这篇博客的作者弄出来了,可我弄出来的结果不一样,郁闷其实是翻译过来的,原文:http://developer.android.com/tools/help/monkeyrunner_concepts.html        ......
  • Docker, containerd, CRI-O and runc之间的区别?
    1、概述在容器生态系统中充斥着各种各样的术语,今天就来为你,一一解码! Docker开启了容器的时代,但不久之后,工具、标准和首字母缩略词似乎在这一领域爆发。那么,“docker”......
  • Unity热更技术对比(Lua、ILRuntime、HybridCLR)
    热更技术原理:app+脚本解释器+脚本代码,动态执行最新代码,实现热更。解释器:Lua技术=Lua解释器+Lua脚本;C#=C#解释器+c#脚本Unity的热更方案:Lua解决方案(如ToLua,xLu......
  • java中Runtime类执行研究
    Runtime类在java中一般类的调用方法是new类();所以我们使用newRuntime()来实例化一个Runtime类的对象importjava.io.IOException;publicclassmain{public......
  • java.lang.RuntimeException: java.lang.RuntimeException: java.lang.ClassNotFoundE
    oozie报错:java.lang.RuntimeException:java.lang.RuntimeException:java.lang.ClassNotFoundException:Classorg.apache.oozie.action.hadoop.OozieLauncherOutputCo......