Skip to content

Commit

Permalink
UnexpectedAlertPresentException should contain the alert text in pyth…
Browse files Browse the repository at this point in the history
…on too.

Fixes Issue SeleniumHQ#7745
  • Loading branch information
lukeis committed Sep 8, 2014
1 parent 1059079 commit e41d76b
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
7 changes: 6 additions & 1 deletion py/selenium/common/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,12 @@ class UnexpectedAlertPresentException(WebDriverException):
Usually raised when when an expected modal is blocking webdriver form executing any
more commands.
"""
pass
def __init__(self, msg=None, screen=None, stacktrace=None, alert_text=None):
super(WebDriverException, self).__init__(msg, screen, stacktrace)
self.alert_text = alert_text

def __str__(self):
return "Alert Text: %s\n%s" % (self.alert_text, str(super(WebDriverException, self)))

class NoAlertPresentException(WebDriverException):
"""
Expand Down
2 changes: 2 additions & 0 deletions py/selenium/webdriver/remote/errorhandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,8 @@ def check_response(self, response):
pass
if exception_class == ErrorInResponseException:
raise exception_class(response, message)
elif exception_class == UnexpectedAlertPresentException and 'alert' in value:
raise exception_class(message, screen, stacktrace, value['alert'].get('text'))
raise exception_class(message, screen, stacktrace)

def _value_or_default(self, obj, key, default):
Expand Down
13 changes: 13 additions & 0 deletions py/test/selenium/webdriver/common/alerts_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from selenium.common.exceptions import ElementNotVisibleException
from selenium.common.exceptions import InvalidElementStateException
from selenium.common.exceptions import NoAlertPresentException
from selenium.common.exceptions import UnexpectedAlertPresentException

import unittest

Expand Down Expand Up @@ -214,6 +215,18 @@ def testShouldAllowTheUserToGetTheTextOfAnAlert(self):
alert.accept()
self.assertEqual("cheese", value)

def testUnexpectedAlertPresentExceptionContainsAlertText(self):
self._loadPage("alerts")
self.driver.find_element(by=By.ID, value="alert").click()
alert = self._waitForAlert()
value = alert.text
try:
self._loadPage("simpleTest")
raise Exception("UnexpectedAlertPresentException should have been thrown")
except UnexpectedAlertPresentException as uape:
self.assertEquals(value, uape.alert_text)
self.assertTrue(str(uape).startswith("Alert Text: %s" % value))

def _waitForAlert(self):
return WebDriverWait(self.driver, 3).until(EC.alert_is_present())

Expand Down

0 comments on commit e41d76b

Please sign in to comment.