Selenium with Python bindings

After a lot of posts on Tornado web server and understanding BDD, lets get to testing our website. What better than to you selenium. Lets go through the setup and create our first test..

Prerequisites

1. Python bindings for Selenium – Go to, selenium site and download the package

Install as:

  • tar xvf selenium-2.25.0.tar.gz
  • cd selenium-2.25.0
  • sudo python setup.py install

2. Java Server – Download the server from here

Run as:

  • java -jar selenium-server-standalone-2.25.0.jar

Here we discuss the usage of Selenium 2.0 Web Driver, with/without selenium server. Below are the examples of each of these:

Just a bit of history first… Web Driver aims to improve Selenium 1.0 Remote Control. The distinguishing factors being:

  • Object Oriented APIs
  • More features
  • Web Driver uses the APIs exported by the browser for automated testing while Selenium Remote Control injects Javascript to run the test

Web Driver without selenium server

from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.keys import Keys
import time

browser = webdriver.Firefox() # Get local session of firefox
browser.get("http://www.yahoo.com") # Load page
assert "Yahoo!" in browser.title

Web Driver with selenium server – WebDriver Remote

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
driver = webdriver.Remote(
   command_executor='http://127.0.0.1:4444/wd/hub',
   desired_capabilities=DesiredCapabilities.FIREFOX)

driver.get("http://www.python.org")
driver.close()

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.