@@ -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 )):
0 commit comments