Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/py/test/selenium/webdriver/common/script_pinning_tests.py
1865 views
1
# Licensed to the Software Freedom Conservancy (SFC) under one
2
# or more contributor license agreements. See the NOTICE file
3
# distributed with this work for additional information
4
# regarding copyright ownership. The SFC licenses this file
5
# to you under the Apache License, Version 2.0 (the
6
# "License"); you may not use this file except in compliance
7
# with the License. You may obtain a copy of the License at
8
#
9
# http://www.apache.org/licenses/LICENSE-2.0
10
#
11
# Unless required by applicable law or agreed to in writing,
12
# software distributed under the License is distributed on an
13
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
# KIND, either express or implied. See the License for the
15
# specific language governing permissions and limitations
16
# under the License.
17
import re
18
19
import pytest
20
21
from selenium.common.exceptions import JavascriptException
22
from selenium.webdriver.remote.script_key import ScriptKey
23
24
25
def test_should_allow_script_pinning(driver, pages):
26
pages.load("simpleTest.html")
27
driver.pinned_scripts = {}
28
script_key = driver.pin_script("return 'i like cheese';")
29
30
result = driver.execute_script(script_key)
31
32
assert result == "i like cheese"
33
34
35
def test_should_allow_pinned_scripts_to_take_arguments(driver, pages):
36
pages.load("simpleTest.html")
37
driver.pinned_scripts = {}
38
hello = driver.pin_script("return arguments[0]")
39
40
result = driver.execute_script(hello, "cheese")
41
42
assert result == "cheese"
43
44
45
def test_should_list_all_pinned_scripts(driver, pages):
46
pages.load("simpleTest.html")
47
driver.pinned_scripts = {}
48
expected = []
49
expected.append(driver.pin_script("return arguments[0];").id)
50
expected.append(driver.pin_script("return 'cheese';").id)
51
expected.append(driver.pin_script("return 42;").id)
52
53
result = driver.get_pinned_scripts()
54
assert expected == result
55
56
57
def test_should_allow_scripts_to_be_unpinned(driver, pages):
58
pages.load("simpleTest.html")
59
driver.pinned_scripts = {}
60
cheese = driver.pin_script("return 'cheese';")
61
driver.unpin(cheese)
62
results = driver.get_pinned_scripts()
63
assert cheese not in results
64
65
66
def test_calling_unpinned_script_causes_error(driver, pages):
67
pages.load("simpleTest.html")
68
cheese = driver.pin_script("return 'brie';")
69
driver.unpin(cheese)
70
with pytest.raises(JavascriptException):
71
driver.execute_script(cheese)
72
73
74
def test_unpinning_non_existing_script_raises(driver, pages):
75
pages.load("simpleTest.html")
76
with pytest.raises(KeyError, match=re.escape(r"No script with key: ScriptKey(id=1) existed in {}")):
77
driver.unpin(ScriptKey(1))
78
79