Skip to content

Instantly share code, notes, and snippets.

@mjumbewu
Forked from fkh/gist:5923372
Last active December 19, 2015 08:09
Show Gist options
  • Save mjumbewu/5923624 to your computer and use it in GitHub Desktop.
Save mjumbewu/5923624 to your computer and use it in GitHub Desktop.
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from unittest import TestCase, main
from functools import wraps
from os import environ
# ============================================================
# Helper functions -- skip these and go to the tests...
# ============================================================
def make_selenium_driver(options):
"""
Create and return a selenium driver with the given capability options.
"""
return webdriver.Remote(command_executor='http://openplans:e4722c24-580f-4e72-a6b2-afa78963c150@ondemand.saucelabs.com:80/wd/hub',desired_capabilities=options)
# return webdriver.Remote(command_executor='https://openplans:opaUakft3rkZSxBF1Mx5@hub.browserstack.com/wd/hub',desired_capabilities=options)
def selenium_test(func, **options):
"""
Create a test that runs the given function with the given selenium options.
The function is expected to be a class instance method that takes a driver
argument.
"""
# Set up defaults and process the options
options.setdefault('name', func.__name__)
options.setdefault('javascriptEnabled', True)
options.setdefault('build', environ.get('TRAVIS_BUILD_NUMBER', 4))
if 'browserName' not in options and 'browser' in options:
options['browserName'] = options.pop('browser')
if options.get('browserName').lower() == 'ie':
options['browserName'] = 'Internet Explorer'
browserName = options.get('browserName').lower()
if browserName == 'internet explorer':
assert 'version' in options
version = float(options['version'])
if version < 8:
options.setdefault('platform', 'Windows XP')
elif version < 10:
options.setdefault('platform', 'Windows 7')
else:
options.setdefault('platform', 'Windows 8')
elif browserName == 'android':
options.setdefault('platform', 'Linux')
elif browserName in ('iphone', 'ipad'):
assert 'version' in options
version = float(options['version'])
if version < 5.1:
options.setdefault('platform', 'OS X 10.6')
else:
options.setdefault('platform', 'OS X 10.8')
# Create the test function
@wraps(func)
def test(self):
driver = make_selenium_driver(options)
try:
func(self, driver)
finally:
driver.quit()
# Make sure it's known as a test, regardless of function name
test.__test__ = True
return test
# ============================================================
# The tests!
# ============================================================
class MyTests (TestCase):
def do_walkphilly_test(self, driver):
driver.get("http://walkphilly.shareabouts.org")
inputElement = driver.find_element_by_css_selector(".close-bttn")
inputElement.click()
inputElement = driver.find_element_by_css_selector(".add-place")
inputElement.click()
print driver.title
test_walkphilly_android4 = selenium_test(do_walkphilly_test, browser='Android', version='4.0')
test_walkphilly_iphone6 = selenium_test(do_walkphilly_test, browser='iPhone', version='6.0')
test_walkphilly_chrome27 = selenium_test(do_walkphilly_test, browser='Chrome', version='27.0')
def do_nosur_test(self, driver):
driver.get("http://nosur.shareabouts.org/")
inputElement = driver.find_element_by_css_selector(".close-bttn")
inputElement.click()
inputElement = driver.find_element_by_css_selector(".add-place")
inputElement.click()
print driver.title
test_nosur_ie10 = selenium_test(do_nosur_test, browser='IE', version='10.0')
test_nosur_chrome27 = selenium_test(do_nosur_test, browser='Chrome', version='27.0')
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment