Skip to content

Instantly share code, notes, and snippets.

@klrmn
Last active November 19, 2015 23:57
Show Gist options
  • Save klrmn/b67aec9a3b8563e824b3 to your computer and use it in GitHub Desktop.
Save klrmn/b67aec9a3b8563e824b3 to your computer and use it in GitHub Desktop.
import ConfigParser
from selenium import webdriver
class WebdriverFactory(object):
def setup_selenium_from_config(self, config):
"""Start selenium with values from config file, or defaults
rather than requiring the command-line options. File must be
ConfigParser compliant and have a section called 'SELENIUM'.
"""
if config.has_option("SELENIUM", "BROWSER_LOCATION"):
self.BROWSER_LOCATION = config.get("SELENIUM", "BROWSER_LOCATION")
else:
self.BROWSER_LOCATION = 'local'
if config.has_option("SELENIUM", "BROWSER"):
self.BROWSER = config.get("SELENIUM", "BROWSER")
else:
self.BROWSER = 'FIREFOX'
if config.has_option("SELENIUM", "BUILD"):
self.BUILD = config.get("SELENIUM", "BUILD")
if config.has_option("SELENIUM", "BROWSER_VERSION"):
self.BROWSER_VERSION = config.get("SELENIUM", "BROWSER_VERSION")
if config.has_option("SELENIUM", "OS"):
self.OS = config.get("SELENIUM", "OS")
if config.has_option("SELENIUM", "REMOTE_ADDRESS"):
self.REMOTE_ADDRESS = config.get("SELENIUM", "REMOTE_ADDRESS")
if config.has_option("SELENIUM", "REMOTE_PORT"):
self.REMOTE_PORT = config.get("SELENIUM", "REMOTE_PORT")
else:
self.REMOTE_PORT = 4444
if config.has_option("SELENIUM", "TIMEOUT"):
self.TIMEOUT = config.getfloat("SELENIUM", "TIMEOUT")
else:
self.TIMEOUT = 60
if config.has_option("SELENIUM", "SAUCE_USERNAME"):
self.SAUCE_USERNAME = config.get("SELENIUM", "SAUCE_USERNAME")
if config.has_option("SELENIUM", "SAUCE_APIKEY"):
self.SAUCE_APIKEY = config.get("SELENIUM", "SAUCE_APIKEY")
def build_webdriver(self, name="", tags=[], public=False):
"""Create and return the desired WebDriver instance."""
wd = None
if self.BROWSER_LOCATION == 'local':
if self.BROWSER == 'FIREFOX':
wd = webdriver.Firefox()
elif self.BROWSER == 'CHROME':
wd = webdriver.Chrome()
elif self.BROWSER == 'INTERNETEXPLORER':
wd = webdriver.Ie()
else:
raise TypeError(
'WebDriver does not have a driver for local %s'
% self.BROWSER)
elif self.BROWSER_LOCATION == 'remote':
capabilities = getattr(webdriver.DesiredCapabilities,
self.BROWSER.upper())
executor = 'http://%s:%s/wd/hub' % (self.REMOTE_ADDRESS,
self.REMOTE_PORT)
wd = webdriver.Remote(command_executor=executor,
desired_capabilities=capabilities)
elif self.BROWSER_LOCATION == 'grid':
capabilities = getattr(webdriver.DesiredCapabilities,
self.BROWSER.upper())
capabilities['version'] = self.BROWSER_VERSION
capabilities['platform'] = self.OS.upper()
executor = 'http://%s:%s/wd/hub' % (self.REMOTE_ADDRESS,
self.REMOTE_PORT)
wd = webdriver.Remote(command_executor=executor,
desired_capabilities=capabilities)
elif self.BROWSER_LOCATION == 'sauce':
capabilities = {
'build': self.BUILD,
'name': name,
'tags': tags,
'public': public,
'restricted-public-info': not public,
'platform': self.OS,
'browserName': self.BROWSER,
'version': self.BROWSER_VERSION,
}
executor = 'http://%s:%s@ondemand.saucelabs.com:80/wd/hub' % (
self.SAUCE_USERNAME, self.SAUCE_APIKEY)
wd = webdriver.Remote(command_executor=executor,
desired_capabilities=capabilities)
else:
raise TypeError("browser location %s not found" %
self.BROWSER_LOCATION)
wd.implicitly_wait(self.TIMEOUT)
# sometimes what goes out != what goes in, so log it
self.logger.info("actual capabilities: %s" % wd.capabilities)
def __str__(self):
# make exceptions less verbose
return repr(self.msg).split('\\n')[0]
WebDriverException.__str__ = __str__
wd.maximize_window()
return wd
CONF_FILE = os.getcwd() + "/acceptance_test.conf"
CONFIG = ConfigParser.ConfigParser()
CONFIG.read(CONF_FILE)
""" config file looks like
[SELENIUM]
BROWSER_LOCATION: remote
BROWSER: FIREFOX
REMOTE_ADDRESS: testclient
REMOTE_PORT: 4444
TIMEOUT: 60
"""
factory = WebdriverFactory()
factory.setup_selenium_from_config(CONFIG)
wd = factory.build_webdriver()
@klrmn
Copy link
Author

klrmn commented Nov 19, 2015

Note: this is pulled from company repo and twiddled to make sense stand-alone, the methods in the class have been tested, but the code at the bottom not-so-much.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment