Path: blob/trunk/py/test/selenium/webdriver/common/executing_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 WebDriverException20from selenium.webdriver.common.by import By21from selenium.webdriver.remote.webelement import WebElement2223try:24str = unicode25except NameError:26pass272829def test_should_be_able_to_execute_simple_javascript_and_return_astring(driver, pages):30pages.load("xhtmlTest.html")3132result = driver.execute_script("return document.title")3334assert isinstance(result, str), "The type of the result is %s" % type(result)35assert "XHTML Test Page" == result363738def test_should_be_able_to_execute_simple_javascript_and_return_an_integer(driver, pages):39pages.load("nestedElements.html")40result = driver.execute_script("return document.getElementsByName('checky').length")4142assert isinstance(result, int)43assert int(result) > 1444546def test_should_be_able_to_execute_simple_javascript_and_return_aweb_element(driver, pages):47pages.load("xhtmlTest.html")4849result = driver.execute_script("return document.getElementById('id1')")5051assert result is not None52assert isinstance(result, WebElement)53assert "a" == result.tag_name.lower()545556def test_should_be_able_to_execute_simple_javascript_and_return_alist_of_web_elements(driver, pages):57pages.load("xhtmlTest.html")5859result = driver.execute_script("return document.querySelectorAll('div.navigation a')")6061assert result is not None62assert isinstance(result, list)63assert all(isinstance(item, WebElement) for item in result)64assert all("a" == item.tag_name.lower() for item in result)656667def test_should_be_able_to_execute_simple_javascript_and_return_web_elements_inside_alist(driver, pages):68pages.load("xhtmlTest.html")6970result = driver.execute_script("return [document.body]")7172assert result is not None73assert isinstance(result, list)74assert isinstance(result[0], WebElement)757677def test_should_be_able_to_execute_simple_javascript_and_return_web_elements_inside_anested_list(driver, pages):78pages.load("xhtmlTest.html")7980result = driver.execute_script("return [document.body, [document.getElementById('id1')]]")8182assert result is not None83assert isinstance(result, list)84assert isinstance(result[0], WebElement)85assert isinstance(result[1][0], WebElement)868788def test_should_be_able_to_execute_simple_javascript_and_return_web_elements_inside_adict(driver, pages):89pages.load("xhtmlTest.html")9091result = driver.execute_script("return {el1: document.body}")9293assert result is not None94assert isinstance(result, dict)95assert isinstance(result.get("el1"), WebElement)969798def test_should_be_able_to_execute_simple_javascript_and_return_web_elements_inside_anested_dict(driver, pages):99pages.load("xhtmlTest.html")100101result = driver.execute_script("return {el1: document.body, nested: {el2: document.getElementById('id1')}}")102103assert result is not None104assert isinstance(result, dict)105assert isinstance(result.get("el1"), WebElement)106assert isinstance(result.get("nested").get("el2"), WebElement)107108109def test_should_be_able_to_execute_simple_javascript_and_return_web_elements_inside_alist_inside_adict(driver, pages):110pages.load("xhtmlTest.html")111112result = driver.execute_script("return {el1: [document.body]}")113114assert result is not None115assert isinstance(result, dict)116assert isinstance(result.get("el1"), list)117assert isinstance(result.get("el1")[0], WebElement)118119120def test_should_be_able_to_execute_simple_javascript_and_return_aboolean(driver, pages):121pages.load("xhtmlTest.html")122123result = driver.execute_script("return true")124125assert result is not None126assert isinstance(result, bool)127assert bool(result)128129130def test_should_be_able_to_execute_simple_javascript_and_astrings_array(driver, pages):131pages.load("javascriptPage.html")132expectedResult = []133expectedResult.append("zero")134expectedResult.append("one")135expectedResult.append("two")136result = driver.execute_script("return ['zero', 'one', 'two']")137138assert expectedResult == result139140141def test_should_be_able_to_execute_simple_javascript_and_return_an_array(driver, pages):142pages.load("javascriptPage.html")143expectedResult = []144expectedResult.append("zero")145subList = []146subList.append(True)147subList.append(False)148expectedResult.append(subList)149result = driver.execute_script("return ['zero', [true, false]]")150assert result is not None151assert isinstance(result, list)152assert expectedResult == result153154155def test_passing_and_returning_an_int_should_return_awhole_number(driver, pages):156pages.load("javascriptPage.html")157expectedResult = 1158result = driver.execute_script("return arguments[0]", expectedResult)159assert isinstance(result, int)160assert expectedResult == result161162163def test_passing_and_returning_adouble_should_return_adecimal(driver, pages):164pages.load("javascriptPage.html")165expectedResult = 1.2166result = driver.execute_script("return arguments[0]", expectedResult)167assert isinstance(result, float)168assert expectedResult == result169170171def test_should_throw_an_exception_when_the_javascript_is_bad(driver, pages):172pages.load("xhtmlTest.html")173with pytest.raises(WebDriverException):174driver.execute_script("return squiggle()")175176177def test_should_be_able_to_call_functions_defined_on_the_page(driver, pages):178pages.load("javascriptPage.html")179driver.execute_script("displayMessage('I like cheese')")180text = driver.find_element(By.ID, "result").text181assert "I like cheese" == text.strip()182183184def test_should_be_able_to_pass_astring_an_as_argument(driver, pages):185pages.load("javascriptPage.html")186value = driver.execute_script("return arguments[0] == 'fish' ? 'fish' : 'not fish'", "fish")187assert "fish" == value188189190def test_should_be_able_to_pass_aboolean_an_as_argument(driver, pages):191pages.load("javascriptPage.html")192value = bool(driver.execute_script("return arguments[0] == true", True))193assert value194195196def test_should_be_able_to_pass_anumber_an_as_argument(driver, pages):197pages.load("javascriptPage.html")198value = bool(driver.execute_script("return arguments[0] == 1 ? true : false", 1))199assert value200201202def test_should_be_able_to_pass_aweb_element_as_argument(driver, pages):203pages.load("javascriptPage.html")204button = driver.find_element(By.ID, "plainButton")205value = driver.execute_script(206"arguments[0]['flibble'] = arguments[0].getAttribute('id'); return arguments[0]['flibble']", button207)208assert "plainButton" == value209210211def test_should_be_able_to_pass_an_array_as_argument(driver, pages):212pages.load("javascriptPage.html")213array = ["zero", 1, True, 3.14159]214length = int(driver.execute_script("return arguments[0].length", array))215assert len(array) == length216217218def test_should_be_able_to_pass_acollection_as_argument(driver, pages):219pages.load("javascriptPage.html")220collection = []221collection.append("Cheddar")222collection.append("Brie")223collection.append(7)224length = int(driver.execute_script("return arguments[0].length", collection))225assert len(collection) == length226227collection = []228collection.append("Gouda")229collection.append("Stilton")230collection.append("Stilton")231collection.append(True)232length = int(driver.execute_script("return arguments[0].length", collection))233assert len(collection) == length234235236def test_should_throw_an_exception_if_an_argument_is_not_valid(driver, pages):237pages.load("javascriptPage.html")238with pytest.raises(Exception):239driver.execute_script("return arguments[0]", driver)240241242def test_should_be_able_to_pass_in_more_than_one_argument(driver, pages):243pages.load("javascriptPage.html")244result = driver.execute_script("return arguments[0] + arguments[1]", "one", "two")245assert "onetwo" == result246247248def test_javascript_string_handling_should_work_as_expected(driver, pages):249pages.load("javascriptPage.html")250value = driver.execute_script("return ''")251assert "" == value252253value = driver.execute_script("return undefined")254assert value is None255256value = driver.execute_script("return ' '")257assert " " == value258259260def test_should_be_able_to_create_apersistent_value(driver, pages):261pages.load("formPage.html")262driver.execute_script("document.alerts = []")263driver.execute_script("document.alerts.push('hello world')")264text = driver.execute_script("return document.alerts.shift()")265assert "hello world" == text266267268def test_can_pass_adictionary_as_aparameter(driver, pages):269pages.load("simpleTest.html")270nums = [1, 2]271args = {"bar": "test", "foo": nums}272res = driver.execute_script("return arguments[0]['foo'][1]", args)273assert 2 == res274275276def test_can_pass_anone(driver, pages):277pages.load("simpleTest.html")278res = driver.execute_script("return arguments[0] === null", None)279assert res280281282def test_can_return_a_const(driver, pages):283pages.load("simpleTest.html")284res = driver.execute_script("const cheese='cheese'; return cheese")285assert res == "cheese"286287288def test_can_return_a_const_in_a_page(driver, pages):289pages.load("const_js.html")290res = driver.execute_script("return makeMeA('sandwich');")291assert res == "cheese sandwich"292293294@pytest.mark.xfail_remote295@pytest.mark.xfail_firefox296def test_can_return_global_const(driver, pages):297pages.load("const_js.html")298# cheese is a variable with "cheese" in it299res = driver.execute_script("return cheese")300assert res == "cheese"301302303