Running python tests with GhostDriver in Travis CI
on Feb 18, 2013 by Ramona
Selenium is great by itself, but its API grows every day - it’s not obvious anymore whether to use “type” or “typeKeys”.
- Download PhantomJS from here and extract it.
- Add the folder to the system path and run phantomjs --version in a new command prompt. You should see the actual version (just to verify there are no errors so far).
- On Linux (Ubuntu) - command line:
cd /usr/local/share wget http://phantomjs.googlecode.com/files/phantomjs-1.8.1-linux-x86_64.tar.bz2 tar xjf phantomjs-1.8.1-linux-x86_64.tar.bz2 sudo ln -s /usr/local/share/phantomjs-1.8.1-linux-x86_64/bin/phantomjs /usr/local/share/phantomjs sudo ln -s /usr/local/share/phantomjs-1.8.1-linux-x86_64/bin/phantomjs /usr/local/bin/phantomjs sudo ln -s /usr/local/share/phantomjs-1.8.1-linux-x86_64/bin/phantomjs /usr/bin/phantomjs
It is important that you have installed version 1.8*, because GhostDriver is not integrated into PhantomJS’s older versions.
- Start GhostDriver by setting phantomjs --webdriver=PORT, where PORT can be any available port. You should see a successful message, indicating that GhostDriver is running on port PORT.
- Configure your test environment to use PhantomJS:
For example, create a folder structure like:
- headless_testing, containing a subfolder test, where I will add my selenium scripts written with Python
Add the following two files inside test folder:
- base_test.py (but delete the first two lines - there's no need for these as we will place all py files in the same folder):
Modify the base_test.py file as follows:
- replace port 8080 with the port where GhostDriver is running for you (line 59)
- replace open() with open() (line 40)
- Add your own selenium python script to the same test folder. Make sure your methods are named as test_*, so that you can run them using unittest module.
Add a config.ini file in the test folder, containing
driver=phantomjs.In this way, you’re telling Selenium it should use the headless PhantomJS browser. If you don't want your config.ini file to be mixed up with the python scripts, you could add it to a separate folder, and modify the base_test.py file accordingly.
- pip install discover ( the test discovery mechanism and load_tests protocol for unittest backported from Python 2.7 to work with Python 2.4 or more recent (including Python 3).)
python -m unittest discover -s test --pattern=test*.py
- Log in to Travis with GitHub and enable service hooks for the repositories you want added to Travis. More details here.
- On your local machine, drill inside headless_testing folder and add a file called .travis.yml - this is the config file that will tell Travis how to run your selenium python scripts:
language: python python: - "2.7" install: "pip install -r requirements.txt --use-mirrors" script: - "python -m unittest discover -s test --pattern=test*.py" before_script: - "python test/start_ghostdriver.py"
- Add this to the requirements.txt file (separate lines): selenium==2.28.0 and discover==0.4.0
- The start_ghostdriver.py script is a script I created in order to be able to start GhostDriver and get the control back (so that you are able to run other commands from the command line as well). The content of this script is:
import os #os.system('phantomjs --webdriver=PORT 2>&1 > /dev/null &')
- Travis comes with PhantomJS configured, so no other action is needed in this direction.
- Make sure to add the start_ghostdriver.py file inside headless_testing/test folder. What it does is that it starts a process in the background and it captures the output in the log.txt file. Adding the & ensures the fact that you will gain control back in the command line. Only two lines will be written inside the log file, so you shouldn’t worry about it too much.
- Push the content of the headless_testing folder to GitHub (you might want to use a .gitignore file as well).
comments powered by Disqus