-
Notifications
You must be signed in to change notification settings - Fork 5
/
siderunner.py
411 lines (334 loc) · 12.3 KB
/
siderunner.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
"""
Program.....: sidetest.py
Author......: Herb Lainchbury
License.....: (C) 2012 Dynamic Solutions Inc.
Description.: SeleniumIDE test runner for Python
Runs tests built in Selenium IDE using WebDriver in place without the need
to export those tests.
TODO:
* subclass tests from unittest.TestCase?
"""
# pylint: disable=C0111, C0103, R0201
import os
import xml.dom.minidom
import logging
import unittest
from pyvirtualdisplay import Display
from selenium import webdriver
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
__all__ = ['SeleniumTestCase', 'SeleniumTestSuite', 'SeleniumTests']
logger = logging.getLogger(__name__)
selenium_logger = logging.getLogger('selenium')
selenium_logger.setLevel(logging.INFO)
easyprocess_logger = logging.getLogger('easyprocess')
easyprocess_logger.setLevel(logging.INFO)
target_cache = {}
def to_text(node):
"""return node text"""
if hasattr(node, 'data'):
return node.data
elif node.toxml() == '<br/>':
return '\n'
else:
return ''
def get_command(nodes):
"""get node text"""
result = []
for node in nodes:
if node.childNodes == []:
result.append(None)
else:
result.append(''.join(to_text(n) for n in node.childNodes))
return result
def find_element(driver, target):
"""find an element in the page"""
if target in target_cache:
target = target_cache[target]
if target.startswith('link='):
try:
return driver.find_element_by_link_text(target[5:])
except NoSuchElementException:
# try lowercase version of link, work around text-transform bug
result = driver.find_element_by_link_text(target[5:].lower())
target_cache[target] = 'link=' + target[5:].lower()
msg = ' label %s is being cached as %s'
logger.info(msg, target, target_cache[target])
return result
elif target.startswith('//'):
return driver.find_element_by_xpath(target)
elif target.startswith('xpath='):
return driver.find_element_by_xpath(target[6:])
elif target.startswith('css='):
return driver.find_element_by_css_selector(target[4:])
elif target.startswith('id='):
return driver.find_element_by_id(target[3:])
elif target.startswith('name='):
return driver.find_element_by_name(target[5:])
else:
direct = (
driver.find_element_by_name(target) or
driver.find_element_by_id(target) or
driver.find_element_by_link_text(target)
)
if direct:
return direct
raise Exception('Don\'t know how to find %s' % target)
class SeleniumTestCase(object):
"""A Single Selenium Test Case"""
def __init__(self, filename, callback=None):
self.filename = filename
self.callback = callback
self.base_url = None
self.commands = []
document = open(filename).read()
dom = xml.dom.minidom.parseString(document)
rows = dom.getElementsByTagName('tr')
for row in rows[1:]:
self.commands.append(get_command(row.getElementsByTagName('td')))
for command in self.commands:
if not hasattr(self, str(command[0])):
raise Exception('Unknown Selenium IDE command %s' % command)
def run(self, driver, url):
self.base_url = url
logger.info('running '+self.filename)
for command in self.commands:
method = getattr(self, str(command[0]))
args = []
if command[1] is not None:
args.append(command[1])
if command[2] is not None:
args.append(command[2])
logger.info(
' ' + ' '.join(
[command[0]]+[repr(a) for a in args]
).splitlines()[0]
)
method(driver, *args)
if self.callback:
self.callback(driver.page_source)
def open(self, driver, url):
driver.get(self.base_url + url)
def click(self, driver, target):
element = find_element(driver, target)
driver.execute_script("arguments[0].scrollIntoView();", element)
element.click()
def clickAndWait(self, driver, target):
self.click(driver, target)
def type(self, driver, target, text=''):
element = find_element(driver, target)
element.click()
element.clear()
element.send_keys(text)
def select(self, driver, target, value):
element = find_element(driver, target)
if value.startswith('label='):
Select(element).select_by_visible_text(value[6:])
else:
msg = "Don\'t know how to select %s on %s"
raise Exception(msg % (value, target))
def verifyTextPresent(self, driver, text):
try:
source = driver.page_source
assert bool(text in source)
except:
print(
'verifyTextPresent: ',
repr(text),
'not present in',
repr(source)
)
raise
def verifyTextNotPresent(self, driver, text):
try:
assert not bool(text in driver.page_source)
except:
print(
'verifyNotTextPresent: ',
repr(text),
'present'
)
raise
def assertElementPresent(self, driver, target):
try:
assert bool(find_element(driver, target))
except:
print('assertElementPresent: ', repr(target), 'not present')
raise
def verifyElementPresent(self, driver, target):
try:
assert bool(find_element(driver, target))
except:
print('verifyElementPresent: ', repr(target), 'not present')
raise
def verifyElementNotPresent(self, driver, target):
present = True
try:
find_element(driver, target)
except NoSuchElementException:
present = False
try:
assert not present
except:
print('verifyElementNotPresent: ', repr(target), 'present')
raise
def waitForTextPresent(self, driver, text):
try:
assert bool(text in driver.page_source)
except:
print('waitForTextPresent: ', repr(text), 'not present')
raise
def waitForTextNotPresent(self, driver, text):
try:
assert not bool(text in driver.page_source)
except:
print('waitForTextNotPresent: ', repr(text), 'present')
raise
def assertText(self, driver, target, value=u''):
try:
target_value = find_element(driver, target).text
logger.info(' assertText target value =' + repr(target_value))
if value.startswith('exact:'):
assert target_value == value[len('exact:'):]
else:
assert target_value == value
except:
print(
'assertText: ',
repr(target),
repr(find_element(driver, target).get_attribute('value')),
repr(value),
)
raise
def assertNotText(self, driver, target, value=u''):
try:
target_value = find_element(driver, target).text
logger.info(' assertNotText target value =' + repr(target_value))
if value.startswith('exact:'):
assert target_value != value[len('exact:'):]
else:
assert target_value != value
except:
print(
'assertNotText: ',
repr(target),
repr(find_element(driver, target).get_attribute('value')),
repr(value),
)
raise
def assertValue(self, driver, target, value=u''):
try:
target_value = find_element(driver, target).get_attribute('value')
logger.info(' assertValue target value ='+repr(target_value))
assert target_value == value
except:
print(
'assertValue: ',
repr(target),
repr(find_element(driver, target).get_attribute('value')),
repr(value),
)
raise
def assertNotValue(self, driver, target, value=u''):
try:
target_value = find_element(driver, target).get_attribute('value')
logger.info(' assertNotValue target value ='+repr(target_value))
assert target_value != value
except:
print(
'assertNotValue: ',
repr(target),
repr(target_value),
repr(value),
)
raise
def verifyValue(self, driver, target, value=u''):
try:
target_value = find_element(driver, target).get_attribute('value')
logger.info(' verifyValue target value ='+repr(target_value))
assert target_value == value
except:
print(
'verifyValue: ',
repr(target),
repr(find_element(driver, target).get_attribute('value')),
repr(value),
)
raise
def selectWindow(self, driver, window):
pass
class SeleniumTestSuite(object):
"""A Selenium Test Suite"""
def __init__(self, filename, callback=None):
def get_test_rows(dom):
return dom.getElementsByTagName('tr')
def get_suite_title(row):
return row.getElementsByTagName('b')[0].childNodes[0].data
def get_test_title(row):
return row.getElementsByTagName('a')[0].childNodes[0].data
def get_filename(row):
return row.getElementsByTagName('a')[0].attributes.items()[0][1]
path = os.path.abspath(os.path.split(filename)[0])
self.callback = callback
document = open(filename).read()
dom = xml.dom.minidom.parseString(document)
self.tests = []
rows = get_test_rows(dom)
self.title = get_suite_title(rows[0])
for row in rows[1:]:
title = get_test_title(row)
test_filename = get_filename(row)
pathname = os.path.join(path, test_filename)
logger.debug('loading test: %s', pathname)
test_case = SeleniumTestCase(pathname, self.callback)
self.tests.append((title, test_case))
def run(self, driver, url):
for title, test in self.tests:
try:
test.run(driver, url)
except:
print('Error in %s (%s)' % (title, test.filename))
raise
def __repr__(self):
tests = '\n'.join(['%s - %s' % (title, test.filename)
for title, test in self.tests])
return '%s\n%s' % (self.title, tests)
class SeleniumTests(unittest.TestCase):
"""A Set of Selenium Test Suites"""
url = 'http://localhost'
headless = True
size = (1024, 768)
logger = logging.getLogger(__name__)
path = '.'
def get_driver(self):
return webdriver.Chrome()
def setUp(self):
if self.headless:
self.logger.info('running headless')
self.display = Display(visible=0, size=self.size)
self.display.start()
else:
self.logger.info('not running headless')
self.driver = self.get_driver()
self.driver.set_window_size(*self.size)
self.driver.implicitly_wait(10)
def tearDown(self):
if self.headless:
self.display.stop()
self.driver.quit()
def run_suite(self, suite_name):
# logger = self.logger
logger.debug('running test against %s', self.url)
suite_file = os.path.join(self.path, suite_name)
if os.path.isfile(suite_file):
tests = SeleniumTestSuite(suite_file, self.check_for_errors)
try:
logger.debug('running suite %s', suite_file)
tests.run(self.driver, self.url)
except:
self.driver.save_screenshot('%s-error_screen.png' % suite_name)
raise
else:
raise Exception('suite %r missing' % suite_file)
def check_for_errors(self, text):
pass