1
1
#!/usr/bin/env python
2
2
3
3
from __future__ import print_function
4
+ import ConfigParser
4
5
import argparse
5
6
import os
6
7
import re
7
8
import subprocess
8
9
import sys
9
10
import time
11
+ import yaml
10
12
11
13
SHELL = '/bin/bash'
12
14
SOURCE = 'codebender_cc'
@@ -39,19 +41,17 @@ def run(self, operation, libraries=None):
39
41
elif operation == 'staging' :
40
42
self .staging ()
41
43
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 ):
45
45
command = ' ' .join (command )
46
46
print ('command:' , command )
47
47
return subprocess .call (command , shell = True , executable = SHELL )
48
48
49
49
def send_mail_no_logs (self , identifier ):
50
50
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 )
52
52
53
53
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__ ), '..' ))
55
55
root_dir = os .getenv ('ROOTDIR' , default_tests_dir )
56
56
57
57
logs = os .path .join (root_dir , 'logs' )
@@ -74,12 +74,12 @@ def send_mail_with_logs(self, identifier):
74
74
'uuencode "{reports}/{reportfile}" "{reportfile}")' .format (reports = reports , reportfile = reportfile ),
75
75
'| mail -s "Selenium Tests Report: {identifier} {email_date} Changes: {changes}" {email}' .format (identifier = identifier , email_date = email_date , changes = changes , email = self .email )
76
76
]
77
- run_command (command )
77
+ self . run_command (command )
78
78
except :
79
79
pass
80
80
81
81
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 )
83
83
84
84
def common (self , identifier = 'common' ):
85
85
command = self .create_command ('common' , '--plugin' )
@@ -115,14 +115,16 @@ def noplugin(self, identifier = 'noplugin'):
115
115
def walkthrough (self , identifier = 'walkthrough' ):
116
116
command = self .create_command ('walkthrough' , '--plugin' )
117
117
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 )
125
126
retvals .append (retval )
127
+
126
128
retval = max (retvals )
127
129
if retval != 0 :
128
130
self .send_mail_no_logs (identifier )
@@ -148,6 +150,35 @@ def staging(self):
148
150
'local' : 'http://dev.codebender.cc'
149
151
}
150
152
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
+
151
182
def main ():
152
183
available_operations = ['{operation}\t {description}' .format (operation = x , description = OPERATIONS [x ]) for x in sorted (OPERATIONS .keys ())]
153
184
available_targets = ['{target}\t {url}' .format (target = x , url = TARGETS [x ]) for x in sorted (TARGETS .keys ())]
@@ -161,7 +192,7 @@ def main():
161
192
default = 'live' ,
162
193
help = 'Target site for the tests.\n Available targets (default: live):\n \t {targets}' .format (targets = '\n \t ' .join (available_targets )))
163
194
parser .add_argument ('--config' ,
164
- default = 'env_vars.sh ' ,
195
+ default = 'config.cfg ' ,
165
196
help = 'Configuration file to load (default: config.cfg).' )
166
197
parser .add_argument ('--libraries' ,
167
198
default = None ,
@@ -170,6 +201,9 @@ def main():
170
201
action = 'store_true' ,
171
202
default = False ,
172
203
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 ())
173
207
174
208
# Parse arguments
175
209
args = parser .parse_args ()
@@ -185,21 +219,50 @@ def main():
185
219
parser .print_help ()
186
220
sys .exit ()
187
221
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
+
188
228
config = args .config
189
229
if not os .path .exists (config ):
190
230
print ('Config file:' , config , 'does not exist' )
191
231
sys .exit ()
192
232
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' )
197
252
sys .exit ()
198
253
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 )
203
266
204
267
# Run tests
205
268
tests = Tests (target , config )
0 commit comments