Path: blob/trunk/py/test/selenium/webdriver/common/script_pinning_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.16import re1718import pytest1920from selenium.common.exceptions import JavascriptException21from selenium.webdriver.remote.script_key import ScriptKey222324def test_should_allow_script_pinning(driver, pages):25pages.load("simpleTest.html")26driver.pinned_scripts = {}27script_key = driver.pin_script("return 'i like cheese';")2829result = driver.execute_script(script_key)3031assert result == "i like cheese"323334def test_should_allow_pinned_scripts_to_take_arguments(driver, pages):35pages.load("simpleTest.html")36driver.pinned_scripts = {}37hello = driver.pin_script("return arguments[0]")3839result = driver.execute_script(hello, "cheese")4041assert result == "cheese"424344def test_should_list_all_pinned_scripts(driver, pages):45pages.load("simpleTest.html")46driver.pinned_scripts = {}47expected = []48expected.append(driver.pin_script("return arguments[0];").id)49expected.append(driver.pin_script("return 'cheese';").id)50expected.append(driver.pin_script("return 42;").id)5152result = driver.get_pinned_scripts()53assert expected == result545556def test_should_allow_scripts_to_be_unpinned(driver, pages):57pages.load("simpleTest.html")58driver.pinned_scripts = {}59cheese = driver.pin_script("return 'cheese';")60driver.unpin(cheese)61results = driver.get_pinned_scripts()62assert cheese not in results636465def test_calling_unpinned_script_causes_error(driver, pages):66pages.load("simpleTest.html")67cheese = driver.pin_script("return 'brie';")68driver.unpin(cheese)69with pytest.raises(JavascriptException):70driver.execute_script(cheese)717273def test_unpinning_non_existing_script_raises(driver, pages):74pages.load("simpleTest.html")75with pytest.raises(KeyError, match=re.escape(r"No script with key: ScriptKey(id=1) existed in {}")):76driver.unpin(ScriptKey(1))777879