Hello!
Today I want to share our experience in automating web application testing using Selenium WebDriver and the Behave framework.
If interested, welcome under cat.
Before the start
Here I will not dwell on how to search for elements, and how to interact with them with the help of selenium webdriver, but I’ll only talk about how you can easily and conveniently write and run tests written in python using behave.
1. Installing a need
First we need python. You can download it
here . I will use version 2.7.6, and you can use the one that you like best. In addition to python, we also need selenium and behave itself.
You can install selenium using
pip :
pip install selenium
')
Now install the behave. You can use pip:
pip install behave
And you can download here:
behave on pypiWhat is behave? Behave is a framework for programming through python-style behavior (
What is Behavior-driven development? ).
Behave uses tests written in natural language, with logic in python.
He has several serious advantages:
+ Easy installation
+ Reports in junit format
+ Tests can write anyone in natural language.
+ There is the possibility of integration with jenkins (with other CI, probably, too)
Compare behave with similar tools.When everything you need is installed, you can start setting up the environment for writing and executing tests.
2. Customization
The directory from which we will run the tests should contain:
1. Files with the .feature extension are the tests themselves.
2. The steps directory, which contains .py files with a description of the steps from which tests are composed, in python.
Optionally, you can add to the directory the folder containing the folder steps file - environment.py with a code that will need to be executed before or after certain events in the test.
Detailed description of this file
here .
Our example will be as simple as possible, so we will not include this file.
Total, what our directory looks like in the minimum configuration:
features /
features / everything.feature
features / steps /
features / steps / steps.py
example from behave tutorialThat's the whole setup, you can write the first test!
3. Writing and running the first test
First we need to create a folder in the folder with a file .py, in which we describe the logic of our test.
Let's name this file first_test.py.
For example, I decided to take a simple case of testing the search form ya.ru.
So copy the code below into the firs_test.py file.
If you are too lazy to watch the code, then the logic is as follows:
1. Open ya.ru
2. Click on the "Find" button
3. Check that there is a text on the issue page: “An empty search query is set”
Now that the test logic is ready, you can write the test itself.
In the folder with the steps directory, create a .feature file in which we use the written steps:
# Feature: Checking search # ( ) Scenario: heck some text in search results # . Given website "ya.ru" Then push button with text '' Then page include text ' '
The steps from the test, by decorators will be associated with the python functions. (Decorators marked with @)
Now let's run our test.
Go to the folder where it lies, and execute the command:
behave -i first_test.feature
The -i flag indicates that it is necessary to run only files matching the search pattern.
If we run it with the --junit flag, we get a report in junit format.
If everything went well, the report would be approximately as follows:
Feature: Checking search # first_test.feature:1 Scenario: Send some text in search box and check some text in search results # ya.feature:2 Given website "ya.ru" # steps/test.py:9 0.662s Then push button with text '' # steps/test.py:14 0.242s Then page include text ' ' # steps/test.py:22 1.009s 1 feature passed, 0 failed, 0 skipped 1 scenario passed, 0 failed, 0 skipped 3 steps passed, 0 failed, 0 skipped, 0 undefined Took 0m1.913s
Conclusion
Here I brought only the most basic.
Features of selenium are incalculable, and behave knows a lot of things. This includes junit reports and jenkis integration.
Thank you for your attention, and have a great weekend!
List of materials used
Selenium:
habrahabr.ru/post/152653habrahabr.ru/post/248559www.seleniumhq.org/docsselenium2.ru/docs.htmlselenium-python.readthedocs.org/index.htmlselenium.googlecode.com/svn/trunk/docs/api/py/index.htmlBehave:
pythonhosted.org/behavePython:
pypi.python.orgpython.orgUPD: If something in the article is not very well written, or something is missing, then you can comment in the comments, or write to me in person, and together we will improve it.