So, you set out to debug your web application. You want the browser to “stick it yourself” on the buttons, follow the links and also check the status of the elements. In a word, you want integration testing, so that a certain script sets up a script of user actions while at the same time checking the state of elements and changes in the database.
Suppose you find or have already heard somewhere, or even have long wanted to try a special toolkit for these cases, called Selenium WebDriver. You go to the Selenium website and joyfully jump in delight: your favorite Python scripting language is supported! But alas, as in the case of Django, developers have not yet reached out to Python 3.x. So all the tasty pieces of refined sugar, as well as native support for UTF-8 are not supported by default.
However, armed with knowledge of the differences between Python 2 and 3, as well as the standard utility 2to3.py, we will overcome in two ways everything that hinders our happiness and overall progress in the development of test scripts for integration testing.
First of all, you will need to download the actual WebDriver Selenium for the old and not always good Python 2+, so as not to roam the site and download anything unnecessary,
here is the download page link , there you only need one tar.gz file of the type “selenium - <version> .tar.gz ".
')
In general, the idea is to copy the selenium folder contained in this archive from there to the Python3x \ Lib folder, apply 2to3.py to it, and then it will be necessary to slightly correct one file: “remote_connection.py”. The fact is that in the old bad Python 2.x, almost as in C / C ++, the string was not at all different from the byte array, i.e. in essence, no distinction was made between a character and a byte. Accordingly, to this day in old Python applications, various network libraries send and receive strings (!) Instead of an array of bytes over the network. Two functions will help us cope with this misfortune: encode () at the string and decode () at the byte array, respectively.
In principle, I have already written a script for a long time to automate this conversion, of course, all on the same Python 3, and even put it in my Issue on supporting Python 3 in the Selenium development team (this topic was created by me even earlier, since the visible progress towards support for the third python is not planned). Link to the script:
pyselenium.py , link to the topic on Python 3.x support for the Selenium web driver:
Support Python 3.x for Selenium WebDriver .
The pyselenium.py script accepts the tar.gz file that you downloaded at the very beginning, the script takes the path to the archive as an argument, and then copies the selenium to the Python3x \ Lib folder and converts the web driver to work in accordance with Python 3 and strings in UTF-8. In fact, in the script, first run the 2to3.py utility, then insert the lines encode () into the right places before sending them over the network, and then decode () to the line the received data.
In principle, the script is quite readable, short and clear, thanks for that to the Python language. All that the script needs to work with is the Python 3.x interpreter and the actual file with the archive.
Link to readable page with script code.You can check it using interactive python mode:
>>>from selenium.webdriver import Firefox
>>>driver = Firefox()
>>>driver.get('http://ya.ru/')
Our brother, the Fire Fox, should appear and upload the indicated site to us.
Further use of the driver depends only on your imagination.
Read more about working with Selenium WebDriver here:
documentation for Selenium Webdriver