How to Automate Testing Using Selenium WebDriver, Jenkins, and Allure
Before writing your tests and being able to execute them on a nightly basis, there are several prerequisites which need to take into consideration.
Join the DZone community and get the full member experience.
Join For Freein order to create automated testing in a continuous integration (ci) environment with the objective of creating a continuous testing (ct) flow, you will need to familiarize yourself with a series of automation and testing tools. in this post, i’ll look at how to combine and leverage these tools to implement and automate testing in ci and cd.
before we dive in, just a note that to better understand the rest of this blog post, you should already be familiar with the basics of programming languages and unit tests .
there are lots of tools and techniques that can be integrated into a ci environment. here at blazemeter, we are using selenium webdriver python and we integrate it with jenkins . the testing framework we are working with is pytest , which makes it easy to write small tests, yet scales to support complex functional testing for applications and libraries.
before writing your tests and being able to execute them on a nightly basis, there are several prerequisites which need to take into consideration.
prepare your environment
in order to prepare your environment for testing, you should perform the following steps.
1. choose your browser
we will focus on chromedriver as a proof of concept, though there are other desktop browsers available out there such as firefoxdriver, internetexplorerdriver, safaridriver, operadriver, and phantomjs. download chromedriver and store it under the global /bin folder. in our case, it will be /python27/bin.
2. pick your programming language & ide
we will focus on python since this is the language we mostly use in blazemeter, but you may choose any language you desire as long as webdriver has bindings to it.
- more info on the languages selenium webdriver supports can be found here .
- additional info on python can be found here .
the relevant ide (integrated development environment) for our purposes would be pycharm .
3. dive into selenium webdriver
selenium webdriver is one of the leading open-source tools for automated testing. generally speaking, webdriver is one of the top gui testing tools there is. if you haven't heard about webdriver yet, this is your time to deep dive in order to utilize a high-quality automated testing and a stable product. additional info on webdriver can be found here .
4. decide which testing framework you want
depending on the programming language that you have chosen, the next step would be to choose the testing framework. we will focus on py.test since this is the testing framework with which we are working in blazemeter.
of course, there are many other testing frameworks from which to choose from.
in this post, we will describe tests created via py.test, which interprets the test.py file and parses only the functions that contain ‘test’ in them. additional info about how to write tests under pytest convention can be found here.
create your tests
once you have prepared your environment for automated tests, go ahead and create your first end-to-end (e2e) flow. keep in mind that your tests should be as quick and short as possible in order to prepare for frequent changes in the code (i.e., log in to your website and verify the url once signed in).
make sure to follow the py.test structure to run your tests as expected.
here is a short example code for a small test using webdriver python vs. pytest:
login_scenario.py
import tests.tests as tests
# test step 1 - open url
def test_open_url(driver, url):
tests.open_url(driver, url)
# test step 2 - add credentials
def test_add_credentials(driver, username, password):
tests.add_credentials(driver, username, password)
# test step 3 - submit form
def test_submit_form(driver):
tests.submit_form(driver)
# test step 4 - verify url
def test_verify_url(driver, url):
tests.verify_url(driver, url)
tests.py
from selenium.webdriver.common.by import by
page = "page"
username_el = "username"
password_el = "password"
submit = "//input[@type='submit']"
expected_url = "my/"
def open_url(driver, url):
driver.get(url)
driver.find_element(by.id, page)
def add_field(driver, value, field):
username_field = driver.find_element(by.name, field)
username_field.clear()
username_field.send_keys(value)
def add_credentials(driver, username, password):
add_field(driver, username, username_el)
add_field(driver, password, password_el)
def submit_form(driver):
driver.find_element(by.xpath, submit).click()
def verify_url(driver, url):
assert (url + expected_url) == driver.current_url
conftest.py
import pytest
from selenium import webdriver
def pytest_addoption(parser):
parser.addoption("--driver", action="store", default="chrome", help="type in browser type")
parser.addoption("--url", action="store", default="https://qa.moodle.net/", help="url")
parser.addoption("--username", action="store", default="manager", help="username")
parser.addoption("--password", action="store", default="test", help="password")
@pytest.fixture(scope="module", autouse=true)
def driver(request):
browser = request.config.getoption("--driver")
if browser == 'chrome':
browser = webdriver.chrome()
browser.get("about:blank")
browser.implicitly_wait(10)
browser.maximize_window()
return browser
else:
print 'only chrome is supported at the moment'
@pytest.fixture(scope="module")
def username(request):
return request.config.getoption("--username")
@pytest.fixture(scope="module")
def password(request):
return request.config.getoption("--password")
@pytest.fixture(scope="module")
def url(request):
return request.config.getoption("--url")
you can reference the full github test repository here .
run your tests locally
since we have created the tests under py.test convention, the correct way to run them in your local environment would be:
py.test test_name.py
the results should be something like this:
run your tests via jenkins
as an extensible automation server, jenkins can be used as a simple ci server or turned into the continuous delivery hub for any project .
note: it is best to run your tests in a pre-configured virtual environment which installs requirements.txt prior to implementing them in jenkins.
- create a virtual environment - information on how to create a virtual python environment and its usage can be found here
- export project requirements - additional information on how to export all relevant installed modules to requirements.txt can be found here .
install the allure reports plugin to jenkins
allure is an open source framework designed to create test execution reports with good representation of the test execution output. the allure plugin allows you to automatically generate an allure report and attach it to any build during a jenkins job run. install the plugin and then let’s start working with jenkins.
create a new jenkins job
the jenkins job you are now going to create should include all of the steps mentioned above, including:
- create virtual environment
- install requirements.txt
- activate virtual environment
- run tests
- deactivate virtual environment
create a new jenkins job with the following shell script:
#!/bin/bash
echo '#### create virtual environment ####'
virtual_env_name='virtual-environment'
virtualenv $virtual_env_name
echo '#### activate virtual environment ####'
source $virtual_env_name/bin/activate
echo '#### install requirements ####'
pip install -r ./gui_automation/requirements.txt
echo '#### run tests ####'
py.test --alluredir=../../allure-results scenarios/login_scenario.py
echo ### deactivate virtual environment ###
deactivate
as seen in the screenshot below:
view your test results in allure reports
after successfully integrating your project into jenkins, the next challenge is to export the test results to a readable user friendly report. allure allows to inject environment variable and to provide statistics on tests statuses.
it also has the option of embedding images, xml, json, txt, mp4 and many more attachments.
the execution of the py.test suits will be done as follows:
py.test --allure-dir="path_to_dir" login_scenario.py
allure has lots of cool features and parameters which can be embedded into the live report such as screenshots, logs files, and much more.
schedule jobs on a nightly basis
be sure to verify that the ci environment is configured to the latest release on a nightly basis. additional information on how to configure a jenkins job to run on a specific schedule can be found here .
congratulations! all of your tests are now configured in jenkins. all that’s left is to schedule these tests to run automatically on your ci environment which should be with the latest build on a nightly basis.
in the next guide, i will explain how to create a master job in jenkins that triggers all these jobs that you have created.
next steps
i hope that this tutorial assisted you in achieving your goals.
note: while this post was focused on unit and functional testing, jenkins - combined with open source tools like apache jmeter and taurus , and of course blazemeter - is also a great tool for continuous performance testing as well. if you haven’t already, i encourage you to read more about how to run jmeter with jenkins , running a jmeter test via jenkins pipeline , or how to easily automate performance tests in jenkins .
blazemeter is also hosting an upcoming webinar that might be valuable for you - ensuring test maintenance & productivity in the ci pipeline .
do not hesitate to approach me if you have any feedback, questions or other concerns.
Published at DZone with permission of Eliran Shani, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments