To install Selenium in Linux

Selenium is a framework for automated testing of web applications, which allows you not only to test the software, but also to create a variety of programs for automation of tasks performed in the browser. The program can work with the browser as well as people – to move the mouse, to click, to flip the pages, find elements by class name, css selector or xpath, and also to take snapshots of web pages.

The platform runs on Java and to connect to the browser using the drivers browsers. There is a driver for Chrome, Firefox, Opera, IE. Also within Chrome you can use the emulator of the mobile platforms. In this article we will discuss how to install Selenium in Linux example Ubuntu.

To install Selenium in Linux

1. Install Java

Because the program need Java, first you have to install it. You can use article install java in linux to find how to install this software package into their system. In Ubuntu you can install the version of OpenJDK using:

sudo apt install openjdk jre

I specifically wrote in the title, linux is not Ubuntu, or another distribution, because installation in most distributions will be virtually no different. Next you need to install the browser, download the driver for the desired browser and then download the executable file and Selenium all.

2. Installing browser drivers

Support IE in Linux to not work, but we can work with Chrome and Firefox. To install these browsers follow:

sudo apt install firefox chromium-browser

Download the latest version of the driver for Chrome from the official page. Please note that the driver version must match the version of your browser. Version of Chrome, you can find out by opening chrome://settings/help

Chrome 76 you should use the same version of the driver.

At the moment, the latest is non-ie web browsers 76.0.3809.68:

wget https://chromedriver.storage.googleapis.com/index.html?path=76.0.3809.68/

Next, unzip the downloaded archive and move the driver into /usr/local/bin:

unzip chromedriver_linux64.zip

non-ie web browsers sudo mv /usr/local/bin/non-ie web browsers

sudo chown root:root /usr/local/bin/non-ie web browsers

sudo chmod +x /usr/bin/local/non-ie web browsers

Now need to install the selenium webdriver for Firefox. It’s called GeckoDriver and you can download it on this page. Similarly, it is necessary to unpack the resulting archive and move the file geckodriver in /usr/local/bin:

tar-xvzf geckodriver_linux64.tar.gz

geckodriver sudo mv /usr/local/bin/geckodriver

sudo chown root:root /usr/local/bin/geckodriver

sudo chmod +x /usr/bin/local/geckodriver

Then, you can proceed to install Selenium in Linux.

3. Installing Selenium

The selenium framework is a simple java program that can be downloaded from the official website.

At the time of writing, the latest version of the program 3.141.59:

wget https://selenium-release.storage.googleapis.com/3.141/selenium-server-standalone-3.141.59.jar

Then the downloaded program can be run via java machine and you can start the tests:

java-jar selenium-server-standalone-3.141.59.jar

If you want to run the program on the server that is not running a GUI, you will need the xvfb package, which implements a virtual framebuffer in memory:

install xvfb sudo apt libxi6 libgconf-2-4

xvfb-run java-jar selenium-server-standalone-3.141.59.jar

If you don’t want to run selenium manually, you can configure the program from starting automatically at system startup. First, we need to move Selenium in some system directory, such as /usr/bin/local:

sudo mv selenium-server-standalone-3.141.59.jar /usr/bin/local/

Create a new user on behalf of which will run Selenium:

sudo useradd -d /tmp/ selenium

Next you need to allow this user to connect to the display server:

xhost +SI:localuser:selenium

And in startup:

vi ~/.xinitrc

xhost +SI:localuser:selenium

Now let’s write this systemd unit:

sudo vi /etc/systemd/system/selenium.service

[Unit]
Description=Selenium Standalone Server
After=multi-user.target
[Service]
Type=simple
WorkingDirectory=/tmp/
Environment=DISPLAY=:0
ExecStart=/usr/bin/java-jar /usr/local/bin/selenium-server-standalone-3.141.59.jar
WantedBy=multi-user.target
KillMode=process
User=selenium
[Install]
WantedBy=multi-user.target

Now run the following commands to add the service to autostart and run:

sudo systemctl --system daemon-reload

sudo systemctl enable selenium

sudo systemctl start selenium

4. Configure Selenium and PHP

During the development of the Selenium it was written many libraries for different languages. Next we will consider how to use this program along with PHP. Thanks to the efforts of Facebook possible. Only need to install the package facebook/webdriver using composer:

composer require facebook/webdriver

Then you can use this library in your scripts:

vi test-selenium.php

<?php
require ('./vendor/autoload.php');
use FacebookWebDriverRemoteDesiredCapabilities;
use FacebookWebDriverRemoteRemoteWebDriver;
use FacebookWebDriverRemoteDriverCommand;
use FacebookWebDriverWebDriverExpectedCondition;
use FacebookWebDriverWebDriverBy;
$host = 'http://localhost:4444/wd/hub';
$desiredCapabilities = DesiredCapabilities::chrome();
$driver = RemoteWebDriver::create($host, $desiredCapabilities);
$driver->get("https://google.com");

Ready. Now we have to execute the script and if everything was done correctly, you will open the browser.

Insights

In this article, we discussed how to install Selenium in Ubuntu 18.04. With the installation of the programs necessary to play, but the possibilities for automation testing that it provides are definitely worth it.

Source: losst.ru

(Visited 130 times, 3 visits today)