Пример #1
0
    def create_dialog(dialog_type, text=None):
        assert dialog_type in ("alert", "confirm",
                               "prompt"), ("Invalid dialog type: '%s'" %
                                           dialog_type)

        if text is None:
            text = ""

        assert isinstance(text, str), "`text` parameter must be a string"

        # Script completes itself when the user prompt has been opened.
        # For prompt() dialogs, add a value for the 'default' argument,
        # as some user agents (IE, for example) do not produce consistent
        # values for the default.
        session.execute_async_script("""
            let dialog_type = arguments[0];
            let text = arguments[1];

            setTimeout(function() {
              if (dialog_type == 'prompt') {
                window.dialog_return_value = window[dialog_type](text, '');
              } else {
                window.dialog_return_value = window[dialog_type](text);
              }
            }, 0);
            """,
                                     args=(dialog_type, text))

        wait = Poll(
            session,
            timeout=15,
            ignored_exceptions=NoSuchAlertException,
            message="No user prompt with text '{}' detected".format(text))
        wait.until(lambda s: s.alert.text == text)
Пример #2
0
    def create_dialog(dialog_type, text=None):
        assert dialog_type in ("alert", "confirm", "prompt"), (
            "Invalid dialog type: '%s'" % dialog_type)

        if text is None:
            text = ""

        assert isinstance(text, basestring), "`text` parameter must be a string"

        # Script completes itself when the user prompt has been opened.
        # For prompt() dialogs, add a value for the 'default' argument,
        # as some user agents (IE, for example) do not produce consistent
        # values for the default.
        session.execute_async_script("""
            let dialog_type = arguments[0];
            let text = arguments[1];

            setTimeout(function() {
              if (dialog_type == 'prompt') {
                window.dialog_return_value = window[dialog_type](text, '');
              } else {
                window.dialog_return_value = window[dialog_type](text);
              }
            }, 0);
            """, args=(dialog_type, text))

        wait = Poll(
            session,
            timeout=15,
            ignored_exceptions=webdriver.NoSuchAlertException,
            message="No user prompt with text '{}' detected".format(text))
        wait.until(lambda s: s.alert.text == text)
Пример #3
0
def test_link_from_nested_context_with_target(session, target):
    target_page = inline("<p id='foo'>foo</p>")

    session.url = inline(
        iframe("<a href='{}' target='{}'>click</a>".format(
            target_page, target)))
    frame = session.find.css("iframe", all=False)
    session.switch_frame(frame)
    element = session.find.css("a".format(target), all=False)

    orig_handles = session.handles

    response = element_click(session, element)
    assert_success(response)

    if target == "_blank":
        session.window_handle = wait_for_new_handle(session, orig_handles)

    # With the current browsing context removed the navigation should
    # not timeout. Switch to the target context, and wait until the expected
    # element is available.
    if target == "_parent":
        session.switch_frame("parent")
    elif target == "_top":
        session.switch_frame(None)

    wait = Poll(session,
                timeout=5,
                ignored_exceptions=NoSuchElementException,
                message="Expected element has not been found")
    wait.until(lambda s: s.find.css("#foo"))
Пример #4
0
def test_title_after_modification(session):
    def title():
        return read_global(session, "document.title")

    session.url = inline("<title>Initial</title><h2>Hello</h2>")
    session.execute_script("document.title = 'Updated'")

    wait = Poll(session, message='Document title does not match "{}"'.format(title()))
    wait.until(lambda s: assert_success(get_title(s)) == title())
Пример #5
0
def test_unexpected_alert(session):
    session.execute_script("setTimeout(function() { alert('Hello'); }, 100);")
    wait = Poll(session,
                timeout=5,
                ignored_exceptions=NoSuchAlertException,
                message="No user prompt with text 'Hello' detected")
    wait.until(lambda s: s.alert.text == "Hello")

    response = get_alert_text(session)
    assert_success(response)
Пример #6
0
def test_title_after_modification(session):
    def title():
        return read_global(session, "document.title")

    session.url = inline("<title>Initial</title><h2>Hello</h2>")
    session.execute_script("document.title = 'Updated'")

    wait = Poll(session,
                message='Document title does not match "{}"'.format(title()))
    wait.until(lambda s: assert_success(get_title(s)) == title())
Пример #7
0
def wait_for_new_handle(session, handles_before):
    def find_new_handle(session):
        new_handles = list(set(session.handles) - set(handles_before))
        if new_handles and len(new_handles) == 1:
            return new_handles[0]
        return None

    wait = Poll(session, timeout=5, message="No new window has been opened")

    return wait.until(find_new_handle)
Пример #8
0
def test_link_from_toplevel_context_with_target(session, target):
    target_page = inline("<p id='foo'>foo</p>")

    session.url = inline("<a href='{}' target='{}'>click</a>".format(
        target_page, target))
    element = session.find.css("a", all=False)

    orig_handles = session.handles

    response = element_click(session, element)
    assert_success(response)

    if target == "_blank":
        session.window_handle = wait_for_new_handle(session, orig_handles)

    wait = Poll(session,
                timeout=5,
                ignored_exceptions=NoSuchElementException,
                message="Expected element has not been found")
    wait.until(lambda s: s.find.css("#foo"))
Пример #9
0
def test_opening_new_window_keeps_current_window_handle(session, inline):
    original_handle = session.window_handle
    original_handles = session.handles

    url = inline("""<a href="javascript:window.open();">open window</a>""")
    session.url = url
    session.find.css("a", all=False).click()
    wait = Poll(session, timeout=5, message="No new window has been opened")
    new_handles = wait.until(lambda s: set(s.handles) - set(original_handles))

    assert len(new_handles) == 1
    assert session.window_handle == original_handle
    assert session.url == url
Пример #10
0
def test_abort_by_user_prompt_twice(session, dialog_type):
    response = execute_script(
        session, "window.{0}('Hello'); window.{0}('Bye'); return 1;".format(
            dialog_type))
    assert_success(response, None)

    session.alert.accept()

    # The first alert has been accepted by the user prompt handler, the second
    # alert will still be opened because the current step isn't aborted.
    wait = Poll(session,
                timeout=5,
                message="Second alert has not been opened",
                ignored_exceptions=NoSuchAlertException)
    text = wait.until(lambda s: s.alert.text)

    assert text == "Bye"

    session.alert.accept()
Пример #11
0
def test_no_abort_by_user_prompt_in_other_tab(session, inline, dialog_type):
    original_handle = session.window_handle
    original_handles = session.handles

    session.url = inline(
        """
      <a onclick="window.open();">open window</a>
      <script>
        window.addEventListener("message", function (event) {{
          {}("foo");
        }});
      </script>
    """.format(
            dialog_type
        )
    )

    session.find.css("a", all=False).click()
    wait = Poll(session, timeout=5, message="No new window has been opened")
    new_handles = wait.until(lambda s: set(s.handles) - set(original_handles))
    assert len(new_handles) == 1

    session.window_handle = new_handles.pop()

    response = execute_async_script(
        session,
        """
        const resolve = arguments[0];

        // Trigger opening a user prompt in the other window.
        window.opener.postMessage("foo", "*");

        // Delay resolving the Promise to ensure a user prompt has been opened.
        setTimeout(() => resolve(42), 500);
        """,
    )

    assert_success(response, 42)

    session.window.close()

    session.window_handle = original_handle
    session.alert.accept()
Пример #12
0
def test_link_open_target_in_new_window(session, url):
    orig_handles = session.handles

    session.url = inline("""
        <a href="{}" target="_blank">Open in new window</a>
    """.format(inline("<p id=foo")))
    element = session.find.css("a", all=False)

    response = element_click(session, element)
    assert_success(response)

    def find_new_handle(session):
        new_handles = list(set(session.handles) - set(orig_handles))
        if new_handles and len(new_handles) == 1:
            return new_handles[0]
        return None

    wait = Poll(session, timeout=5, message="No new window has been opened")
    new_handle = wait.until(find_new_handle)

    session.window_handle = new_handle
    session.find.css("#foo")