1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
|
# Licensed to the Software Freedom Conservancy (SFC) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The SFC licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
import pytest
from selenium.common.exceptions import WebDriverException
from selenium.webdriver.common.by import By
from selenium.webdriver.remote.webelement import WebElement
try:
str = unicode
except NameError:
pass
def test_should_be_able_to_execute_simple_javascript_and_return_astring(driver, pages):
pages.load("xhtmlTest.html")
result = driver.execute_script("return document.title")
assert isinstance(result, str), "The type of the result is %s" % type(result)
assert "XHTML Test Page" == result
def test_should_be_able_to_execute_simple_javascript_and_return_an_integer(driver, pages):
pages.load("nestedElements.html")
result = driver.execute_script("return document.getElementsByName('checky').length")
assert isinstance(result, int)
assert int(result) > 1
def test_should_be_able_to_execute_simple_javascript_and_return_aweb_element(driver, pages):
pages.load("xhtmlTest.html")
result = driver.execute_script("return document.getElementById('id1')")
assert result is not None
assert isinstance(result, WebElement)
assert "a" == result.tag_name.lower()
def test_should_be_able_to_execute_simple_javascript_and_return_alist_of_web_elements(driver, pages):
pages.load("xhtmlTest.html")
result = driver.execute_script("return document.querySelectorAll('div.navigation a')")
assert result is not None
assert isinstance(result, list)
assert all(isinstance(item, WebElement) for item in result)
assert all("a" == item.tag_name.lower() for item in result)
def test_should_be_able_to_execute_simple_javascript_and_return_web_elements_inside_alist(driver, pages):
pages.load("xhtmlTest.html")
result = driver.execute_script("return [document.body]")
assert result is not None
assert isinstance(result, list)
assert isinstance(result[0], WebElement)
def test_should_be_able_to_execute_simple_javascript_and_return_web_elements_inside_anested_list(driver, pages):
pages.load("xhtmlTest.html")
result = driver.execute_script("return [document.body, [document.getElementById('id1')]]")
assert result is not None
assert isinstance(result, list)
assert isinstance(result[0], WebElement)
assert isinstance(result[1][0], WebElement)
def test_should_be_able_to_execute_simple_javascript_and_return_web_elements_inside_adict(driver, pages):
pages.load("xhtmlTest.html")
result = driver.execute_script("return {el1: document.body}")
assert result is not None
assert isinstance(result, dict)
assert isinstance(result.get("el1"), WebElement)
def test_should_be_able_to_execute_simple_javascript_and_return_web_elements_inside_anested_dict(driver, pages):
pages.load("xhtmlTest.html")
result = driver.execute_script("return {el1: document.body, " "nested: {el2: document.getElementById('id1')}}")
assert result is not None
assert isinstance(result, dict)
assert isinstance(result.get("el1"), WebElement)
assert isinstance(result.get("nested").get("el2"), WebElement)
def test_should_be_able_to_execute_simple_javascript_and_return_web_elements_inside_alist_inside_adict(driver, pages):
pages.load("xhtmlTest.html")
result = driver.execute_script("return {el1: [document.body]}")
assert result is not None
assert isinstance(result, dict)
assert isinstance(result.get("el1"), list)
assert isinstance(result.get("el1")[0], WebElement)
def test_should_be_able_to_execute_simple_javascript_and_return_aboolean(driver, pages):
pages.load("xhtmlTest.html")
result = driver.execute_script("return true")
assert result is not None
assert isinstance(result, bool)
assert bool(result)
def test_should_be_able_to_execute_simple_javascript_and_astrings_array(driver, pages):
pages.load("javascriptPage.html")
expectedResult = []
expectedResult.append("zero")
expectedResult.append("one")
expectedResult.append("two")
result = driver.execute_script("return ['zero', 'one', 'two']")
assert expectedResult == result
def test_should_be_able_to_execute_simple_javascript_and_return_an_array(driver, pages):
pages.load("javascriptPage.html")
expectedResult = []
expectedResult.append("zero")
subList = []
subList.append(True)
subList.append(False)
expectedResult.append(subList)
result = driver.execute_script("return ['zero', [true, false]]")
assert result is not None
assert isinstance(result, list)
assert expectedResult == result
def test_passing_and_returning_an_int_should_return_awhole_number(driver, pages):
pages.load("javascriptPage.html")
expectedResult = 1
result = driver.execute_script("return arguments[0]", expectedResult)
assert isinstance(result, int)
assert expectedResult == result
def test_passing_and_returning_adouble_should_return_adecimal(driver, pages):
pages.load("javascriptPage.html")
expectedResult = 1.2
result = driver.execute_script("return arguments[0]", expectedResult)
assert isinstance(result, float)
assert expectedResult == result
def test_should_throw_an_exception_when_the_javascript_is_bad(driver, pages):
pages.load("xhtmlTest.html")
with pytest.raises(WebDriverException):
driver.execute_script("return squiggle()")
def test_should_be_able_to_call_functions_defined_on_the_page(driver, pages):
pages.load("javascriptPage.html")
driver.execute_script("displayMessage('I like cheese')")
text = driver.find_element(By.ID, "result").text
assert "I like cheese" == text.strip()
def test_should_be_able_to_pass_astring_an_as_argument(driver, pages):
pages.load("javascriptPage.html")
value = driver.execute_script("return arguments[0] == 'fish' ? 'fish' : 'not fish'", "fish")
assert "fish" == value
def test_should_be_able_to_pass_aboolean_an_as_argument(driver, pages):
pages.load("javascriptPage.html")
value = bool(driver.execute_script("return arguments[0] == true", True))
assert value
def test_should_be_able_to_pass_anumber_an_as_argument(driver, pages):
pages.load("javascriptPage.html")
value = bool(driver.execute_script("return arguments[0] == 1 ? true : false", 1))
assert value
def test_should_be_able_to_pass_aweb_element_as_argument(driver, pages):
pages.load("javascriptPage.html")
button = driver.find_element(By.ID, "plainButton")
value = driver.execute_script(
"arguments[0]['flibble'] = arguments[0].getAttribute('id'); return arguments[0]['flibble']", button
)
assert "plainButton" == value
def test_should_be_able_to_pass_an_array_as_argument(driver, pages):
pages.load("javascriptPage.html")
array = ["zero", 1, True, 3.14159]
length = int(driver.execute_script("return arguments[0].length", array))
assert len(array) == length
def test_should_be_able_to_pass_acollection_as_argument(driver, pages):
pages.load("javascriptPage.html")
collection = []
collection.append("Cheddar")
collection.append("Brie")
collection.append(7)
length = int(driver.execute_script("return arguments[0].length", collection))
assert len(collection) == length
collection = []
collection.append("Gouda")
collection.append("Stilton")
collection.append("Stilton")
collection.append(True)
length = int(driver.execute_script("return arguments[0].length", collection))
assert len(collection) == length
def test_should_throw_an_exception_if_an_argument_is_not_valid(driver, pages):
pages.load("javascriptPage.html")
with pytest.raises(Exception):
driver.execute_script("return arguments[0]", driver)
def test_should_be_able_to_pass_in_more_than_one_argument(driver, pages):
pages.load("javascriptPage.html")
result = driver.execute_script("return arguments[0] + arguments[1]", "one", "two")
assert "onetwo" == result
def test_javascript_string_handling_should_work_as_expected(driver, pages):
pages.load("javascriptPage.html")
value = driver.execute_script("return ''")
assert "" == value
value = driver.execute_script("return undefined")
assert value is None
value = driver.execute_script("return ' '")
assert " " == value
def test_should_be_able_to_create_apersistent_value(driver, pages):
pages.load("formPage.html")
driver.execute_script("document.alerts = []")
driver.execute_script("document.alerts.push('hello world')")
text = driver.execute_script("return document.alerts.shift()")
assert "hello world" == text
def test_can_pass_adictionary_as_aparameter(driver, pages):
pages.load("simpleTest.html")
nums = [1, 2]
args = {"bar": "test", "foo": nums}
res = driver.execute_script("return arguments[0]['foo'][1]", args)
assert 2 == res
def test_can_pass_anone(driver, pages):
pages.load("simpleTest.html")
res = driver.execute_script("return arguments[0] === null", None)
assert res
def test_can_return_a_const(driver, pages):
pages.load("simpleTest.html")
res = driver.execute_script("const cheese='cheese'; return cheese")
assert res == "cheese"
def test_can_return_a_const_in_a_page(driver, pages):
pages.load("const_js.html")
res = driver.execute_script("return makeMeA('sandwich');")
assert res == "cheese sandwich"
@pytest.mark.xfail_remote
@pytest.mark.xfail_firefox
def test_can_return_global_const(driver, pages):
pages.load("const_js.html")
# cheese is a variable with "cheese" in it
res = driver.execute_script("return cheese")
assert res == "cheese"
|