Skip to content

Commit e41d76b

Browse files
committed
UnexpectedAlertPresentException should contain the alert text in python too.
Fixes Issue #7745
1 parent 1059079 commit e41d76b

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

py/selenium/common/exceptions.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,12 @@ class UnexpectedAlertPresentException(WebDriverException):
124124
Usually raised when when an expected modal is blocking webdriver form executing any
125125
more commands.
126126
"""
127-
pass
127+
def __init__(self, msg=None, screen=None, stacktrace=None, alert_text=None):
128+
super(WebDriverException, self).__init__(msg, screen, stacktrace)
129+
self.alert_text = alert_text
130+
131+
def __str__(self):
132+
return "Alert Text: %s\n%s" % (self.alert_text, str(super(WebDriverException, self)))
128133

129134
class NoAlertPresentException(WebDriverException):
130135
"""

py/selenium/webdriver/remote/errorhandler.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,8 @@ def check_response(self, response):
161161
pass
162162
if exception_class == ErrorInResponseException:
163163
raise exception_class(response, message)
164+
elif exception_class == UnexpectedAlertPresentException and 'alert' in value:
165+
raise exception_class(message, screen, stacktrace, value['alert'].get('text'))
164166
raise exception_class(message, screen, stacktrace)
165167

166168
def _value_or_default(self, obj, key, default):

py/test/selenium/webdriver/common/alerts_tests.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from selenium.common.exceptions import ElementNotVisibleException
2121
from selenium.common.exceptions import InvalidElementStateException
2222
from selenium.common.exceptions import NoAlertPresentException
23+
from selenium.common.exceptions import UnexpectedAlertPresentException
2324

2425
import unittest
2526

@@ -214,6 +215,18 @@ def testShouldAllowTheUserToGetTheTextOfAnAlert(self):
214215
alert.accept()
215216
self.assertEqual("cheese", value)
216217

218+
def testUnexpectedAlertPresentExceptionContainsAlertText(self):
219+
self._loadPage("alerts")
220+
self.driver.find_element(by=By.ID, value="alert").click()
221+
alert = self._waitForAlert()
222+
value = alert.text
223+
try:
224+
self._loadPage("simpleTest")
225+
raise Exception("UnexpectedAlertPresentException should have been thrown")
226+
except UnexpectedAlertPresentException as uape:
227+
self.assertEquals(value, uape.alert_text)
228+
self.assertTrue(str(uape).startswith("Alert Text: %s" % value))
229+
217230
def _waitForAlert(self):
218231
return WebDriverWait(self.driver, 3).until(EC.alert_is_present())
219232

0 commit comments

Comments
 (0)