def create_dialog(dialog_type, text=None, result_var=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" if result_var is None: result_var = "__WEBDRIVER" assert re.search(r"^[_$a-z$][_$a-z0-9]*$", result_var, re.IGNORECASE), ( 'The `result_var` must be a valid JavaScript identifier') # Script completion and modal summoning are scheduled on two separate # turns of the event loop to ensure that both occur regardless of how # the user agent manages script execution. spawn = """ var done = arguments[0]; setTimeout(done, 0); setTimeout(function() {{ window.{0} = window.{1}("{2}"); }}, 0); """.format(result_var, dialog_type, text) session.send_session_command("POST", "execute/async", {"script": spawn, "args": []}) wait(session, lambda s: s.send_session_command("GET", "alert/text") == text, "modal has not appeared", timeout=15, ignored_exceptions=webdriver.NoSuchAlertException)
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. session.execute_async_script(""" let dialog_type = arguments[0]; let text = arguments[1]; setTimeout(function() { window.dialog_return_value = window[dialog_type](text); }, 0); """, args=(dialog_type, text)) wait(session, lambda s: s.alert.text == text, "No user prompt with text '{}' detected".format(text), timeout=15, ignored_exceptions=webdriver.NoSuchAlertException)
def test_title_after_modification(session): session.url = inline("<title>Initial</title><h2>Hello</h2>") session.execute_script("document.title = 'Updated'") wait(session, lambda s: assert_success(get_title(s)) == read_global(session, "document.title"), "Document title doesn't match '{}'".format(read_global(session, "document.title")))
def create_dialog(dialog_type, text=None, result_var=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" if result_var is None: result_var = "__WEBDRIVER" assert re.search( r"^[_$a-z$][_$a-z0-9]*$", result_var, re.IGNORECASE), ( 'The `result_var` must be a valid JavaScript identifier') # Script completes itself when the user prompt has been opened. session.execute_async_script(""" setTimeout(function() {{ window.{0} = window.{1}("{2}"); }}, 0); """.format(result_var, dialog_type, text)) wait(session, lambda s: s.alert.text == text, "No user prompt with text '{}' detected".format(text), timeout=15, ignored_exceptions=webdriver.NoSuchAlertException)
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(session, lambda s: s.alert.text == text, "No user prompt with text '{}' detected".format(text), timeout=15, ignored_exceptions=webdriver.NoSuchAlertException)
def create_dialog(dialog_type, text=None, result_var=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" if result_var is None: result_var = "__WEBDRIVER" assert re.search(r"^[_$a-z$][_$a-z0-9]*$", result_var, re.IGNORECASE), ( 'The `result_var` must be a valid JavaScript identifier') # Script completes itself when the user prompt has been opened. session.execute_async_script(""" setTimeout(function() {{ window.{0} = window.{1}("{2}"); }}, 0); """.format(result_var, dialog_type, text)) wait(session, lambda s: s.alert.text == text, "No user prompt with text '{}' detected".format(text), timeout=15, ignored_exceptions=webdriver.NoSuchAlertException)
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'") wait(session, lambda _: get_current_url(session).body["value"] != start.body["value"], "URL did not change") result = get_current_url(session) assert_success(result, "about:blank#wd_test_modification")
def test_get_current_url_after_modified_location(session): start = session.transport.send("GET", "session/%s/url" % session.session_id) session.execute_script("window.location.href = 'about:blank#wd_test_modification'") wait(session, lambda s: s.transport.send("GET", "session/%s/url" % session.session_id) != start.body["value"], "URL did not change") result = session.transport.send("GET", "session/%s/url" % session.session_id) assert_success(result, "about:blank#wd_test_modification")
def test_get_current_url_after_modified_location(session): start = session.transport.send("GET", "session/%s/url" % session.session_id) session.execute_script( "window.location.href = 'about:blank#wd_test_modification'") wait( session, lambda s: s.transport.send("GET", "session/%s/url" % session.session_id ) != start.body["value"], "URL did not change") result = session.transport.send("GET", "session/%s/url" % session.session_id) assert_success(result, "about:blank#wd_test_modification")
def test_click_navigation(session, url, release_actions): 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)) wait(session, lambda s: s.url == destination, error_message) # repeat steps to check behaviour after document unload session.url = start click(session.find.css("#link", all=False)) wait(session, lambda s: s.url == destination, error_message)