📜 ⬆️ ⬇️

An interesting way to run Selenium tests in the background

image
Recently, in our project, the question of launching Selenium tests in the background has become acute. One of the rules in our team says that we do not commit the code until all the auto tests have passed. For a project of our size, this is completely realistic. Our projects change every 2-3 months and usually takes from 5 to 15 minutes to get rid of all the tests. The operating system in which we work is Ubuntu, each has two monitors (thanks to the customer). Therefore, looking at the tests, it is convenient to do something else. Pull back the task, for example, or start doing a new task. The main problem is that during the tests on the computer, nothing can be done. Namely, each web driver action makes its window active. Thus, you can only listen to music normally. At best - read or watch a video. But if you print - you almost completely overwhelm any test. Because of this problem, every team member wasted a lot of precious time. So I started invisiting this problem.

Initially, everyone advised to switch from chrome to phantomJs. We tried it, but one problem turned into another. Many items are no longer on the page. A couple of minuses were noticed: it is slower and takes a lot of memory in the repository. In general, phantomJs did not quite meet the requirements, after all, Chrome is the most popular browser and we should have tested it.

On my last project, we also used a chrome driver, but his behavior was significantly different. During the test run, the chrome opened and its window became active, but it was enough to click the mouse once in another place and after that it was possible to continue working normally. Even it was possible to minimize the window. The test continued to work, screenshots were made and reports were generated ... I wanted to customize how the driver be configured in the new project that way.

I asked a question about this issue at many sites. I wrote letters to various experts, including those who commitit directly to Selenium. In fact, nobody answered me like that. Basically there were suggestions to use phantomJs and set up a local jenkins ... Collecting information, I came to the conclusion that this behavior is associated with the operating system. Namely, on Windows, chrome takes focus only at the start, and in Ubuntu and Mac OS for every action, be it a click or an element value check. I’m successful in setting up the driver so that after launching it no longer makes the window active, I never achieved it. Therefore, if you have a solution on this issue, be sure to share it, many people will be very grateful to you.
')
At some point I was completely desperate and thought that I could not solve this problem. And then quite by chance I decided to consult with a friend from the other team, and he told me that they run tests through vncserver. He explained to me how to set it up, and then in the process of using, I came up with a few more details that made the process easier and made it faster and more convenient. This way I want to share with you today.

Slightly deviating from the topic, I want to inform you that we use thucydides reports and therefore we see a beautiful result of all the tests after they pass. This makes the process of observing tests unnecessary. This manual is for Ubuntu and specifically for java projects using maven. Who works on a Mac, I think this approach should work, so who can set up - share instructions to supplement the article.

I advise you first to fully read the entire manual, and only then proceed to the setup:

1. First of all, you need to install vncserver, enter the following command in the terminal: sudo apt-get install vnc4server

2. You need to start the server, under some display number, I chose number 7. When you start the server for the first time, you will be asked to enter a password. Remember the password, it will be needed in the future to connect. Enter: vncserver: 7

3. The server can start with a small display resolution, with the help of this command (immediately without performing the second step) you can set the resolution for you: vncserver: 7 -geometry 1920-1080

4. To connect to the server, you can use any vncviewer, I prefer Real VNC. Download, install ...

5. Open the viewer. Click the button “start a new connection” and enter the server: localhost: 7 in the line, and in the line Encryption: Let VNC Server choose

6. Click "connect" and enter the password, which we indicated in paragraph two.

7. A window should open, duplicating your desktop. Run the terminal in this window.

8. In my case, go to the project folder and enter the command: mvn clean install

9. Tests run without interfering with other programs. You can safely minimize the window or even close the vnc viewer program - this will not affect the passing of the tests.

Additional steps:
If you want to run tests without vncviewer, then there is a wonderful command, go to the project folder in the TM (you can also in the IDE terminal) and enter:
xterm -display localhost: 7 -e mvn clean install
The disadvantage of this method is that you do not see the console output, but only the end of the process. If you have reports, then for you it is not particularly important.

If you want to stop vncserver, then type: vncserver -kill: 7
After restarting the computer, you usually need to repeat step 2 or 3. And then run the tests through the viewer or directly in the terminal with the command written above.

As you already understood, this method is not at all perfect, but, undoubtedly, it is much better than just running tests, as we did before. Tests are as stable and fast as they are without a vnc server.

Now for me the most convenient is to start the server and immediately start tests from the IDE directly. I also tried to open the IDE directly in vncviewer (to do this you first need to close it in a normal window). Work and run tests in a standard way, one at a time, for example. This is also quite convenient, especially if you debug one long test and run it many times.

I hope this material will help you save a lot of time. If you have more ways to run tests without interfering with the work, then be sure to share them. And also, if you have ideas on how to improve this approach.

UPD: Quite by accident, I found the reason (or one of the reasons) for this behavior of the web driver. Our tests took a very long time and we decided to turn off the screenshots for each step. Now the screenshot is taken only on the last step, before the test falls. And suddenly the tests began to pass normally in the background. Now I wonder if tests with screenshots for each step do not allow the web driver to work in the background on Mac OS X and Windows?

Source: https://habr.com/ru/post/239363/


All Articles