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
- SOURCE = 'codebender_cc'
13
14
14
15
class Tests :
15
16
def __init__ (self , url , environment ):
16
- self .source = SOURCE
17
17
self .url = url
18
18
self .environment = os .path .abspath (environment )
19
19
self .
email = os .
getenv (
'EMAIL' ,
'[email protected] ' )
@@ -39,19 +39,17 @@ def run(self, operation, libraries=None):
39
39
elif operation == 'staging' :
40
40
self .staging ()
41
41
42
- def run_command (self , command , user_agent = None ):
43
- if user_agent :
44
- os .environ ['SELENIUM_USER_AGENT' ] = user_agent
42
+ def run_command (self , command ):
45
43
command = ' ' .join (command )
46
44
print ('command:' , command )
47
45
return subprocess .call (command , shell = True , executable = SHELL )
48
46
49
47
def send_mail_no_logs (self , identifier ):
50
48
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 )
49
+ self . run_command (command )
52
50
53
51
def send_mail_with_logs (self , identifier ):
54
- default_tests_dir = os .path .normpath (os .path .join (os .getcwd ( ), '..' ))
52
+ default_tests_dir = os .path .normpath (os .path .join (os .path . dirname ( __file__ ), '..' ))
55
53
root_dir = os .getenv ('ROOTDIR' , default_tests_dir )
56
54
57
55
logs = os .path .join (root_dir , 'logs' )
@@ -74,12 +72,12 @@ def send_mail_with_logs(self, identifier):
74
72
'uuencode "{reports}/{reportfile}" "{reportfile}")' .format (reports = reports , reportfile = reportfile ),
75
73
'| mail -s "Selenium Tests Report: {identifier} {email_date} Changes: {changes}" {email}' .format (identifier = identifier , email_date = email_date , changes = changes , email = self .email )
76
74
]
77
- run_command (command )
75
+ self . run_command (command )
78
76
except :
79
77
pass
80
78
81
79
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 )
80
+ return ['tox' , 'tests/' + test_directory , '--' , '--url={}' .format (TARGETS [self .url ])] + list (extra_arguments )
83
81
84
82
def common (self , identifier = 'common' ):
85
83
command = self .create_command ('common' , '--plugin' )
@@ -115,14 +113,13 @@ def noplugin(self, identifier = 'noplugin'):
115
113
def walkthrough (self , identifier = 'walkthrough' ):
116
114
command = self .create_command ('walkthrough' , '--plugin' )
117
115
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 )
116
+ for platform in USER_AGENTS .keys ():
117
+ for browser in USER_AGENTS [platform ].keys ():
118
+ os .environ ['SELENIUM_USER_AGENT_' + browser .upper ()] = USER_AGENTS [platform ][browser ]
119
+ os .environ ['SELENIUM_PLATFORM' ] = platform
120
+ retval = self .run_command (command )
125
121
retvals .append (retval )
122
+
126
123
retval = max (retvals )
127
124
if retval != 0 :
128
125
self .send_mail_no_logs (identifier )
@@ -148,6 +145,35 @@ def staging(self):
148
145
'local' : 'http://dev.codebender.cc'
149
146
}
150
147
148
+ PLATFORMS = {
149
+ 'Linux' : 'X11; Ubuntu; Linux x86_64' ,
150
+ 'Windows 7' : 'Windows NT 6.1' ,
151
+ 'OS X 10.11' : 'Macintosh; Intel Mac OS X 10_11_1'
152
+ }
153
+ USER_AGENTS = {}
154
+ USER_AGENT_IDENTIFIER = 'codebender-selenium'
155
+
156
+ def generate_user_agents (file_path ):
157
+ with open (file_path , 'rb' ) as fp :
158
+ capabilities_file = yaml .load (fp )
159
+ for section in capabilities_file :
160
+ browser = section ['browserName' ]
161
+ for platform in PLATFORMS .keys ():
162
+ if platform == 'Linux' :
163
+ os .environ ['SELENIUM_PLATFORM' ] = platform
164
+ version = section ['version' ]
165
+ if browser == 'chrome' :
166
+ 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 )
167
+ if platform == 'Linux' :
168
+ os .environ ['SELENIUM_USER_AGENT_CHROME' ] = user_agent
169
+ elif browser == 'firefox' :
170
+ 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 )
171
+ if platform == 'Linux' :
172
+ os .environ ['SELENIUM_USER_AGENT' ] = user_agent
173
+
174
+ USER_AGENTS .setdefault (platform , {})
175
+ USER_AGENTS [platform ][browser ] = user_agent
176
+
151
177
def main ():
152
178
available_operations = ['{operation}\t {description}' .format (operation = x , description = OPERATIONS [x ]) for x in sorted (OPERATIONS .keys ())]
153
179
available_targets = ['{target}\t {url}' .format (target = x , url = TARGETS [x ]) for x in sorted (TARGETS .keys ())]
@@ -161,7 +187,7 @@ def main():
161
187
default = 'live' ,
162
188
help = 'Target site for the tests.\n Available targets (default: live):\n \t {targets}' .format (targets = '\n \t ' .join (available_targets )))
163
189
parser .add_argument ('--config' ,
164
- default = 'env_vars.sh ' ,
190
+ default = 'config.cfg ' ,
165
191
help = 'Configuration file to load (default: config.cfg).' )
166
192
parser .add_argument ('--libraries' ,
167
193
default = None ,
@@ -170,6 +196,9 @@ def main():
170
196
action = 'store_true' ,
171
197
default = False ,
172
198
help = 'Use saucelabs as the Selenium server' )
199
+ parser .add_argument ('--capabilities' ,
200
+ default = 'capabilities_firefox.yaml' ,
201
+ help = 'Selenium capabilities file (default: capabilities_firefox.yaml).' .format ())
173
202
174
203
# Parse arguments
175
204
args = parser .parse_args ()
@@ -185,21 +214,47 @@ def main():
185
214
parser .print_help ()
186
215
sys .exit ()
187
216
217
+ libraries = args .libraries
218
+ if operation == 'target' and not libraries :
219
+ print ('No target libraries specified!\n ' )
220
+ parser .print_help ()
221
+ sys .exit ()
222
+
188
223
config = args .config
189
224
if not os .path .exists (config ):
190
225
print ('Config file:' , config , 'does not exist' )
191
226
sys .exit ()
192
227
193
- libraries = args .libraries
194
- if operation == 'target' and not libraries :
195
- print ('No target libraries specified!\n ' )
196
- parser .print_help ()
228
+ # Read config file
229
+ config_parser = ConfigParser .RawConfigParser ()
230
+ config_parser .optionxform = str
231
+ sections = ['common' , target ]
232
+ try :
233
+ config_parser .read (config )
234
+ for section in sections :
235
+ for option , value in config_parser .items (section ):
236
+ if option == 'SAUCELABS_HUB_URL' :
237
+ saucelabs_user = os .environ ['SAUCELABS_USER' ]
238
+ saucelabs_key = os .environ ['SAUCELABS_KEY' ]
239
+ value = value .replace ('SAUCELABS_USER' , saucelabs_user )
240
+ value = value .replace ('SAUCELABS_KEY' , saucelabs_key )
241
+ os .environ [option ] = value
242
+ except :
243
+ print ('Error parsing config file:' , config )
244
+ print ('Please check the config.cfg.template for the required format' )
197
245
sys .exit ()
198
246
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 )
247
+ os .environ ['CODEBENDER_SELENIUM_HUB_URL' ] = os .environ ['LOCAL_HUB_URL' ]
248
+ if args .saucelabs :
249
+ os .environ ['CODEBENDER_SELENIUM_HUB_URL' ] = os .environ ['SAUCELABS_HUB_URL' ]
250
+
251
+ capabilities = args .capabilities
252
+ if capabilities :
253
+ os .environ ['CAPABILITIES' ] = capabilities
254
+
255
+ # Generate User agents
256
+ file_path = os .path .join (os .path .dirname (__file__ ), '..' , 'codebender_testing' , capabilities )
257
+ generate_user_agents (file_path )
203
258
204
259
# Run tests
205
260
tests = Tests (target , config )
0 commit comments