An interesting way to run Selenium tests in the background
Recently, our project has sharply raised the issue of launching Selenium tests in the background. One of the rules in our team says that we don’t commit the code until we have passed all the auto tests. For a project of our size, this is quite real. Our projects change every 2-3 months and usually takes 5 to 15 minutes to run all the tests. The operating system in which we work is Ubuntu, everyone has two monitors (thanks to the customer). Therefore, looking at the tests, it’s convenient to do something else. Review the tasks, for example, or start doing a new task. The main problem is that while passing the tests on the computer, nothing can be done. Namely, every action of the web driver makes its window active. Thus, you can only listen to music normally. At best, read or watch a video. But if you print, then you will almost completely fail some test. Because of this problem, each team member wasted a lot of precious time. So I started the invystigation of this problem.
Initially, everyone advised switching from chrome to phantomJs. We tried this, but one problem turned into another. Many elements are no longer on the page. A couple of minuses were noticed: it is slower and takes up a lot of memory in the repository. In general, phantomJs did not quite meet the requirements, yet Chrome is the most popular browser and we should have tested it.
On my last project, we also used the chrome driver, but its behavior was significantly different. When the test started, 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. It was even possible to minimize the window. The test continued to work, screenshots were taken and reports were generated ... I wanted to configure the driver behavior in the new project in this way.
I asked a question on this issue at many sites. He wrote letters to various experts, including those who commit directly to Selenium. In fact, no one answered me that way. Basically, there were suggestions to use phantomJs and set up a local jenkins ... Collecting information, I came to the conclusion that this behavior is related to the operating system. Namely - on Windows, chrome takes focus only at the start, and in Ubuntu and Mac OS, for every action, whether it be a click or checking the value of an element. Success in setting up the driver so that after starting it no longer makes the window active, I still haven’t 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 completely despaired and thought that I could not solve this problem. And then, quite by accident, I decided to consult with a friend from another team, and he told me that they run tests through vncserver. He explained to me how to configure this, and then in the process of using it, I came up with a few more details that made the process easier and faster and more convenient. This is the way I want to share with you today.
A little deviating from the topic, I want to report that we use thucydides reports and therefore we see a beautiful result of all tests after passing them. This makes the process of monitoring the tests unnecessary. This instruction is for Ubuntu and specifically for java projects using maven. Who works on a Mac, I think this approach should work, so whoever can configure it, share the instructions to complete the article.
I advise you to first read the entire instruction, and only then proceed with the configuration:
1. First, you need to install vncserver, enter the 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. We enter : vncserver: 7
3. The server can start up with a small display resolution, with this command (immediately without performing the second step) you can set the desired resolution for you yourself: 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. Press the button “start a new connection” and enter the server in the line: localhost: 7 , and in the line Encryption: Let VNC Server choose
6. Click "connect" and enter the password that we specified in paragraph two.
7. A window should open that duplicates your desktop. Launch 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 tests.
If you want to run tests without vncviewer, then there is a wonderful command, go to the project folder in the final (you can also in the IDE terminal) and type:
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 this is not particularly important for you.
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 far from ideal, but, undoubtedly, it is much better than just running tests, as we did before. Tests are as stable and fast as without a vnc server.
Now the most convenient thing for me is to start the server and immediately start the tests directly from the IDE console. I also tried to open the IDE directly in vncviewer (for this, you first need to close it in a regular window). Run and run tests in a standard way, one at a time, for example. This is also quite convenient, especially if you debate one long test and run it many times.
I hope this material helps you save a lot of time. If you still have ways to run the 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 disable screenshots at every step. Now the screenshot is taken only at the front-end step before the test crashes. And suddenly, tests began to pass normally in the background. Now I wonder if the tests with screenshots for each step prevent the web driver from working in the background on Mac OS X and Windows?