在Docker中使用Python Selenium和Headless Chrome进行网站自动化测试的方法

by Joyz

通过乔伊斯

在Docker中使用Python Selenium和Headless Chrome进行网站自动化测试的方法 (A recipe for website automated tests with Python Selenium & Headless Chrome in Docker)

The QA team leads bug catching, but manual testing is not scalable when your company takes on more projects. Since my company sends builds every two weeks, the QA team wants to test every build before we pass them to our clients.

质量检查团队负责捕获错误,但是当您的公司承担更多项目时,手动测试就无法扩展。 由于我的公司每两周发送一次构建,因此质量检查团队希望在将每个构建传递给客户之前进行测试。

To improve QA, I’ve helped modify project management processes, recruited a team of exploratory testers, and built automated testing tools for continuous integration as a QA Engineer. Most of the tools I build use open-source or free libraries.

为了提高质量保证,我帮助改进了项目管理流程,招募了一批探索性测试人员,并构建了自动测试工具以实现持续集成,从而成为一名质量检查工程师。 我构建的大多数工具都使用开源或免费库。

Below is a guide for my open-source Github repo with 100,000+ docker image pulls to help development teams and freelance developers set up their own automated tests.

以下是我的开源Github存储库的指南,其中包含100,000多个Docker镜像,可帮助开发团队和自由开发人员设置自己的自动化测试。

我们的任务 (Our Task)

We will go through the process step-by-step to see how to set up a test with Selenium, which automates browsers to perform tests. In this example, we will use headless Chrome to load our website and perform a simple click on the button we want to test on the site.

我们将逐步完成该过程,以了解如何使用Selenium设置测试,该测试可自动使浏览器执行测试。 在此示例中,我们将使用无头Chrome浏览器加载我们的网站,然后简单地单击要在该网站上测试的按钮。

设置无头Chrome (Setting up the Headless chrome)

Starting up a Chrome browser in Docker to run a Selenium test takes just a minute. Once it works, it works with any automated CI builds. Here is an example: First, open your terminal and go to your working directory.

在Docker中启动Chrome浏览器以运行Selenium测试仅需一分钟。 一旦起作用,它就可以与任何自动化CI构建一起使用。 这是一个示例:首先,打开终端并转到工作目录。

$ cd [your working directory]

Then pull and run this docker image from joyzoursky/python-chromedriver. We will run the Selenium test inside the Docker container.

然后从joyzoursky / python-chromedriver拉并运行此docker映像 。 我们将在Docker容器中运行Selenium测试。

$ docker run -it -v $(pwd):/usr/workspace joyzoursky/python-chromedriver:3.6-alpine3.7-selenium shUnable to find image 'joyzoursky/python-chromedriver:3.6-alpine3.7-selenium' locally3.6-alpine3.7-selenium: Pulling from joyzoursky/python-chromedriverff3a5c916c92: Pull complete471170bb1257: Pull completed487cc70216e: Pull complete9358b3ca3321: Pull complete78b9945f52f1: Pull complete66eb40d9fb29: Pull complete36cb996dbd54: Pull complete8e6f0ca23b1a: Pull completed5a3895f190c: Pull completeDigest: sha256:c51c240f1a472b0f252e96cd39678c7d039b757b83e46bf8ed182e95caaf02e7Status: Downloaded newer image for joyzoursky/python-chromedriver:3.6-alpine3.7-selenium

Now the container is ready. Let’s move to the workspace and try out the code.

现在容器已准备就绪。 让我们转到工作区并尝试代码。

/ # cd /usr/workspace/

现在,我们可以编写测试脚本 (Now, we can script our test)

Let’s start Python.

让我们启动Python。

/usr/workspace # pythonPython 3.6.4 (default, Jan 10 2018, 05:20:21)[GCC 6.4.0] on linuxType "help", "copyright", "credits" or "license" for more information.>>>

Before trying the code, import the Selenium webdriver from the pre-installed package.

在尝试代码之前,请从预安装的软件包中导入Selenium Webdriver。

>>> from selenium import webdriver

Then let’s start the headless Chrome. Some options are required to pass to the driver to avoid crashing during startup.

然后,让我们启动无头的Chrome。 传递给驱动程序时需要一些选项,以避免在启动过程中崩溃。

>>> chrome_options = webdriver.ChromeOptions()>>> chrome_options.add_argument('--no-sandbox')>>> chrome_options.add_argument('--window-size=1420,1080')>>> chrome_options.add_argument('--headless')>>> chrome_options.add_argument('--disable-gpu')>>> driver = webdriver.Chrome(chrome_options=chrome_options)

Now the browser is already opened in the container, but we cannot see it. Let’s try to go to this website and check the inner text of the top right button.

现在浏览器已经在容器中打开,但是我们看不到它。 让我们尝试转到该网站并检查右上角按钮的内部文本。

>>> driver.get('https://www.oursky.com/')>>> el = driver.find_element_by_class_name('btn-header')>>> el.text'START YOUR PROJECT'

Let's find the element we wish to interact with, for example the button in the header “btn-header”. Got it! Now, let's try to trigger a click on the button.

让我们找到我们希望与之交互的元素,例如标题“ btn-header”中的按钮。 得到它了! 现在,让我们尝试触发按钮的点击。

>>> el.click()>>> driver.current_url'https://oursky.com/enquiry/general/'

Success! The driver goes to the expected URL after clicking the button. You can now run your scripts in the container, or use the image in a CI build script. You can also build your own image with more pip packages installed, so you can automate more powerful tests. Enjoy! You may find the GitHub repository of the docker image here with the testing environment set up.

成功! 单击按钮后,驱动程序转到所需的URL。 现在,您可以在容器中运行脚本,或在CI构建脚本中使用映像。 您还可以安装更多pip软件包来构建自己的映像,从而可以自动化更强大的测试。 请享用! 您可以在此处找到设置了测试环境的Docker映像的GitHub存储库 。

Also take a look at the full Python Selenium script example, so you can customize it into your own test.

还要查看完整的Python Selenium脚本示例 ,因此您可以将其自定义为自己的测试。

I work for the Hong Kong and Taiwan-based software development company Oursky. We build digital products for clients and developer tools like our opensource BaaS, Skygear, which helps developers build apps faster.

我在香港和台湾的软件开发公司Oursky工作 。 我们为客户和开发人员工具(例如开源BaaS, Skygear) 构建数字产品 可帮助开发人员更快地构建应用程序。

翻译自: https://www.freecodecamp.org/news/a-recipe-for-website-automated-tests-with-python-selenium-headless-chrome-in-docker-8d344a97afb5/

你可能感兴趣的:(项目管理,selenium,java,python,软件测试)