Selenium is a web testing toolkit that allows you to test a web site in a browser of your choice.
In this post I’m going to explain how to setup Selenium with Firefox and PhantomJS on a RHEL / Centos 7.
We start off by installing pip which in turn installs selenium.
[root@vm1 ~]# easy_install pip [root@vm1 ~]# pip install selenium
Lets have a quick test to see if it works
[root@vm1 ~]# python Python 2.7.5 (default, Aug 2 2016, 04:20:16) [GCC 4.8.5 20150623 (Red Hat 4.8.5-4)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import selenium >>> import unittest >>> quit()
So that’s a success.
Moving on to Xvfb and firefox:
Xvfb is a display server that does everything in memory without showing anything on the screen.
This makes it ideal for automated testing on a headless system.
Xvfb is present in the default repos of Centos (Optional repo in RHEL).
[root@vm1 ~]# yum install xorg-x11-server-Xvfb firefox
Easy enough, lets test it out by starting Xvfb and checking if we can run firefox in the foreground without errors:
[root@vm1]# Xvfb :0 -ac & [2] 18787 [root@vm1]# export DISPLAY=:1 [root@vm1]# firefox Xlib: extension "RANDR" missing on display ":1". GLib-GIO-Message: Using the 'memory' GSettings backend. Your settings will not be saved or shared with other applications.
You can kill it with ctrl-c after that.
Since version 3.0.2 the Selenium documentation explicitly states you need to install the gecko driver.
If you really don’t want this install a 2.x version. The latest is 2.53.6.
[kevin@vm1]$ wget https://github.com/mozilla/geckodriver/releases/download/v0.17.0/geckodriver-v0.17.0-linux64.tar.gz
Copy the geckodriver in /usr/local/bin. You can install is somewhere else but then you need to add it to the path.
[kevin@vm1]$ cd /usr/local/bin/ [kevin@vm1 bin]$ ls [kevin@vm1 bin]$ sudo tar -xvzf ~/geckodriver-v0.17.0-linux64.tar.gz [sudo] password for kevin: geckodriver [kevin@vm1 bin]$ sudo chown root:root geckodriver [kevin@vm1 bin]$ ll total 6972 -rwxrwxr-x. 1 root root 7136049 Jun 9 17:47 geckodriver
So that’s one browser present and ready to go. I also added PhantomJS as a secondary webdriver. PhantomJS requires some basic libraries.
yum install fontconfig freetype freetype-devel fontconfig-devel libstdc++
Next we download Phantomjs and install it under /opt.
[kevin@vm1 ~]$ sudo mkdir -p /opt/phantomjs [sudo] password for kevin: [kevin@vm1 ~]$ cd /opt/phantomjs/ [kevin@vm1 phantomjs]$ sudo tar -xvjf ~/phantomjs-2.1.1-linux-x86_64.tar.bz2 --strip-components 1
Run a quick test:
[kevin@vm1 phantomjs]$ bin/phantomjs examples/hello.js Hello, world!
All we have left now is write a little test script. In this case I use PhantomJS to check the some basic stuff on the python.org site. My systems use a proxy so I added that as well to the code:
import unittest from selenium import webdriver from selenium.webdriver.common.keys import Keys from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.by import By class PythonOrgSearch(unittest.TestCase): def setUp(self): service_args = [ '--proxy=127.0.0.1:3128', '--proxy-type=http' ] self.driver = webdriver.PhantomJS('/opt/phantomjs/bin/phantomjs',service_args=service_args) self.driver.set_window_size(1124,850) def test_search_in_python_org(self): driver = self.driver driver.get("http://www.python.org") assert "Bolero" in driver.title elem = driver.find_element_by_name("q") elem.clear() elem.send_keys("python") elem.send_keys(Keys.RETURN) assert "No results found" not in driver.page_source def tearDown(self): self.driver.close() if __name__ == "__main__": unittest.main()