Per Erik Strandberg /cv /kurser /blog

I just did my first scripted web-test with Selenium.

On a Ubuntu 12.04 LTS machine all I had to do was three ridiculously simple steps:

1 - Install easy_install

In fact this was already done - but I'll demonstrate it here any way:

$ easy_install --version
The program 'easy_install' is currently not installed.  You can install it by typing:
sudo apt-get install python-setuptools

$ sudo apt-get install python-setuptools
[sudo] password for per: 
[...]
Setting up python-setuptools (0.6.24-1ubuntu1) ...

$ easy_install --version
distribute 0.6.24dev-r0

2 - Installing Selenium

$ sudo easy_install selenium
[sudo] password for per: 
Searching for selenium
Reading http://pypi.python.org/simple/selenium/
Reading http://code.google.com/p/selenium/
Reading http://www.openqa.org/
Reading http://seleniumhq.org/
Best match: selenium 2.25.0
Downloading http://pypi.python.org/packages/source/s/selenium/selenium-2.25.0.tar.gz#md5=9b799caef8fe32e50094ff78f856265a
Processing selenium-2.25.0.tar.gz
Running selenium-2.25.0/setup.py -q bdist_egg --dist-dir /tmp/easy_install-sQCKSx/selenium-2.25.0/egg-dist-tmp-GH7sWH
warning: no files found matching 'docs/api/py/index.rst'
Adding selenium 2.25.0 to easy-install.pth file

Installed /usr/local/lib/python2.7/dist-packages/selenium-2.25.0-py2.7.egg
Processing dependencies for selenium
Finished processing dependencies for selenium

3 - Verifying that the installation was ok

I got this test script of the selenium home page (see [1])

from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait # available since 2.4.0
import time

# Create a new instance of the Firefox driver
driver = webdriver.Firefox()

# go to the google home page
driver.get("http://www.google.com")

# find the element that's name attribute is q (the google search box)
inputElement = driver.find_element_by_name("q")

# type in the search
inputElement.send_keys("Cheese!")

# submit the form (although google automatically searches now without submitting)
inputElement.submit()

# the page is ajaxy so the title is originally this:
print driver.title

try:
    # we have to wait for the page to refresh, the last thing that seems to be updated is the title
    WebDriverWait(driver, 10).until(lambda driver : driver.title.lower().startswith("cheese!"))

    # You should see "cheese! - Google Search"
    print driver.title

finally:
    driver.quit()

I just ran it and it was ok

$ python test-selenium.py 
Google
cheese! - Sök på Google

By adding a time.sleep(5) in the finally branch I was able to take a screen shot:
http://www.pererikstrandberg.se/blog/selenium/selenium_in_action.png


See also Selenium Plus Android


This page belongs in Kategori Programmering
This page belongs in Kategori Test