Skip to content

Commit

Permalink
Merge 40921c9 into a77e2b9
Browse files Browse the repository at this point in the history
  • Loading branch information
digitronik committed May 27, 2020
2 parents a77e2b9 + 40921c9 commit 904b8fc
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/widgetastic/browser.py
Expand Up @@ -740,7 +740,7 @@ def get_alert(self):
"""
if not self.handles_alerts:
return None
return self.selenium.switch_to_alert()
return self.selenium.switch_to.alert

@property
def alert_present(self):
Expand Down
2 changes: 1 addition & 1 deletion src/widgetastic/widget/base.py
Expand Up @@ -776,7 +776,7 @@ def view_process(cls_or_descriptor):
isinstance(cls_or_descriptor, WidgetDescriptor) or
(inspect.isclass(cls_or_descriptor) and issubclass(cls_or_descriptor, Widget))):
raise TypeError(
'Unsupported object registered into the selector (!r})'.format(
'Unsupported object registered into the selector ({!r})'.format(
cls_or_descriptor))
self.registered_views.append((condition, cls_or_descriptor))
if default:
Expand Down
17 changes: 17 additions & 0 deletions testing/html/testing_page.html
Expand Up @@ -28,6 +28,23 @@ <h2 style="display: none;" id="invisible">This is invisible</h2>
<input type='text' id='input' />
<input type='file' id='fileinput' />
<input type='color' id='colourinput' />


<button id="alert_button" onclick="alertFunction()">Alert</button>
<p id="alert_out"></p>
<script>
function alertFunction() {
var txt;
var widget = prompt("Please enter widget name:", "TextBox");
if (widget == null || widget == "") {
txt = "User dismissed alert.";
} else {
txt = "User accepted alert:"+ widget;
}
document.getElementById("alert_out").innerHTML = txt;
}
</script>

<form id="testform">
<h3>test test</h3>
<input name="input1" type="text" />
Expand Down
48 changes: 48 additions & 0 deletions testing/test_browser.py
Expand Up @@ -18,6 +18,17 @@ def _close_window():
return browser.current_window_handle, handle


@pytest.fixture()
def invoke_alert(browser):
"""fixture to invoke sample alert."""
alert_btn = browser.element('#alert_button')
alert_btn.click()
yield
if browser.alert_present:
alert = browser.get_alert()
alert.dismiss()


def test_is_displayed(browser):
assert browser.is_displayed('#hello')

Expand Down Expand Up @@ -289,3 +300,40 @@ def test_switch_to_window(browser, current_and_new_handle):
assert new_handle == browser.current_window_handle
browser.switch_to_window(main_handle)
assert main_handle == browser.current_window_handle


def test_alert(browser):
"""Test alert_present, get_alert object"""
assert not browser.alert_present
alert_btn = browser.element('#alert_button')
alert_btn.click()
assert browser.alert_present

alert = browser.get_alert()
assert alert.text == "Please enter widget name:"
alert.dismiss()
assert not browser.alert_present


def test_dismiss_any_alerts(browser, invoke_alert):
"""Test dismiss_any_alerts"""
assert browser.alert_present
browser.dismiss_any_alerts()
assert not browser.alert_present


@pytest.mark.parametrize(
"cancel_text",
[(True, "User dismissed alert."), (False, "User accepted alert:")],
ids=["dismiss", "accept"],
)
@pytest.mark.parametrize("prompt", [None, "Input"], ids=["without_prompt", "with_prompt"])
def test_handle_alert(browser, cancel_text, prompt, invoke_alert):
"""Test handle_alert method with cancel and prompt"""
cancel, alert_out_text = cancel_text
assert browser.alert_present
assert browser.handle_alert(cancel=cancel, prompt=prompt)
if not cancel:
alert_out_text = alert_out_text + ("Input" if prompt else "TextBox")
assert browser.text("#alert_out") == alert_out_text
assert not browser.alert_present

0 comments on commit 904b8fc

Please sign in to comment.