Test Automation with Selenium, Appium and Jenkins
If you work in software development, you have probably thought “I am running the same tests again” or “there has to be a more efficient way to do this.” I certainly have, more than once.
That is where test automation comes in. Selenium and Appium let you hand work you were doing by hand over to a script. Add Jenkins and you have an environment that keeps running those tests for you. Add JMeter and you can automate load testing as well.
This article covers the basics of test automation, organised around the things I wish I had known before I started.
Why automate tests at all?
Running everything manually produces the same four complaints, over and over:
- Repeating identical steps every time is tedious
- Small changes force you to rewrite test cases
- You want to eliminate human error
- You want to develop faster
I used to spend hours running tests by hand before it occurred to me how much easier this would be automated. Once I started, the repetitive work disappeared and I could concentrate on development.
How do you automate web app tests with Selenium?
Selenium automates browser operations for web applications. Clicking buttons, filling in forms, moving between pages — anything you were doing by hand can be reproduced in a script.
Typical things worth automating:
- Search: enter a keyword in the search form, then check that the results display correctly
- Login: enter a username and password, press the login button, and confirm you land on the right page
- Shopping cart: add an item to the cart and confirm the total is calculated correctly
What do you need to run Selenium?
| Requirement | Details |
|---|---|
| Programming language | Python or Java |
| Selenium WebDriver | The driver that controls the browser |
| Target browser | Chrome, Firefox, Edge and others |
With Python the setup is a single command:
pip install selenium
Here is a script that types a keyword into Google’s search box and searches:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
# Launch Chrome
driver = webdriver.Chrome()
# Open Google
driver.get("https://www.google.com")
# Find the search box and type a keyword
search_box = driver.find_element("name", "q")
search_box.send_keys("Selenium automation")
search_box.send_keys(Keys.RETURN)
# Wait for the results to appear
driver.implicitly_wait(5)
# Close the browser
driver.quit()
Run this and Chrome starts on its own and performs the search. Watching it move by itself the first time is genuinely satisfying.
How do you automate mobile app tests with Appium?
Appium automates UI testing for mobile apps on iOS and Android. It runs on the same WebDriver protocol as Selenium, so if you are comfortable with Selenium, picking up Appium is straightforward.
Things you can test with it:
- Login screen: launch the app, enter an ID and password, press login, and confirm the home screen appears
- Button behaviour: confirm that tapping a button opens the correct screen
Here is an example in Python:
from appium import webdriver
# Connect to the Appium server
capabilities = {
"platformName": "Android",
"deviceName": "emulator-5554",
"app": "/path/to/app.apk"
}
driver = webdriver.Remote("http://localhost:4723/wd/hub", capabilities)
# Tap a button
button = driver.find_element("id", "com.example.app:id/login_button")
button.click()
# Check the result
assert "Home" in driver.page_source
# Finish
driver.quit()
That automates tapping the login button and verifying where it takes you.
How does Jenkins turn automated tests into a system?
Jenkins is what turns a set of scripts into something that runs without you. The flow looks like this:
- A developer pushes code to GitHub
- Jenkins detects the change
- Jenkins runs the Selenium or Appium tests automatically
- Results are sent to Slack or by email
Configuring a Jenkins job with a shell script like this is enough to run the Selenium tests:
#!/bin/bash
cd /path/to/test-scripts
pytest test_selenium.py
From then on the tests run whenever the code changes, without anyone starting them.
How do you automate load testing with JMeter?
Suppose you want to know what happens when 1,000 people hit an API at the same time. JMeter makes that scenario simple to build.
An example configuration:
- Number of requests: 1,000
- Concurrent connections: 50
- Delay: 1 second
JMeter then produces a report like this:
| Metric | Result |
|---|---|
| Average response time | 250 ms |
| Maximum response time | 1.2 s |
| Error rate | 0.5% |
If the maximum response time came back at three seconds or more, that is your signal that the server is struggling.
Summary
This article covered test automation with Selenium, Appium, Jenkins and JMeter.
Setting up the environment is the hard part. Once the system is in place, everything after it gets easier. If any of this sounds useful for your situation, it is worth trying.