Oursky Code
  • AI
  • Auth
  • More from Oursky
    • Oursky Business Blog
    • GitHub
    • About Us
  • AI
  • Auth
  • More from Oursky
0
Subscribe
Oursky Code
Oursky Code
  • AI
  • Auth
  • More from Oursky
    • Oursky Business Blog
    • GitHub
    • About Us
  • Software Testing

A recipe for website automated tests with Python Selenium & Headless Chrome in Docker

  • March 30, 2018
  • One comment
  • 2 minute read
  • Joyz Ng
carmen female developer
Total
0
Shares
0
0
0

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.

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.

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.

$ cd 

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

$ docker run -it -v $(pwd):/usr/workspace joyzoursky/python-chromedriver:3.6-alpine3.7-selenium sh
Unable to find image 'joyzoursky/python-chromedriver:3.6-alpine3.7-selenium' locally
3.6-alpine3.7-selenium: Pulling from joyzoursky/python-chromedriver
ff3a5c916c92: Pull complete
471170bb1257: Pull complete
d487cc70216e: Pull complete
9358b3ca3321: Pull complete
78b9945f52f1: Pull complete
66eb40d9fb29: Pull complete
36cb996dbd54: Pull complete
8e6f0ca23b1a: Pull complete
d5a3895f190c: Pull complete
Digest: sha256:c51c240f1a472b0f252e96cd39678c7d039b757b83e46bf8ed182e95caaf02e7
Status: 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.

/usr/workspace # python
Python 3.6.4 (default, Jan 10 2018, 05:20:21)
 on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>

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

>>> 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_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.

headless chrome driver
Testing Oursky’s website
>>> 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.

>>> 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.

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

Automated website tests oursky
Oursky builds digital products for clients and developer tools like our opensource BaaS, Skygear, which helps developers build apps faster.

Read more from Oursky

Total
0
Shares
Share 0
Tweet 0
Pin it 0
Related Topics
  • headless chrome in docker
  • Python Selenium
  • website automated tests
Joyz Ng

Previous Article
discourse forum deployed on kubernetes k8s
  • Opensource

How to set up an internal team forum in half a day using Discourse

  • January 29, 2018
  • Ben Cheng
View Post
Next Article
oursky opensource github
  • Android
  • Opensource

An Opensourced Recipe for Intializing Redux x Android Native Apps

  • May 9, 2018
  • David Ng
View Post
You May Also Like
View Post
  • Skygear
  • Software Testing

Catch bugs systematically: how to build a GitLab CI testing pipeline in 4 steps

  • August 15, 2017
  • Joyz Ng
Oursky QA Team
View Post
  • Geek
  • Software Testing

Software Testing World Cup 2016 Recap: 7 Critical Learnings You May have Missed

  • September 2, 2016
  • Joyz Ng
View Post
  • Android
  • iOS
  • Software Testing

Journey Through Agile Test Automation

  • May 6, 2016
  • Joyz Ng
1 comment
  1. software testing says:
    November 20, 2018 at 6:41 pm

    Nice post! Found your post very informative to read. I can’t wait to see your post soon. This article is really very useful and effective. I really appreciated to you on this quality work. Thanks a million and please keep up the effective work.

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Subscribe Us

Join our mailing list to never miss an update!

Oursky Code Blog
A team of Developers, Designers and Geeks. All about hacking and building things.

Input your search keywords and press Enter.