Skip to content

Commit

Permalink
Browser.get_alert() returns Alert instead of a wrapper object (#682)
Browse files Browse the repository at this point in the history
  • Loading branch information
jsfehler authored and andrewsmedina committed Jun 27, 2019
1 parent bc32668 commit cebaa3d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 29 deletions.
14 changes: 7 additions & 7 deletions docs/iframes-and-alerts.rst
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ Handling alerts and prompts

Chrome support for alerts and prompts is new in Splinter 0.4.

**IMPORTANT:** Only webdrivers (Firefox and Chrome) has support for alerts and prompts.
**IMPORTANT:** Only webdriver (Firefox and Chrome) has support for alerts and prompts.

You can deal with alerts and prompts using the ``get_alert`` method.
You can interact with alerts and prompts using the ``get_alert`` method.

.. highlight:: python

Expand All @@ -42,20 +42,20 @@ You can deal with alerts and prompts using the ``get_alert`` method.
alert.dismiss()


In case of prompts, you can answer it using the ``fill_with`` method.
In case of prompts, you can answer it using the ``send_keys`` method.

.. highlight:: python

::

prompt = browser.get_alert()
prompt.text
prompt.fill_with('text')
prompt.send_keys('text')
prompt.accept()
prompt.dismiss()


You can use the ``with`` statement to interacte with both alerts and prompts too.
You can also use the ``with`` statement to interact with both alerts and prompts.

.. highlight:: python

Expand All @@ -64,6 +64,6 @@ You can use the ``with`` statement to interacte with both alerts and prompts too
with browser.get_alert() as alert:
alert.do_stuff()

If there's not any prompt or alert, ``get_alert`` will return ``None``.
If there's no prompt or alert, ``get_alert`` will return ``None``.
Remember to always use at least one of the alert/prompt ending methods (accept and dismiss).
Otherwise your browser instance will be frozen until you accept or dismiss the alert/prompt correctly.
Otherwise, your browser instance will be frozen until you accept or dismiss the alert/prompt correctly.
35 changes: 13 additions & 22 deletions splinter/driver/webdriver/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import time
from contextlib import contextmanager

from selenium.webdriver.common.alert import Alert
from selenium.common.exceptions import NoSuchElementException, WebDriverException, StaleElementReferenceException
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support import expected_conditions as EC
Expand All @@ -30,6 +31,17 @@
_meth_func = "im_func"
_func_name = "func_name"

# Patch contextmanager onto Selenium's Alert
def alert_enter(self):
return self

def alert_exit(self, type, value, traceback):
pass

Alert.__enter__ = alert_enter
Alert.__exit__ = alert_exit
Alert.fill_with = Alert.send_keys


class switch_window:
def __init__(self, browser, window_handle):
Expand Down Expand Up @@ -310,7 +322,7 @@ def get_alert(self, wait_time=None):

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

return AlertElement(alert)
return alert

def is_text_present(self, text, wait_time=None):
wait_time = wait_time or self.wait_time
Expand Down Expand Up @@ -844,24 +856,3 @@ def screenshot_as_png(self):

def __getitem__(self, attr):
return self._element.get_attribute(attr)


class AlertElement(object):
def __init__(self, alert):
self._alert = alert
self.text = alert.text

def accept(self):
self._alert.accept()

def dismiss(self):
self._alert.dismiss()

def fill_with(self, text):
self._alert.send_keys(text)

def __enter__(self):
return self

def __exit__(self, type, value, traceback):
pass

0 comments on commit cebaa3d

Please sign in to comment.