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)
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"))
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)
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())
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)
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)
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
def test_click_navigation(session, url): destination = url("/webdriver/tests/actions/support/test_actions_wdspec.html") start = link_doc(destination) def click(link): mouse_chain = session.actions.sequence( "pointer", "pointer_id", {"pointerType": "mouse"}) mouse_chain.click(element=link).perform() session.url = start error_message = "Did not navigate to %s" % destination click(session.find.css("#link", all=False)) Poll(session, message=error_message).until(lambda s: s.url == destination) # repeat steps to check behaviour after document unload session.url = start click(session.find.css("#link", all=False)) Poll(session, message=error_message).until(lambda s: s.url == destination)
def test_get_current_url_after_modified_location(session): start = get_current_url(session) session.execute_script( "window.location.href = 'about:blank#wd_test_modification'") Poll(session, message="URL did not change").until( lambda s: get_current_url(s).body["value"] != start.body["value"]) response = get_current_url(session) assert_success(response, "about:blank#wd_test_modification")
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()
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()
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"))
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")
def document_hidden(session): """Polls for the document to become hidden.""" def hidden(session): return session.execute_script("return document.hidden") return Poll(session, timeout=3, raises=None).until(hidden)