Python selenium webdriver – quick start guide on Mac

Selenium is an excellent tool which automates browsers. One of the commonly approach is to use Selenium webdriver which has API in many languages like Python, Java, etc. This article will cover Selenium webdriver test automation on Mac using python. You can run Selenium webdriver locally or remotely.

Selenium – download and Install

selenium-big-logo

Here are the steps to download and install stuff required to run selenium using python on Mac:

  1. Download selenium-server-standalone-2.33.0.jar from selenium google code site. You can also download selenium-server-2.33.0.zip which contains more stuff along with stand alone server. Skip this step if you just want to run test locally.
  2. Install python selenium bindings using:
    pip install selenium

    To verify and see selenium location run

    python -c "import selenium; print selenium.__file__;"
  3. Install Firefox on Mac if not already installed.

Running python selenium locally

Running selenium locally need only python bindings. For the purpose of this tutorial, we’ll visit infoheap.com and click on search icon which should expand it to a search box. Then we’ll check the existence of the search box.

Here is the python selenium code to achieve this test:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

driver = webdriver.Firefox()
#driver = webdriver.remote.webdriver.WebDriver(command_executor='http://127.0.0.1:4444/wd/hub', desired_capabilities=DesiredCapabilities.FIREFOX)
driver.get("http://infoheap.com")

## no search form
try:
  searchform = driver.find_element_by_css_selector("div[id='searchdiv'] > form")
  ## Above find should raise exception. So we'll never come here.
  assert 0 == 1, "Should not find search form in html before the click"
except:
  pass

## before click
searchimg = driver.find_element_by_css_selector("div[id='searchdiv'] > img")
assert "/img/search_icon" in searchimg.get_attribute("src")

## ensure search form and input box after click
searchimg.click()
searchform = driver.find_element_by_css_selector("div[id='searchdiv'] > form")
assert searchform != None, "No form after the click"

searchfield = driver.find_element_by_css_selector("div[id='searchdiv'] > form input[name='q']")
assert searchfield != None, "No search input box after the click"

driver.close()

The expected outcome from the test is:

  1. Firefox windows opens with search image icon as shown below:
  2. The test script click on the search icon and it expands to a search box as shown below:
    selenium-python-searchbox-test-before-click
  3. The python script exists with no error message.
    selenium-python-searchbox-test-after-click

Running python selenium remotely

To run the selenium test remotely these are the steps:

  1. Run the selenium server on the machine where you want to run browser using the following command.
    java -jar selenium-server-standalone-2.33.0.jar
  2. Make changes to the python script replace driver = webdriver.Firefox() with following:
    driver = webdriver.remote.webdriver.WebDriver(command_executor='http://127.0.0.1:4444/wd/hub', desired_capabilities=DesiredCapabilities.FIREFOX)
  3. Run the python script on the machine from where you want to run the test. For the purpose of this tutorial we are using the same machine. The outcome is similar to running the test locally except that browser runs on the selenium server machine whereas python script output is on client machine.
  4. You can also track the sessions running by visiting the url http://127.0.0.1:4444/wd/hub/ (replace 127.0.0.1 with remote host where selenium server is running).
Share this article: share on Google+ share on facebook share on linkedin tweet this submit to reddit

Comments

Click here to write/view comments