Wednesday, May 22, 2013

How to setup Jenkins for python for tests

How to configure jenkins to run python tests after every commit or by schedule:
Download and install java on your computer.
Download last stable version of jenkins for your operating system from the official site
Run with the following command:
java -jar jenkins.war
Assuming that you already have virtualenv and create your virtual environment
$virtualenv mytestenv
$source mytestenv/bin/activate
Install dependencies for python, in our case: nose and nosexunit (for writing and running tests),
pip install nose
pip install nosexunit
pip install selenium

open a browser and go to http://127.0.0.1:8080 jenkins web interface
install git plugin

Create new job and select run bash:
Put the path to your git working copy: /home/yourname/your_repository

And add the following script:
#!/bin/bash -ex
source /home/andrii/Downloads/mytestdir/bin/activate
nosetests tests.py --with-nosexunit

And add it for reporting:
target/NoseXUnit/core/*.xml

Tuesday, May 21, 2013

How to run webdriver remotely on virtual machine with python

To run webdriver remotely using Python + VBox we need to run selenium standalone server on each machines Master and Slaves with different roles.
Master(Hub)
Slave1(Node)
...
Slave1(Node)

Run selenium server on Master PC with a hub role
>java -jar selenium-server-standalone-2.28.0.jar -role hub
Run selenium instance on Slave VBox with webdriver or node role
>java -jar selenium-server-standalone-2.28.0.jar -role webdriver -hub http://<Hub IP address>:4444/grid/register -browser browserName=chrome, platform=WINDOWS
Run selenium instance on Slave VBox with webdriver or node role
>java -jar selenium-server-standalone-2.28.0.jar -role webdriver -hub http://<Hub IP address>:4444/grid/register -browser browserName=firefox, platform=LINUX
Run selenium instance on Slave VBox with webdriver or node role
>java -jar selenium-server-standalone-2.28.0.jar -role webdriver -hub http://<Hub IP address>:4444/grid/register -browser browserName=safari, platform=MAC
Run webdriver commands on Master PC
>from selenium import webdriver
>browser_win = webdriver.Remote(command_executor='http://localhost:4444/wd/hub', desired_capabilities={'browserName':'firefox', 'platform':'WINDOWS'})
>browser_nix = webdriver.Remote(command_executor='http://localhost:4444/wd/hub', desired_capabilities={'browserName':'firefox', 'platform':'LINUX'})
>browser_mac = webdriver.Remote(command_executor='http://localhost:4444/wd/hub', desired_capabilities={'browserName':'firefox', 'platform':'MAC'})
>browser_nix.get('http://google.com')

Now we're able to write tests which can be executed on remote and/or virtual machines.