
Hello.
It's no secret that application testing is an important step in software development, and if you are developing web applications, then you just need to test the web interface. Fortunately, for this purpose there is such a tool as
Selenium . The well-known companies of
SKB Kontur and Yandex have already chosen it as a tool for the functional testing of their applications and services (Yandex talked about this at the recent YaC).
')
Now to the point.
In one of the projects I'm working on, we used
Selenium starting from the first branch. But time does not stand still, Selenium 2.0, based on the
webdriver technology,
came out , more functional, convenient, and correctly simulating browser events (
more about the advantages ).
In order not to engage in updating the old
Selenium support code within our application (what if you have to do this more than once?), And also to try to do something useful for other developers, we decided to immediately bring the integration with Selenium into a separate library.
Next, I will describe what we did and how to use this library.
Installation
You can get the library from
https://github.com/dragoon/django-selenium or install via pip:
pip install django-selenium
Opportunities
First of all, the library allows you to integrate with the
django testing subsystem. You can either simply specify in the settings to use TestRunner from the library:
TEST_RUNNER = 'django_selenium.selenium_runner.SeleniumTestRunner'
either inherit it and write your class.
Behind the scenes,
SeleniumTestRunner performs the following operations:
- starts the server selenium-server.jar
- starts a test server instance with test fixtures
Next, in order to start writing Selenium tests, you need to create a
seltests.py file in the application, similar to the usual
tests.py . Then you simply inherit the test class from
django_selenium.testcases.SeleniumTestCase and write tests using Selenium commands to interact with the browser.
Standard driver extension
To facilitate the implementation of standard operations, an extended driver class has been written that interacts with the browser,
django_selenium.testcases.MyDriver . It contains such operations as open a url, log in to the site (standard django form), find an item by css selector, find an item and click, check for text on the page, get the alert text and others. For details, please refer to the source code of the
MyDriver class for
now .
Replacing the test execution command
Finally, to replace the standard django test execution command with a command with selenium test support, you need to inherit the command class somewhere in your application as follows:
from django_selenium.management.commands import test_selenium class Command(test_selenium.Command): def handle(self, *test_labels, **options): super(Command, self).handle(*test_labels, **options)
The command supports two additional options:
- --selenium - perform all tests, including Selenium tests
- --selenium-only - perform only Selenium tests
As in the case of a standard command, you can run Selenium tests for a specific application or only a specific test from an application.
Test application
To consolidate the above, I prepared a small django application containing one selenium test:
If everything went right, the following picture should be observed:

As always, I will be glad to hear comments and suggestions for further refinement, opinions about how useful this library is, and of course I will be happy with any help for refinement from other githubbers.
Thanks for attention.