Skip to content

Commit fd731a7

Browse files
committed
Updated seleniumbender.py
- Replaced configuration bash script with config file - Grabbing browser version from capabilities yaml to generate the appropriate user agents - Setting platform in capabilities, to run the tests in the appropriate OS when in sauccelabs
1 parent edcadb6 commit fd731a7

File tree

1 file changed

+86
-23
lines changed

1 file changed

+86
-23
lines changed

bin/seleniumbender.py

Lines changed: 86 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
#!/usr/bin/env python
22

33
from __future__ import print_function
4+
import ConfigParser
45
import argparse
56
import os
67
import re
78
import subprocess
89
import sys
910
import time
11+
import yaml
1012

1113
SHELL='/bin/bash'
1214
SOURCE = 'codebender_cc'
@@ -39,19 +41,17 @@ def run(self, operation, libraries=None):
3941
elif operation == 'staging':
4042
self.staging()
4143

42-
def run_command(self, command, user_agent=None):
43-
if user_agent:
44-
os.environ['SELENIUM_USER_AGENT'] = user_agent
44+
def run_command(self, command):
4545
command = ' '.join(command)
4646
print('command:', command)
4747
return subprocess.call(command, shell=True, executable=SHELL)
4848

4949
def send_mail_no_logs(self, identifier):
5050
command = ['mail', '-s', '"Selenium Tests: {identifier} Failed To Run" {email} <<< "Something went wrong with {identifier} tests. Please check the logs."'.format(identifier=identifier, email=self.email)]
51-
run_command(command)
51+
self.run_command(command)
5252

5353
def send_mail_with_logs(self, identifier):
54-
default_tests_dir = os.path.normpath(os.path.join(os.getcwd(), '..'))
54+
default_tests_dir = os.path.normpath(os.path.join(os.path.dirname(__file__), '..'))
5555
root_dir = os.getenv('ROOTDIR', default_tests_dir)
5656

5757
logs = os.path.join(root_dir, 'logs')
@@ -74,12 +74,12 @@ def send_mail_with_logs(self, identifier):
7474
'uuencode "{reports}/{reportfile}" "{reportfile}")'.format(reports=reports, reportfile=reportfile),
7575
'| mail -s "Selenium Tests Report: {identifier} {email_date} Changes: {changes}" {email}'.format(identifier=identifier, email_date=email_date, changes=changes, email=self.email)
7676
]
77-
run_command(command)
77+
self.run_command(command)
7878
except:
7979
pass
8080

8181
def create_command(self, test_directory, *extra_arguments):
82-
return ['tox', 'tests/' + test_directory, '--', '--url={}'.format(TARGETS[self.url]), '--source={}'.format(SOURCE)] + list(extra_arguments)
82+
return ['tox', 'tests/' + test_directory, '--', '--url={}'.format(TARGETS[self.url])] + list(extra_arguments)
8383

8484
def common(self, identifier='common'):
8585
command = self.create_command('common', '--plugin')
@@ -115,14 +115,16 @@ def noplugin(self, identifier = 'noplugin'):
115115
def walkthrough(self, identifier='walkthrough'):
116116
command = self.create_command('walkthrough', '--plugin')
117117
retvals = []
118-
user_agents = [
119-
'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:43.0) Gecko/20100101 Firefox/43.0 codebender-selenium',
120-
'Mozilla/5.0 (Windows NT 6.1; rv:43.0) Gecko/20100101 Firefox/43.0 codebender-selenium',
121-
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_1; rv:43.0) Gecko/20100101 Firefox/43.0 codebender-selenium'
122-
]
123-
for user_agent in user_agents:
124-
retval = self.run_command(command, user_agent=user_agent)
118+
for platform in USER_AGENTS.keys():
119+
for browser in USER_AGENTS[platform].keys():
120+
if browser == 'chrome':
121+
os.environ['SELENIUM_USER_AGENT_CHROME'] = USER_AGENTS[platform][browser]
122+
elif browser == 'firefox':
123+
os.environ['SELENIUM_USER_AGENT_FIREFOX'] = USER_AGENTS[platform][browser]
124+
os.environ['SELENIUM_PLATFORM'] = platform
125+
retval = self.run_command(command)
125126
retvals.append(retval)
127+
126128
retval = max(retvals)
127129
if retval != 0:
128130
self.send_mail_no_logs(identifier)
@@ -148,6 +150,35 @@ def staging(self):
148150
'local': 'http://dev.codebender.cc'
149151
}
150152

153+
PLATFORMS = {
154+
'Linux': 'X11; Ubuntu; Linux x86_64',
155+
'Windows 7': 'Windows NT 6.1',
156+
'OS X 10.11': 'Macintosh; Intel Mac OS X 10_11_1'
157+
}
158+
USER_AGENTS = {}
159+
USER_AGENT_IDENTIFIER = 'codebender-selenium'
160+
161+
def generate_user_agents(file_path):
162+
with open(file_path, 'rb') as fp:
163+
capabilities_file = yaml.load(fp)
164+
for section in capabilities_file:
165+
browser = section['browserName']
166+
for platform in PLATFORMS.keys():
167+
if platform == 'Linux':
168+
os.environ['SELENIUM_PLATFORM'] = platform
169+
version = section['version']
170+
if browser == 'chrome':
171+
user_agent = 'Mozilla/5.0 ({platform}) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/{version}.0.2564.109 Safari/537.36 {identifier}'.format(platform=PLATFORMS[platform], version=version, identifier=USER_AGENT_IDENTIFIER)
172+
if platform == 'Linux':
173+
os.environ['SELENIUM_USER_AGENT_CHROME'] = user_agent
174+
elif browser == 'firefox':
175+
user_agent = 'Mozilla/5.0 ({platform}; rv:{version}.0) Gecko/20100101 Firefox/{version}.0 {identifier}'.format(platform=PLATFORMS[platform], version=version, identifier=USER_AGENT_IDENTIFIER)
176+
if platform == 'Linux':
177+
os.environ['SELENIUM_USER_AGENT'] = user_agent
178+
179+
USER_AGENTS.setdefault(platform, {})
180+
USER_AGENTS[platform][browser] = user_agent
181+
151182
def main():
152183
available_operations = ['{operation}\t{description}'.format(operation=x, description=OPERATIONS[x]) for x in sorted(OPERATIONS.keys())]
153184
available_targets = ['{target}\t{url}'.format(target=x, url=TARGETS[x]) for x in sorted(TARGETS.keys())]
@@ -161,7 +192,7 @@ def main():
161192
default='live',
162193
help='Target site for the tests.\nAvailable targets (default: live):\n\t{targets}'.format(targets='\n\t'.join(available_targets)))
163194
parser.add_argument('--config',
164-
default='env_vars.sh',
195+
default='config.cfg',
165196
help='Configuration file to load (default: config.cfg).')
166197
parser.add_argument('--libraries',
167198
default=None,
@@ -170,6 +201,9 @@ def main():
170201
action='store_true',
171202
default=False,
172203
help='Use saucelabs as the Selenium server')
204+
parser.add_argument('--capabilities',
205+
default='capabilities_firefox.yaml',
206+
help='Selenium capabilities file (default: capabilities_firefox.yaml).'.format())
173207

174208
# Parse arguments
175209
args = parser.parse_args()
@@ -185,21 +219,50 @@ def main():
185219
parser.print_help()
186220
sys.exit()
187221

222+
libraries = args.libraries
223+
if operation == 'target' and not libraries:
224+
print('No target libraries specified!\n')
225+
parser.print_help()
226+
sys.exit()
227+
188228
config = args.config
189229
if not os.path.exists(config):
190230
print('Config file:', config, 'does not exist')
191231
sys.exit()
192232

193-
libraries = args.libraries
194-
if operation == 'target' and not libraries:
195-
print('No target libraries specified!\n')
196-
parser.print_help()
233+
# Read config file
234+
config_parser = ConfigParser.RawConfigParser()
235+
config_parser.optionxform = str
236+
sections = ['common', target]
237+
try:
238+
config_parser.read(config)
239+
for section in sections:
240+
options = config_parser.options(section)
241+
for option in options:
242+
value = config_parser.get(section, option)
243+
if option == 'SAUCELABS_HUB_URL':
244+
saucelabs_user = os.environ['SAUCELABS_USER']
245+
saucelabs_key = os.environ['SAUCELABS_KEY']
246+
value = value.replace('SAUCELABS_USER', saucelabs_user)
247+
value = value.replace('SAUCELABS_KEY', saucelabs_key)
248+
os.environ[option] = value
249+
except:
250+
print('Error parsing config file:', config)
251+
print('Please check the config.cfg.template for the required format')
197252
sys.exit()
198253

199-
# Read environment variables file
200-
output = subprocess.check_output('source {}; env'.format(config), shell=True, executable=SHELL)
201-
env_vars = dict((line.split('=', 1) for line in output.splitlines()))
202-
os.environ.update(env_vars)
254+
if args.saucelabs:
255+
os.environ['CODEBENDER_SELENIUM_HUB_URL'] = os.environ['SAUCELABS_HUB_URL']
256+
else:
257+
os.environ['CODEBENDER_SELENIUM_HUB_URL'] = os.environ['LOCAL_HUB_URL']
258+
259+
capabilities = args.capabilities
260+
if capabilities:
261+
os.environ['CAPABILITIES'] = capabilities
262+
263+
# Generate User agents
264+
file_path = os.path.join(os.path.dirname(__file__), '..', 'codebender_testing', capabilities)
265+
generate_user_agents(file_path)
203266

204267
# Run tests
205268
tests = Tests(target, config)

0 commit comments

Comments
 (0)