📜 ⬆️ ⬇️

Cloud Autotest Selenium + Ubuntu (step by step instructions)

In this publication, I will discuss how to make friends with Linux (ubuntu server 14.04) with Selenium Server v.2.43.1 , about pitfalls and why I needed a server for automated tests in the cloud.

image

Not so long ago on Habré was published the article "Autotest - a lordly case . " I believe that in a team where more than 2 developers are working on one project is just a necessary thing. When I worked alone, I did without tests. The project was written from scratch, I knew the code like my 5 fingers. The company grew very quickly - in the place with it and the number of tasks. There were new developers, and then the problems started. We write one functional - another falls off. Do not think this happened rarely, but such mistakes were expensive and it was necessary to deal with it. At this time, I decided to introduce autotests into the development process, which I don’t regret.
')
Now I decided to further optimize the testing process. The idea is to automatically run the tests when raising the functional on the virgins, production. The advantages of this approach are obvious and have already been written about them more than once. At a minimum, it is my confidence that the tests have worked and nothing will break when pouring into production.

Solution of the problem :
I chose a cloud (DigitalOcean) in which you can easily expand / clone VPS. Tests run in 2 threads. For these purposes I chose a server - with 2GB of RAM, 2 cores of 2.40GHz each. It should be operative with a margin, otherwise the tests will fall due to its lack , so I connected Swap for 1ig.

As the OS I chose Ubuntu 14.04 (in DO build Ubuntu 14.04 LAMP).

LAMP is needed for the control panel, you can separately install php. To run the tests you need a browser (we are testing in Firefox), Xvfb to run the browser in the background, GUI + XRDP server to connect to the desktop remotely, Exim to send the results to mail, Java + Selenium + PhpUnit + php curl - for work tests.

Upgrade the system and install the FF:

sudo apt-get update sudo apt-get upgrade sudo apt-get install firefox 

We connect by need SWAP:

 sudo dd if=/dev/zero of=/home/swap-tmp bs=1024 count=1024K sudo mkswap /home/swap-tmp sudo swapon /home/swap-tmp echo "/home/swap-tmp swap swap defaults 0 0" | sudo tee -a /etc/fstab 
in the example, the RAM was supplemented with 1GB.

We put xvfb - a shell to run FF in the background and fonts:

 sudo apt-get install xvfb sudo apt-get install -y xfonts-100dpi xfonts-75dpi xfonts-scalable xfonts-cyrillic xvfb x11-apps 

On demand, we put the XFCE + Xrdp server for remote connection and testing in a visual environment:

 apt-get -y install xubuntu-desktop apt-get -y install xrdp 

in /etc/xrdp/startwm.sh we delete what was - we write the following:

 #!/bin/sh if [ -r /etc/default/locale ]; then . /etc/default/locale export LANG LANGUAGE fi #. /etc/X11/Xsession startxfce4 

Restart xrdp:

 service xrdp restart 

Now you can connect to the server via remote desktop.

Install PHP + Curl :

 sudo apt-get install php5 sudo apt-get install php5-curl 

We put phpunit :

 wget https://phar.phpunit.de/phpunit.phar chmod +x phpunit.phar mv phpunit.phar /usr/local/bin/phpunit 

Install JAVA to run Selenium:

 apt-get install default-jdk 

Downloading the latest version of selenium server . At the time of this writing, the last 2.43 :

 mkdir /var/selenium/ wget http://selenium-release.storage.googleapis.com/2.43/selenium-server-standalone-2.43.1.jar mv selenium-server-standalone-2.43.1.jar /var/selenium/server.jar 

To send notifications set exim4 :

 sudo apt-get install exim4 exim4-config dpkg-reconfigure exim4-config 

For me, SMTP through Yandex.Mail is sufficient for this purpose. On the Internet there are many instructions for setting up, so I will not focus on this.

In order for selenium to work properly and to run Firefox in the background, we run the commands :

 Xvnc :10 -geometry 1920x1080 -depth 24 -bs -ac -nolisten tcp > /dev/null & export DISPLAY=:10 

After that you can run Selenium :

 java -jar /var/selenium/server.jar 

This is where the first problem appeared. Selenium hangs on the line:

 04:04:29.264 INFO - Started HttpContext[/,/] 

One time he let it go and it started after about 15 minutes.
When running through debug:

 java -jar /var/selenium/server.jar --debug 09:24:21.360 DEBUG [1] org.openqa.jetty.http.HttpContext - Init classloader from null, sun.misc.Launcher$AppClassLoader@356e3aaf for HttpContext[/wd,/wd] 09:24:21.361 DEBUG [1] org.openqa.jetty.util.Container - Starting org.openqa.jetty.jetty.servlet.ServletHandler@6c10fa4d 09:24:21.361 DEBUG [1] org.openqa.jetty.jetty.servlet.AbstractSessionManager - New random session seed 

It is evident that he is stupid at the creation of the session. As it turned out, the problem is not with me alone and it is solved by editing the file /etc/java-7-openjdk/security/java.security . It is necessary to replace the line:
 securerandom.source=file:/dev/urandom 

On:

 -Djava.security.egd=file:/dev/urandom 

Saved, launched again - everything is OK.

We put Selenium on autostart . Add to /etc/rc.local (at the end of the file, or before die, if any):

 Xvnc :10 -geometry 1920x1080 -depth 24 -bs -ac -nolisten tcp > /dev/null & export DISPLAY=:10 sleep 2s java -jar /var/www/sao/demon/selenium/selenium-server-standalone-2.39.0.jar > /dev/null & 

Now when you start the server - selenium will start automatically.

Almost everything is ready. It remains to configure the tests themselves to send notifications to the mail with the results and create screenshots in case of a fall. To automatically launch tests, you can write a simple script on sockets that listens to the port and, upon receiving a command, runs the necessary tests. In order for the tests to start automatically when uploading to dev / prodakshin - we make the script a wrapper, or use the callback capabilities of the version control system with which you work.

PS For those who use tests based on Yii 1.1 . They do not work with the new version of phpunit. I decided not to switch to the old versions.

I solved the problem as follows:

In bootstrap.php, replaced yiit.php with yiilite.php , Yii :: createWebApplication with Yii :: createConsoleApplication .
In the WebTestCase.php file I replaced extends CWebTestCase with extends PHPUnit_Extensions_SeleniumTestCase
and moved all the functionality from CWebTestCase .

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


All Articles