Skip to content

Commit

Permalink
Browser.get_alert() returns None if no alert exists (#717)
Browse files Browse the repository at this point in the history
  • Loading branch information
kjkorhonen authored and andrewsmedina committed Sep 27, 2019
1 parent 4b6e09a commit d19443f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
10 changes: 6 additions & 4 deletions splinter/driver/webdriver/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import warnings

from selenium.webdriver.common.alert import Alert
from selenium.common.exceptions import NoSuchElementException, WebDriverException, StaleElementReferenceException
from selenium.common.exceptions import NoSuchElementException, WebDriverException, StaleElementReferenceException, TimeoutException
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
Expand Down Expand Up @@ -366,9 +366,11 @@ def is_element_not_present_by_id(self, id, wait_time=None):
def get_alert(self, wait_time=None):
wait_time = wait_time or self.wait_time

alert = WebDriverWait(self.driver, wait_time).until(EC.alert_is_present())

return alert
try:
alert = WebDriverWait(self.driver, wait_time).until(EC.alert_is_present())
return alert
except TimeoutException:
return None

def is_text_present(self, text, wait_time=None):
wait_time = wait_time or self.wait_time
Expand Down
5 changes: 5 additions & 0 deletions tests/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,11 @@ def test_access_alerts_using_with(self):
self.assertEqual("This is an alert example.", alert.text)
alert.accept()

def test_get_alert_return_none_if_no_alerts(self):
"should return None if no alerts available"
alert = self.browser.get_alert()
self.assertEqual(None, alert)

def test_can_select_a_option_via_element_text(self):
"should provide a way to select a option via element"
self.assertFalse(self.browser.find_option_by_value("rj").selected)
Expand Down

0 comments on commit d19443f

Please sign in to comment.