Skip to content

Instantly share code, notes, and snippets.

@devinmancuso
Last active August 29, 2015 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save devinmancuso/ea96d4f5c7cb309a3e14 to your computer and use it in GitHub Desktop.
Save devinmancuso/ea96d4f5c7cb309a3e14 to your computer and use it in GitHub Desktop.
Example Python Chrome Keyboard Navigation Automated Unit Testing Using Selenium 2 WebDriver ChromeDriver
# Import unittest module for creating unit tests
import unittest
# Import time module to implement
import time
# Import the Selenium 2 module (aka "webdriver")
from selenium import webdriver
# For automating data input
from selenium.webdriver.common.keys import Keys
# For providing custom configurations for Chrome to run
from selenium.webdriver.chrome.options import Options
# --------------------------------------
# Provide a class for the unit test case
class PythonAutoTabberChrome(unittest.TestCase):
# Anything declared in setUp will be executed for all test cases
def setUp(self):
# Define a variable to hold all the configurations we want
chrome_options = webdriver.ChromeOptions()
# Define chrome option configurations here ...
#options.add_argument('--start-maximized')
# Create driver, pass it the path to the chromedriver file and the special configurations you want to run
self.driver = webdriver.Chrome(executable_path='/Library/Python/2.7/site-packages/selenium/webdriver/chrome/chromedriver', chrome_options=chrome_options)
# Window management hacks because I'm using OS X. On Windows or Linux you could just specify these as a ChromeOption
self.driver.set_window_size(1920, 1080)
self.driver.maximize_window()
# An individual test case. Must start with 'test_' (as per unittest module)
def test_search_in_python_chrome(self):
# Assigning a local variable for the global driver
driver = self.driver
# Go to specific page
driver.get('http://stackoverflow.com/questions/18477689/python-selenium-switch-to-an-alert-and-verify-the-text-within')
# Pauses the screen for 1 second so we have time to confirm it arrived at the right page
time.sleep(1)
# Go to the HTML body so we can tab from the beginning of the HTML content
driver.find_element_by_tag_name('body').send_keys(Keys.TAB)
# Count how many elements with the tabindex attribute exist on the page - this is to handle any divs that have been made clickable with a specific tab index
counterTabIndex = len(driver.find_elements_by_xpath("//*[@tabindex]"))
# Count how many a link elements exist on the page
countAnchor = len(driver.find_elements_by_xpath("//a"))
# Print tabindex element count number to console
print counterTabIndex, 'elements with a tabindex found on this page'
# Print a link count number to console
print countAnchor, 'ahref elements found on this page'
# Add them together for the total count
totalCounter = counterTabIndex + countAnchor
# Loop and tab through all elements. Note the length of the loop is defined by the element count done above
for x in range(0, totalCounter):
time.sleep(0.2)
driver.switch_to_active_element().send_keys(Keys.TAB)
# Another pause so we can see what's going on
time.sleep(1)
# Anything declared in tearDown will be executed for all test cases
def tearDown(self):
# Close the browser.
# Note close() will close the current tab, if its the last tab it will close the browser. To close the browser entirely use quit()
self.driver.close()
# Boilerplate code to start the unit tests
if __name__ == "__main__":
unittest.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment