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 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
|
# 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.
"""Tests for advanced user interactions."""
import pytest
from selenium.common.exceptions import MoveTargetOutOfBoundsException
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.actions import interaction
from selenium.webdriver.common.actions.key_input import KeyInput
from selenium.webdriver.common.actions.pointer_input import PointerInput
from selenium.webdriver.common.actions.wheel_input import ScrollOrigin
from selenium.webdriver.common.actions.wheel_input import WheelInput
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
def _is_element_available(driver, id):
"""Copied from org.openqa.selenium.interactions.TestBasicMouseInterface."""
try:
driver.find_element(By.ID, id)
return True
except Exception:
return False
@pytest.mark.xfail_safari
@pytest.mark.xfail_chrome
def test_drag_and_drop_with_pointer(driver, pages):
"""Copied from org.openqa.selenium.interactions.TestBasicMouseInterface."""
element_available_timeout = 15
wait = WebDriverWait(driver, element_available_timeout)
pages.load("droppableItems.html")
wait.until(lambda dr: _is_element_available(driver, "draggable"))
if not _is_element_available(driver, "draggable"):
raise AssertionError("Could not find draggable element after 15 seconds.")
toDrag = driver.find_element(By.ID, "draggable")
dropInto = driver.find_element(By.ID, "droppable")
mouse = PointerInput(interaction.POINTER_MOUSE, "test mouse")
holdDrag = ActionChains(driver, devices=[mouse]).click_and_hold(toDrag)
move = ActionChains(driver, devices=[mouse]).move_to_element(dropInto)
drop = ActionChains(driver, devices=[mouse]).release(dropInto)
holdDrag.perform()
move.perform()
drop.perform()
dropInto = driver.find_element(By.ID, "droppable")
text = dropInto.find_element(By.TAG_NAME, "p").text
assert "Dropped!" == text
@pytest.mark.xfail_safari
def test_double_click_with_pointer(driver, pages):
"""Copied from org.openqa.selenium.interactions.TestBasicMouseInterface."""
pages.load("javascriptPage.html")
toDoubleClick = driver.find_element(By.ID, "doubleClickField")
mouse = PointerInput(interaction.POINTER_MOUSE, "test mouse")
dblClick = ActionChains(driver, devices=[mouse]).double_click(toDoubleClick)
dblClick.perform()
assert "DoubleClicked" == toDoubleClick.get_attribute("value")
def test_context_click_with_pointer(driver, pages):
"""Copied from org.openqa.selenium.interactions.TestBasicMouseInterface."""
pages.load("javascriptPage.html")
toContextClick = driver.find_element(By.ID, "doubleClickField")
mouse = PointerInput(interaction.POINTER_MOUSE, "test mouse")
contextClick = ActionChains(driver, devices=[mouse]).context_click(toContextClick)
contextClick.perform()
assert "ContextClicked" == toContextClick.get_attribute("value")
def test_move_and_click_with_pointer(driver, pages):
"""Copied from org.openqa.selenium.interactions.TestBasicMouseInterface."""
pages.load("javascriptPage.html")
toClick = driver.find_element(By.ID, "clickField")
mouse = PointerInput(interaction.POINTER_MOUSE, "test mouse")
click = ActionChains(driver, devices=[mouse]).move_to_element(toClick).click()
click.perform()
assert "Clicked" == toClick.get_attribute("value")
def test_cannot_move_to_anull_locator_with_pointer(driver, pages):
"""Copied from org.openqa.selenium.interactions.TestBasicMouseInterface."""
pages.load("javascriptPage.html")
mouse = PointerInput(interaction.POINTER_MOUSE, "test mouse")
with pytest.raises(AttributeError):
move = ActionChains(driver, devices=[mouse]).move_to_element(None)
move.perform()
@pytest.mark.xfail_safari
def test_clicking_on_form_elements_with_pointer(driver, pages):
"""Copied from org.openqa.selenium.interactions.CombinedInputActionsTest."""
pages.load("formSelectionPage.html")
options = driver.find_elements(By.TAG_NAME, "option")
mouse = PointerInput(interaction.POINTER_MOUSE, "test mouse")
selectThreeOptions = (
ActionChains(driver, devices=[mouse])
.click(options[1])
.key_down(Keys.SHIFT)
.click(options[3])
.key_up(Keys.SHIFT)
)
selectThreeOptions.perform()
showButton = driver.find_element(By.NAME, "showselected")
showButton.click()
resultElement = driver.find_element(By.ID, "result")
assert "roquefort parmigiano cheddar" == resultElement.text
@pytest.mark.xfail_firefox
@pytest.mark.xfail_safari
def test_selecting_multiple_items_with_devices(driver, pages):
"""Copied from org.openqa.selenium.interactions.CombinedInputActionsTest."""
pages.load("selectableItems.html")
reportingElement = driver.find_element(By.ID, "infodiv")
assert "no info" == reportingElement.text
mouse = PointerInput(interaction.POINTER_MOUSE, "test mouse")
key_board = KeyInput("test keyboard")
listItems = driver.find_elements(By.TAG_NAME, "li")
selectThreeItems = (
ActionChains(driver, devices=[mouse, key_board])
.key_down(Keys.CONTROL)
.click(listItems[1])
.click(listItems[3])
.click(listItems[5])
.key_up(Keys.CONTROL)
)
selectThreeItems.perform()
assert "#item2 #item4 #item6" == reportingElement.text
# Now click on another element, make sure that's the only one selected.
actionsBuilder = ActionChains(driver)
actionsBuilder.click(listItems[6]).perform()
assert "#item7" == reportingElement.text
@pytest.mark.xfail_safari
def test_sending_keys_to_active_element_with_modifier_with_keyboard(driver, pages):
pages.load("formPage.html")
e = driver.find_element(By.ID, "working")
e.click()
key_board = KeyInput("test keyboard")
ActionChains(driver, devices=[key_board]).key_down(Keys.SHIFT).send_keys("abc").key_up(Keys.SHIFT).perform()
assert "ABC" == e.get_attribute("value")
def test_sending_keys_to_element_with_keyboard(driver, pages):
pages.load("formPage.html")
e = driver.find_element(By.ID, "working")
key_board = KeyInput("test keyboard")
ActionChains(driver, devices=[key_board]).send_keys_to_element(e, "abc").perform()
assert "abc" == e.get_attribute("value")
def test_can_send_keys_between_clicks_with_keyboard(driver, pages):
"""
For W3C, ensures that the correct number of pauses are given to the other
input device.
"""
pages.load("javascriptPage.html")
keyup = driver.find_element(By.ID, "keyUp")
keydown = driver.find_element(By.ID, "keyDown")
key_board = KeyInput("test keyboard")
ActionChains(driver, devices=[key_board]).click(keyup).send_keys("foobar").click(keydown).perform()
assert "foobar" == keyup.get_attribute("value")
def test_can_reset_interactions_with_devices(driver):
mouse = PointerInput(interaction.POINTER_MOUSE, "test mouse")
key_board = KeyInput("test keyboard")
actions = ActionChains(driver, devices=[mouse, key_board])
actions.click()
actions.key_down("A")
assert all(len(device.actions) >= 0 for device in actions.w3c_actions.devices if device.type != interaction.WHEEL)
actions.reset_actions()
assert all(len(device.actions) == 0 for device in actions.w3c_actions.devices)
def test_can_pause_with_pointer(driver, pages):
from time import time
pages.load("javascriptPage.html")
pause_time = 2
toClick = driver.find_element(By.ID, "clickField")
toDoubleClick = driver.find_element(By.ID, "doubleClickField")
mouse = PointerInput(interaction.POINTER_MOUSE, "test mouse")
pause = ActionChains(driver, devices=[mouse]).click(toClick).pause(pause_time).click(toDoubleClick)
start = time()
pause.perform()
end = time()
assert pause_time < end - start
assert "Clicked" == toClick.get_attribute("value")
assert "Clicked" == toDoubleClick.get_attribute("value")
@pytest.mark.xfail_firefox
@pytest.mark.xfail_remote
def test_can_scroll_to_element_with_wheel(driver, pages):
pages.load("scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html")
iframe = driver.find_element(By.TAG_NAME, "iframe")
assert not _in_viewport(driver, iframe)
wheel = WheelInput("test wheel")
ActionChains(driver, devices=[wheel]).scroll_to_element(iframe).perform()
assert _in_viewport(driver, iframe)
@pytest.mark.xfail_firefox
@pytest.mark.xfail_remote
def test_can_scroll_from_element_by_amount_with_wheel(driver, pages):
pages.load("scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html")
iframe = driver.find_element(By.TAG_NAME, "iframe")
scroll_origin = ScrollOrigin.from_element(iframe)
wheel = WheelInput("test wheel")
ActionChains(driver, devices=[wheel]).scroll_from_origin(scroll_origin, 0, 200).pause(0.2).perform()
driver.switch_to.frame(iframe)
checkbox = driver.find_element(By.NAME, "scroll_checkbox")
assert _in_viewport(driver, checkbox)
@pytest.mark.xfail_firefox
@pytest.mark.xfail_remote
def test_can_scroll_from_element_with_offset_by_amount_with_wheel(driver, pages):
pages.load("scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html")
footer = driver.find_element(By.TAG_NAME, "footer")
scroll_origin = ScrollOrigin.from_element(footer, 0, -50)
wheel = WheelInput("test wheel")
ActionChains(driver, devices=[wheel]).scroll_from_origin(scroll_origin, 0, 200).pause(0.2).perform()
iframe = driver.find_element(By.TAG_NAME, "iframe")
driver.switch_to.frame(iframe)
checkbox = driver.find_element(By.NAME, "scroll_checkbox")
assert _in_viewport(driver, checkbox)
@pytest.mark.xfail_firefox
def test_errors_when_element_offset_not_in_viewport_with_wheel(driver, pages):
pages.load("scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html")
footer = driver.find_element(By.TAG_NAME, "footer")
scroll_origin = ScrollOrigin.from_element(footer, 0, 50)
wheel = WheelInput("test wheel")
with pytest.raises(MoveTargetOutOfBoundsException):
ActionChains(driver, devices=[wheel]).scroll_from_origin(scroll_origin, 0, 200).pause(0.2).perform()
@pytest.mark.xfail_firefox
@pytest.mark.xfail_remote
def test_can_scroll_from_viewport_by_amount_with_wheel(driver, pages):
pages.load("scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html")
footer = driver.find_element(By.TAG_NAME, "footer")
delta_y = footer.rect["y"]
wheel = WheelInput("test wheel")
ActionChains(driver, devices=[wheel]).scroll_by_amount(0, delta_y).pause(0.2).perform()
assert _in_viewport(driver, footer)
@pytest.mark.xfail_firefox
def test_can_scroll_from_viewport_with_offset_by_amount_with_wheel(driver, pages):
pages.load("scrolling_tests/frame_with_nested_scrolling_frame.html")
scroll_origin = ScrollOrigin.from_viewport(10, 10)
wheel = WheelInput("test wheel")
ActionChains(driver, devices=[wheel]).scroll_from_origin(scroll_origin, 0, 200).pause(0.2).perform()
iframe = driver.find_element(By.TAG_NAME, "iframe")
driver.switch_to.frame(iframe)
checkbox = driver.find_element(By.NAME, "scroll_checkbox")
assert _in_viewport(driver, checkbox)
@pytest.mark.xfail_firefox
def test_errors_when_origin_offset_not_in_viewport_with_wheel(driver, pages):
pages.load("scrolling_tests/frame_with_nested_scrolling_frame.html")
scroll_origin = ScrollOrigin.from_viewport(-10, -10)
wheel = WheelInput("test wheel")
with pytest.raises(MoveTargetOutOfBoundsException):
ActionChains(driver, devices=[wheel]).scroll_from_origin(scroll_origin, 0, 200).pause(0.2).perform()
def _get_events(driver):
"""Return list of key events recorded in the test_keys_page fixture."""
events = driver.execute_script("return allEvents.events;") or []
# `key` values in `allEvents` may be escaped (see `escapeSurrogateHalf` in
# test_keys_wdspec.html), so this converts them back into unicode literals.
for e in events:
# example: turn "U+d83d" (6 chars) into u"\ud83d" (1 char)
if "key" in e and e["key"].startswith("U+"):
key = e["key"]
hex_suffix = key[key.index("+") + 1 :]
e["key"] = chr(int(hex_suffix, 16))
# WebKit sets code as 'Unidentified' for unidentified key codes, but
# tests expect ''.
if "code" in e and e["code"] == "Unidentified":
e["code"] = ""
return events
def _in_viewport(driver, element):
script = (
"for(var e=arguments[0],f=e.offsetTop,t=e.offsetLeft,o=e.offsetWidth,n=e.offsetHeight;\n"
"e.offsetParent;)f+=(e=e.offsetParent).offsetTop,t+=e.offsetLeft;\n"
"return f<window.pageYOffset+window.innerHeight&&t<window.pageXOffset+window.innerWidth&&f+n>\n"
"window.pageYOffset&&t+o>window.pageXOffset"
)
return driver.execute_script(script, element)
|