Settings and helpful functions Selenium + CEF + Python
I joined Voicemod a few months ago. Voicemod allows you to modify your voice in real-time either to play games or to create content in an original way.
Here’s a video of how it works.
An application that uses CEF
On a technical level, the most striking thing about Voicemod is that it is not a native Windows application. What we have is a front that communicates with a back using CEF (Chromium Embedded Framework). That is, an application that inside is like a web and connects to Voicemod servers.
The first challenge was how to interact with the app in order to start automating it.
Having CEF allowed us to interact with the app as if it was a web, so the decision about which tool to use was clear: Selenium.
We decided to use Python as a programming language because of the speed of execution that it provides. However, when looking for information, almost everything in Selenium is related to Java so I hope this article can help to have the information in one place.
Setting up the environment
To write the tests we use PyCharm, the Community version.
We use Python 3.8 and Selenium 3.141.0
Create a project in Pycharm
When we create a new Python project it shows us this wizard
We will always create the projects in a virtual environment so that the configurations or libraries we include do not interfere with other projects.
It is important to indicate in the “Base interpreter” dropdown the python version we have installed.
Installation of libraries in Pycharm
To install the libraries we need, go to File > Settings > Project:ProjectName, and the + button will appear on the right
The adventure begins. Selenium + CEF
IMPORTANT: the version of Chromedriver we use has to be the same one CEF works with. For example v80.
My first obstacle was not to take this into account and try to launch an example using the Chromedriver corresponding to my version of Google Chrome.
Initialize the driver
To do this, it is necessary to specify the route of the Chromedriver we have downloaded and the port through which we are going to connect to the application. In this case, I have selected the 9090.
Changing the focus between two windows
Sometimes, to perform the complete flow of a test, it is necessary to work with several windows in de Chrome and several things must be taken into account:
- Selenium cannot interact with a window that has not been created under it. To be able to interact with the new window that is opened, it will be necessary to open it through the CEF.
- We must close the new window once we have finished performing the action we need. That provides us a clean environment and with less possibility of failures when performing the executions.
Interacting with Windows: AutoIt
When we want to upload a file from our computer, we will be shown a window of the operating system itself (Windows in my case) so its automation is outside of what Selenium can do. In this case, we have another tool, AutoIt.
AutoIt is a Windows interface automation framework that generates files that can be compiled into an exe in x86 or x64 depending on the version of Windows where we are going to run it.
Here’s the AutoIt code to upload a file:
Running an AutoIt script with Selenium
Once we have compiled our AutoIt script, we must now be able to run it from our Selenium project with Python.
The package os allows you to run operating system files.
Sleeps must be avoided at all costs, however, this time they are necessary to give the executable time to start and close without causing a failure in the test.
You can find all the code in this GitHub project.
I hope this article will be helpful to you and in case of doubt, do not hesitate to contact me on Twitter, LinkedIn, or email.