, ,

Python – Testing Selenium with Google Chrome


# use a custom directory for download and installation
mkdir -p ~/build/selenium
cd ~/build/selenium

# Install Google Chrome
wget -q -O- https://dl-ssl.google.com/linux/linux_signing_key.pub | sudo apt-key add -
sudo sh -c 'echo "deb http://dl.google.com/linux/chrome/deb/ stable main" > /etc/apt/sources.list.d/google.list'
sudo apt-get update
sudo apt-get install -y google-chrome-stable

# Download Selenium
wget http://selenium-release.storage.googleapis.com/2.41/selenium-server-standalone-2.41.0.jar

# Download Chrome Driver Selenium

wget http://chromedriver.storage.googleapis.com/2.9/chromedriver_linux64.zip
unzip chromedriver_linux64.zip
# chromedriver

# Install Selenium Python Bindings‎
sudo pip install selenium

# Create Python Test File for Selenium with a Google Chrome Driver
cat > chrome-selenium-test.py <<"_EOF_"
# -*- coding: utf-8 -*-
import os
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
chromedriver = "./chromedriver"
os.environ["webdriver.chrome.driver"] = chromedriver
driver = webdriver.Chrome(chromedriver)
#driver = webdriver.Firefox()
driver.get("http://www.python.org")
print driver.title
assert "Python" in driver.title

# submit query
elem = driver.find_element_by_name("q")
elem.send_keys("selenium")
elem.send_keys(Keys.RETURN)

# get performance data
performance = driver.execute_script("return window.performance")
print performance

driver.close()

_EOF_

python chrome-selenium-test.py
# Welcome to Python.org
# {u'webkitClearResourceTimings': {}, u'memory': {u'totalJSHeapSize': 12700000, u'usedJSHeapSize': 10000000, u'jsHeapSizeLimit': 1620000000}, u'webkitGetEntries': {}, u'removeEventListener': {}, u'webkitSetResourceTimingBufferSize': {}, u'getEntries': {}, u'clearMeasures': {}, u'webkitGetEntriesByType': {}, u'addEventListener': {}, u'measure': {}, u'webkitGetEntriesByName': {}, u'getEntriesByName': {}, u'mark': {}, u'clearMarks': {}, u'onwebkitresourcetimingbufferfull': None, u'getEntriesByType': {}, u'dispatchEvent': {}, u'timing': {u'secureConnectionStart': 1398625289930, u'redirectStart': 0, u'domContentLoadedEventStart': 1398625291520, u'responseEnd': 1398625291392, u'redirectEnd': 0, u'loadEventStart': 1398625292024, u'unloadEventStart': 1398625291395, u'domainLookupEnd': 1398625289457, u'connectEnd': 1398625290252, u'unloadEventEnd': 1398625291395, u'requestStart': 1398625290252, u'loadEventEnd': 1398625292048, u'navigationStart': 1398625289453, u'domLoading': 1398625291407, u'domInteractive': 1398625291520, u'fetchStart': 1398625289453, u'domComplete': 1398625292023, u'domContentLoadedEventEnd': 1398625291571, u'responseStart': 1398625291225, u'connectStart': 1398625289457, u'domainLookupStart': 1398625289457}, u'now': {}, u'navigation': {u'TYPE_RELOAD': 1, u'redirectCount': 0, u'TYPE_RESERVED': 255, u'TYPE_NAVIGATE': 0, u'type': 0, u'TYPE_BACK_FORWARD': 2}}



References

1. http://selenium-python.readthedocs.org/en/latest/installation.html

2. http://qaquestions.wordpress.com/2012/03/05/selenium-web-driver-some-tricks-using-python/