-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathghost_extensions.py
66 lines (59 loc) · 2.36 KB
/
ghost_extensions.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
#!/usr/bin/env python
from robot.libraries.BuiltIn import BuiltIn
from robot.api import logger
class UsageError(Exception):
pass
def open_popup(locator):
"""
Clicks a web element that will open a new popup window.
"""
seleniumlib = BuiltIn().get_library_instance('Selenium2Library')
browser = seleniumlib._current_browser()
#saves the main window handle to an instance variablec
seleniumlib.main_window = browser.current_window_handle
logger.debug('Current window handle is: %s' % seleniumlib.main_window)
before = set(browser.window_handles)
seleniumlib.click_element(locator)
after = set(browser.window_handles)
try:
new_win = (after - before).pop()
except KeyError:
logger.warn("No new popup window was detected!")
raise
logger.debug('Switching to window: %s' % new_win)
browser.switch_to_window(new_win)
def close_popup():
"""
Closes a popup window that was opened with "Open Popup"
"""
seleniumlib = BuiltIn().get_library_instance('Selenium2Library')
if not hasattr(seleniumlib, 'main_window'):
raise UsageError('Popups must be opened with the same Selenium2Library'
' instance before they can be closed')
#This overrides any modal creation (e.g. "are you sure?")
seleniumlib.execute_javascript('window.onbeforeunload = function() {}')
browser = seleniumlib._current_browser()
logger.debug('Closing window: %s' % browser.current_window_handle)
seleniumlib.close_window()
logger.debug('Switching to window: %s' % seleniumlib.main_window)
browser.switch_to_window(seleniumlib.main_window)
def prep_alert():
"""
Overrides window.alert to simply store the alert message rather than
display it. This will become unnecessary once GhostDriver supports alerts
natively.
"""
seleniumlib = BuiltIn().get_library_instance('Selenium2Library')
js = """
window.alert = function(message) {
lastAlert = message;
}
"""
logger.debug('Overriding window.alert to store the alert message')
seleniumlib.execute_javascript('%s' % js)
def get_last_alert():
"""
Gets the last alert message. This command MUST be preceeded by Prep Alert
"""
seleniumlib = BuiltIn().get_library_instance('Selenium2Library')
return seleniumlib.execute_javascript('return lastAlert')