Skip to content

Instantly share code, notes, and snippets.

@callahad
Created December 19, 2012 16:59
Show Gist options
  • Save callahad/4338268 to your computer and use it in GitHub Desktop.
Save callahad/4338268 to your computer and use it in GitHub Desktop.
Yahoo BigTent Selenium Tests
# -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.keys import Keys
# Utility Functions
def _yahoo_login(browser, username, password):
WebDriverWait(browser, 10) \
.until(lambda driver: driver.find_element_by_id('username')) \
.send_keys(username)
browser.find_element_by_id('passwd').send_keys(password + Keys.RETURN)
def _yahoo_logout(browser):
browser.get('http://login.yahoo.com/config/login?logout=1')
WebDriverWait(browser, 10).until(lambda driver: driver.find_element_by_id('yregsonote'))
def _revoke_persona(browser, username, password):
browser.get('https://api.login.yahoo.com/WSLogin/V1/unlink')
_yahoo_login(browser, username, password)
WebDriverWait(browser, 2).until(lambda driver: driver.find_element_by_id('unlinkForm'))
for removal_link in browser.find_elements_by_class_name('removeLnk'):
if 'persona' in removal_link.get_attribute('id'):
removal_link.click()
WebDriverWait(browser, 10) \
.until(lambda driver: driver.find_element_by_css_selector('button.default')) \
.click()
_yahoo_logout(browser)
def _persona_auth(browser, username):
browser.switch_to_window('__persona_dialog')
WebDriverWait(browser, 10) \
.until(lambda driver: driver.find_element_by_css_selector('body:not(.submit_disabled) #authentication_email')) \
.send_keys(username + Keys.RETURN)
# IdP transition info only shows up once, across multiple runs?
try:
WebDriverWait(browser, 5) \
.until(lambda driver: driver.find_element_by_css_selector('body:not(.submit_disabled) #verifyWithPrimary')) \
.click()
except TimeoutException:
pass
# Tests
def test_success(browser, rp, username, password):
"""User should be able to log in via Yahoo OpenID"""
_revoke_persona(browser, username, password)
browser.get(rp)
button = WebDriverWait(browser, 10).until(lambda driver: driver.find_element_by_tag_name('button'))
WebDriverWait(browser, 10).until(lambda _: button.is_displayed() and button.is_enabled())
button.click()
_persona_auth(browser, username)
_yahoo_login(browser, username, password)
WebDriverWait(browser, 10) \
.until(lambda driver: driver.find_element_by_id('agree')) \
.click()
browser.switch_to_window(browser.window_handles[0])
user = WebDriverWait(browser, 10).until(lambda driver: driver.find_element_by_css_selector('#loggedin > span').text)
if user != yahoo['username']:
raise AssertionError('Did not successfully log into RP as user: ' + username)
browser.quit()
def test_mismatch(browser, rp, username, password):
"""User should get an error if authenticates as unexpected address."""
_revoke_persona(browser, username, password)
browser.get(rp)
button = WebDriverWait(browser, 10).until(lambda driver: driver.find_element_by_tag_name('button'))
WebDriverWait(browser, 10).until(lambda _: button.is_displayed() and button.is_enabled())
button.click()
_persona_auth(browser, 'bogus@yahoo.com')
_yahoo_login(browser, username, password)
WebDriverWait(browser, 10).until(lambda driver: driver.find_element_by_id('agree')).click()
error = WebDriverWait(browser, 10).until(lambda driver: driver.find_element_by_css_selector('#error h2').text)
if error != 'Authentication Error':
raise AssertionError('Did not see expected email mismatch error')
browser.quit()
if __name__ == '__main__':
# Note: Currently having issues in Firefox related to
# https://code.google.com/p/selenium/issues/detail?id=4813
from credentials import yahoo
# yahoo = {'username': 'example@yahoo.com', 'password': 'SrKoHoa5PYXe'}
driver = webdriver.Chrome
rp = 'http://dev.123done.org'
username = yahoo['username']
password = yahoo['password']
test_success(driver(), rp, username, password)
test_mismatch(driver(), rp, username, password)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment