Path: blob/trunk/py/test/selenium/webdriver/common/executing_async_javascript_tests.py
1865 views
# Licensed to the Software Freedom Conservancy (SFC) under one1# or more contributor license agreements. See the NOTICE file2# distributed with this work for additional information3# regarding copyright ownership. The SFC licenses this file4# to you under the Apache License, Version 2.0 (the5# "License"); you may not use this file except in compliance6# with the License. You may obtain a copy of the License at7#8# http://www.apache.org/licenses/LICENSE-2.09#10# Unless required by applicable law or agreed to in writing,11# software distributed under the License is distributed on an12# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY13# KIND, either express or implied. See the License for the14# specific language governing permissions and limitations15# under the License.1617import pytest1819from selenium.common.exceptions import TimeoutException, WebDriverException20from selenium.webdriver.common.by import By21from selenium.webdriver.remote.webelement import WebElement222324@pytest.fixture(autouse=True)25def reset_timeouts(driver):26driver.set_script_timeout(5)27yield28driver.set_script_timeout(30)293031def test_should_not_timeout_if_callback_invoked_immediately(driver, pages):32pages.load("ajaxy_page.html")33result = driver.execute_async_script("arguments[arguments.length - 1](123);")34assert isinstance(result, int)35assert 123 == result363738def test_should_be_able_to_return_javascript_primitives_from_async_scripts_neither_none_nor_undefined(driver, pages):39pages.load("ajaxy_page.html")40assert 123 == driver.execute_async_script("arguments[arguments.length - 1](123);")41assert "abc" == driver.execute_async_script("arguments[arguments.length - 1]('abc');")42assert not bool(driver.execute_async_script("arguments[arguments.length - 1](false);"))43assert bool(driver.execute_async_script("arguments[arguments.length - 1](true);"))444546def test_should_be_able_to_return_javascript_primitives_from_async_scripts_null_and_undefined(driver, pages):47pages.load("ajaxy_page.html")48assert driver.execute_async_script("arguments[arguments.length - 1](null)") is None49assert driver.execute_async_script("arguments[arguments.length - 1]()") is None505152def test_should_be_able_to_return_an_array_literal_from_an_async_script(driver, pages):53pages.load("ajaxy_page.html")54result = driver.execute_async_script("arguments[arguments.length - 1]([]);")55assert "Expected not to be null!", result is not None56assert isinstance(result, list)57assert len(result) == 0585960def test_should_be_able_to_return_an_array_object_from_an_async_script(driver, pages):61pages.load("ajaxy_page.html")62result = driver.execute_async_script("arguments[arguments.length - 1](new Array());")63assert "Expected not to be null!", result is not None64assert isinstance(result, list)65assert len(result) == 0666768def test_should_be_able_to_return_arrays_of_primitives_from_async_scripts(driver, pages):69pages.load("ajaxy_page.html")7071result = driver.execute_async_script("arguments[arguments.length - 1]([null, 123, 'abc', true, false]);")7273assert result is not None74assert isinstance(result, list)75assert not bool(result.pop())76assert bool(result.pop())77assert "abc" == result.pop()78assert 123 == result.pop()79assert result.pop() is None80assert len(result) == 0818283def test_should_be_able_to_return_web_elements_from_async_scripts(driver, pages):84pages.load("ajaxy_page.html")8586result = driver.execute_async_script("arguments[arguments.length - 1](document.body);")87assert isinstance(result, WebElement)88assert "body" == result.tag_name.lower()899091@pytest.mark.xfail_safari92@pytest.mark.xfail_chrome(reason="https://bugs.chromium.org/p/chromedriver/issues/detail?id=4525")93def test_should_be_able_to_return_arrays_of_web_elements_from_async_scripts(driver, pages):94pages.load("ajaxy_page.html")9596result = driver.execute_async_script("arguments[arguments.length - 1]([document.body, document.body]);")97assert result is not None98assert isinstance(result, list)99100list_ = result101assert 2 == len(list_)102assert isinstance(list_[0], WebElement)103assert isinstance(list_[1], WebElement)104assert "body" == list_[0].tag_name105# assert list_[0] == list_[1]106107108def test_should_timeout_if_script_does_not_invoke_callback(driver, pages):109pages.load("ajaxy_page.html")110with pytest.raises(TimeoutException):111# Script is expected to be async and explicitly callback, so this should timeout.112driver.execute_async_script("return 1 + 2;")113114115def test_should_timeout_if_script_does_not_invoke_callback_with_azero_timeout(driver, pages):116pages.load("ajaxy_page.html")117with pytest.raises(TimeoutException):118driver.execute_async_script("window.setTimeout(function() {}, 0);")119120121def test_should_not_timeout_if_script_callsback_inside_azero_timeout(driver, pages):122pages.load("ajaxy_page.html")123driver.execute_async_script(124"""var callback = arguments[arguments.length - 1];125window.setTimeout(function() { callback(123); }, 0)"""126)127128129def test_should_timeout_if_script_does_not_invoke_callback_with_long_timeout(driver, pages):130driver.set_script_timeout(0.5)131pages.load("ajaxy_page.html")132with pytest.raises(TimeoutException):133driver.execute_async_script(134"""var callback = arguments[arguments.length - 1];135window.setTimeout(callback, 1500);"""136)137138139def test_should_detect_page_loads_while_waiting_on_an_async_script_and_return_an_error(driver, pages):140pages.load("ajaxy_page.html")141driver.set_script_timeout(0.1)142with pytest.raises(WebDriverException):143url = pages.url("dynamic.html")144driver.execute_async_script(f"window.location = '{url}';")145146147def test_should_catch_errors_when_executing_initial_script(driver, pages):148pages.load("ajaxy_page.html")149with pytest.raises(WebDriverException):150driver.execute_async_script("throw Error('you should catch this!');")151152153def test_should_be_able_to_execute_asynchronous_scripts(driver, pages):154pages.load("ajaxy_page.html")155156typer = driver.find_element(by=By.NAME, value="typer")157typer.send_keys("bob")158assert "bob" == typer.get_attribute("value")159160driver.find_element(by=By.ID, value="red").click()161driver.find_element(by=By.NAME, value="submit").click()162163assert 1 == len(driver.find_elements(by=By.TAG_NAME, value="div")), (164"There should only be 1 DIV at this point, which is used for the butter message"165)166driver.set_script_timeout(10)167text = driver.execute_async_script(168"""var callback = arguments[arguments.length - 1];169window.registerListener(arguments[arguments.length - 1]);"""170)171assert "bob" == text172assert "" == typer.get_attribute("value")173174assert 2 == len(driver.find_elements(by=By.TAG_NAME, value="div")), (175"There should be 1 DIV (for the butter message) + 1 DIV (for the new label)"176)177178179def test_should_be_able_to_pass_multiple_arguments_to_async_scripts(driver, pages):180pages.load("ajaxy_page.html")181result = driver.execute_async_script(182"""183arguments[arguments.length - 1](arguments[0] + arguments[1]);""",1841,1852,186)187assert 3 == result188189190# TODO DavidBurns Disabled till Java WebServer is used191# def test_should_be_able_to_make_xmlhttp_requests_and_wait_for_the_response(driver, pages):192# script = """193# var url = arguments[0];194# var callback = arguments[arguments.length - 1];195# // Adapted from http://www.quirksmode.org/js/xmlhttp.html196# var XMLHttpFactories = [197# function () return new XMLHttpRequest(),198# function () return new ActiveXObject('Msxml2.XMLHTTP'),199# function () return new ActiveXObject('Msxml3.XMLHTTP'),200# function () return new ActiveXObject('Microsoft.XMLHTTP')201# ];202# var xhr = false;203# while (!xhr && XMLHttpFactories.length)204# try{205# xhr = XMLHttpFactories.shift().call();206# }catch (e)207#208# if (!xhr) throw Error('unable to create XHR object');209# xhr.open('GET', url, true);210# xhr.onreadystatechange = function()211# if (xhr.readyState == 4) callback(xhr.responseText);212#213# xhr.send('');""" # empty string to stop firefox 3 from choking214#215# pages.load("ajaxy_page.html")216# driver.set_script_timeout(3)217# response = driver.execute_async_script(script, pages.sleepingPage + "?time=2")218# htm = "<html><head><title>Done</title></head><body>Slept for 2s</body></html>"219# assert response.strip() == htm220221222