Skip to content

Commit ab1e481

Browse files
committed
Merge with master
2 parents 189b532 + 97e2e79 commit ab1e481

File tree

3 files changed

+53
-5
lines changed

3 files changed

+53
-5
lines changed

CHANGES.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,17 @@ Release Notes
55
----------------
66
- Added an argument called screenshot_root_directory that can be passed into S2L's
77
constructor to specify where to store screenshots.
8+
- Added new keyword 'set_screenshot_directory' which can be used to set the output
9+
of screenshots.
810
[zephraph]
911

12+
- Added new keyword Input Text Into Prompt
13+
- Modified 'get_alert_message' to accept a parameter 'dismiss' (defaults to true) which can be
14+
used to prevent closing the alert message and instead will just return the alerts text.
15+
Also created new keyword 'dismiss_alert' to dismiss (default) or confirm the alert without
16+
reading the text of the alert.
17+
[KingWarin]
18+
1019
- Added new keyword Input Text Into Prompt
1120
[boakley][ekasteel]
1221

src/Selenium2Library/keywords/_javascript.py

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -120,28 +120,59 @@ def execute_async_javascript(self, *code):
120120
self._info("Executing Asynchronous JavaScript:\n%s" % js)
121121
return self._current_browser().execute_async_script(js)
122122

123-
def get_alert_message(self):
123+
def get_alert_message(self, dismiss=True):
124124
"""Returns the text of current JavaScript alert.
125125
126+
By default the current JavaScript alert will be dismissed.
126127
This keyword will fail if no alert is present. Note that
127128
following keywords will fail unless the alert is
128129
dismissed by this keyword or another like `Get Alert Message`.
129130
"""
130-
return self._close_alert()
131+
if dismiss:
132+
return self._close_alert()
133+
else:
134+
return self._read_alert()
135+
136+
def dismiss_alert(self, accept=True):
137+
""" Returns true if alert was confirmed, false if it was dismissed
138+
139+
This keyword will fail if no alert is present. Note that
140+
following keywords will fail unless the alert is
141+
dismissed by this keyword or another like `Get Alert Message`.
142+
"""
143+
return self._handle_alert(accept)
131144

132145
# Private
133146

134-
def _close_alert(self, confirm=False):
147+
def _close_alert(self, confirm=True):
148+
try:
149+
text = self._read_alert()
150+
alert = self._handle_alert(confirm)
151+
return text
152+
except WebDriverException:
153+
raise RuntimeError('There were no alerts')
154+
155+
def _read_alert(self):
135156
alert = None
136157
try:
137158
alert = self._current_browser().switch_to_alert()
138159
text = ' '.join(alert.text.splitlines()) # collapse new lines chars
139-
if not confirm: alert.dismiss()
140-
else: alert.accept()
141160
return text
142161
except WebDriverException:
143162
raise RuntimeError('There were no alerts')
144163

164+
def _handle_alert(self, confirm=True):
165+
try:
166+
alert = self._current_browser().switch_to_alert()
167+
if not confirm:
168+
alert.dismiss()
169+
return False
170+
else:
171+
alert.accept()
172+
return True
173+
except WebDriverException:
174+
raise RuntimeError('There were no alerts')
175+
145176
def _get_javascript_to_execute(self, code):
146177
codepath = code.replace('/', os.sep)
147178
if not (os.path.isabs(codepath) and os.path.isfile(codepath)):

test/acceptance/keywords/javascript.robot

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,14 @@ Get Alert Message
2424
Should Be Equal ${msg} ALERT!
2525
Run Keyword And Expect Error There were no alerts Get Alert Message
2626

27+
Read Alert Message
28+
[Setup] Go To Page "javascript/alert.html"
29+
Click Link Click Me!
30+
${msg} = Get Alert Message ${FALSE}
31+
Should Be Equal ${msg} ALERT!
32+
Dismiss Alert
33+
Run Keyword And Expect Error There were no alerts Get Alert Message
34+
2735
Mouse Down On Link
2836
[TAGS] Known Issue - Firefox
2937
[Setup] Go To Page "javascript/mouse_events.html"

0 commit comments

Comments
 (0)