Path: blob/trunk/py/test/selenium/webdriver/common/interactions_with_device_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.1617"""Tests for advanced user interactions."""1819import pytest2021from selenium.common.exceptions import MoveTargetOutOfBoundsException22from selenium.webdriver.common.action_chains import ActionChains23from selenium.webdriver.common.actions import interaction24from selenium.webdriver.common.actions.key_input import KeyInput25from selenium.webdriver.common.actions.pointer_input import PointerInput26from selenium.webdriver.common.actions.wheel_input import ScrollOrigin, WheelInput27from selenium.webdriver.common.by import By28from selenium.webdriver.common.keys import Keys29from selenium.webdriver.support.ui import WebDriverWait303132def _is_element_available(driver, id):33"""Copied from org.openqa.selenium.interactions.TestBasicMouseInterface."""34try:35driver.find_element(By.ID, id)36return True37except Exception:38return False394041@pytest.mark.xfail_safari42@pytest.mark.xfail_chrome43def test_drag_and_drop_with_pointer(driver, pages):44"""Copied from org.openqa.selenium.interactions.TestBasicMouseInterface."""45element_available_timeout = 1546wait = WebDriverWait(driver, element_available_timeout)47pages.load("droppableItems.html")48wait.until(lambda dr: _is_element_available(driver, "draggable"))4950if not _is_element_available(driver, "draggable"):51raise AssertionError("Could not find draggable element after 15 seconds.")5253toDrag = driver.find_element(By.ID, "draggable")54dropInto = driver.find_element(By.ID, "droppable")5556mouse = PointerInput(interaction.POINTER_MOUSE, "test mouse")5758holdDrag = ActionChains(driver, devices=[mouse]).click_and_hold(toDrag)59move = ActionChains(driver, devices=[mouse]).move_to_element(dropInto)60drop = ActionChains(driver, devices=[mouse]).release(dropInto)6162holdDrag.perform()63move.perform()64drop.perform()6566dropInto = driver.find_element(By.ID, "droppable")67text = dropInto.find_element(By.TAG_NAME, "p").text68assert "Dropped!" == text697071@pytest.mark.xfail_safari72def test_double_click_with_pointer(driver, pages):73"""Copied from org.openqa.selenium.interactions.TestBasicMouseInterface."""74pages.load("javascriptPage.html")75toDoubleClick = driver.find_element(By.ID, "doubleClickField")7677mouse = PointerInput(interaction.POINTER_MOUSE, "test mouse")7879dblClick = ActionChains(driver, devices=[mouse]).double_click(toDoubleClick)80dblClick.perform()81assert "DoubleClicked" == toDoubleClick.get_attribute("value")828384def test_context_click_with_pointer(driver, pages):85"""Copied from org.openqa.selenium.interactions.TestBasicMouseInterface."""86pages.load("javascriptPage.html")87toContextClick = driver.find_element(By.ID, "doubleClickField")8889mouse = PointerInput(interaction.POINTER_MOUSE, "test mouse")9091contextClick = ActionChains(driver, devices=[mouse]).context_click(toContextClick)92contextClick.perform()93assert "ContextClicked" == toContextClick.get_attribute("value")949596def test_move_and_click_with_pointer(driver, pages):97"""Copied from org.openqa.selenium.interactions.TestBasicMouseInterface."""98pages.load("javascriptPage.html")99toClick = driver.find_element(By.ID, "clickField")100101mouse = PointerInput(interaction.POINTER_MOUSE, "test mouse")102103click = ActionChains(driver, devices=[mouse]).move_to_element(toClick).click()104click.perform()105assert "Clicked" == toClick.get_attribute("value")106107108def test_cannot_move_to_anull_locator_with_pointer(driver, pages):109"""Copied from org.openqa.selenium.interactions.TestBasicMouseInterface."""110pages.load("javascriptPage.html")111mouse = PointerInput(interaction.POINTER_MOUSE, "test mouse")112113with pytest.raises(AttributeError):114move = ActionChains(driver, devices=[mouse]).move_to_element(None)115move.perform()116117118@pytest.mark.xfail_safari119def test_clicking_on_form_elements_with_pointer(driver, pages):120"""Copied from org.openqa.selenium.interactions.CombinedInputActionsTest."""121pages.load("formSelectionPage.html")122options = driver.find_elements(By.TAG_NAME, "option")123mouse = PointerInput(interaction.POINTER_MOUSE, "test mouse")124125selectThreeOptions = (126ActionChains(driver, devices=[mouse])127.click(options[1])128.key_down(Keys.SHIFT)129.click(options[3])130.key_up(Keys.SHIFT)131)132selectThreeOptions.perform()133134showButton = driver.find_element(By.NAME, "showselected")135showButton.click()136137resultElement = driver.find_element(By.ID, "result")138assert "roquefort parmigiano cheddar" == resultElement.text139140141@pytest.mark.xfail_firefox142@pytest.mark.xfail_safari143def test_selecting_multiple_items_with_devices(driver, pages):144"""Copied from org.openqa.selenium.interactions.CombinedInputActionsTest."""145pages.load("selectableItems.html")146reportingElement = driver.find_element(By.ID, "infodiv")147assert "no info" == reportingElement.text148149mouse = PointerInput(interaction.POINTER_MOUSE, "test mouse")150key_board = KeyInput("test keyboard")151152listItems = driver.find_elements(By.TAG_NAME, "li")153selectThreeItems = (154ActionChains(driver, devices=[mouse, key_board])155.key_down(Keys.CONTROL)156.click(listItems[1])157.click(listItems[3])158.click(listItems[5])159.key_up(Keys.CONTROL)160)161selectThreeItems.perform()162163assert "#item2 #item4 #item6" == reportingElement.text164165# Now click on another element, make sure that's the only one selected.166actionsBuilder = ActionChains(driver)167actionsBuilder.click(listItems[6]).perform()168assert "#item7" == reportingElement.text169170171@pytest.mark.xfail_safari172def test_sending_keys_to_active_element_with_modifier_with_keyboard(driver, pages):173pages.load("formPage.html")174e = driver.find_element(By.ID, "working")175e.click()176177key_board = KeyInput("test keyboard")178179ActionChains(driver, devices=[key_board]).key_down(Keys.SHIFT).send_keys("abc").key_up(Keys.SHIFT).perform()180181assert "ABC" == e.get_attribute("value")182183184def test_sending_keys_to_element_with_keyboard(driver, pages):185pages.load("formPage.html")186e = driver.find_element(By.ID, "working")187188key_board = KeyInput("test keyboard")189190ActionChains(driver, devices=[key_board]).send_keys_to_element(e, "abc").perform()191192assert "abc" == e.get_attribute("value")193194195def test_can_send_keys_between_clicks_with_keyboard(driver, pages):196"""197For W3C, ensures that the correct number of pauses are given to the other198input device.199"""200pages.load("javascriptPage.html")201keyup = driver.find_element(By.ID, "keyUp")202keydown = driver.find_element(By.ID, "keyDown")203204key_board = KeyInput("test keyboard")205206ActionChains(driver, devices=[key_board]).click(keyup).send_keys("foobar").click(keydown).perform()207208assert "foobar" == keyup.get_attribute("value")209210211def test_can_reset_interactions_with_devices(driver):212mouse = PointerInput(interaction.POINTER_MOUSE, "test mouse")213key_board = KeyInput("test keyboard")214actions = ActionChains(driver, devices=[mouse, key_board])215actions.click()216actions.key_down("A")217218assert all(len(device.actions) >= 0 for device in actions.w3c_actions.devices if device.type != interaction.WHEEL)219220actions.reset_actions()221222assert all(len(device.actions) == 0 for device in actions.w3c_actions.devices)223224225def test_can_pause_with_pointer(driver, pages):226from time import time227228pages.load("javascriptPage.html")229230pause_time = 2231toClick = driver.find_element(By.ID, "clickField")232toDoubleClick = driver.find_element(By.ID, "doubleClickField")233234mouse = PointerInput(interaction.POINTER_MOUSE, "test mouse")235236pause = ActionChains(driver, devices=[mouse]).click(toClick).pause(pause_time).click(toDoubleClick)237238start = time()239pause.perform()240end = time()241242assert pause_time < end - start243assert "Clicked" == toClick.get_attribute("value")244assert "Clicked" == toDoubleClick.get_attribute("value")245246247@pytest.mark.xfail_firefox248@pytest.mark.xfail_remote249@pytest.mark.xfail_safari250def test_can_scroll_to_element_with_wheel(driver, pages):251pages.load("scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html")252iframe = driver.find_element(By.TAG_NAME, "iframe")253254assert not _in_viewport(driver, iframe)255256wheel = WheelInput("test wheel")257258ActionChains(driver, devices=[wheel]).scroll_to_element(iframe).perform()259260assert _in_viewport(driver, iframe)261262263@pytest.mark.xfail_firefox264@pytest.mark.xfail_remote265@pytest.mark.xfail_safari266def test_can_scroll_from_element_by_amount_with_wheel(driver, pages):267pages.load("scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html")268iframe = driver.find_element(By.TAG_NAME, "iframe")269scroll_origin = ScrollOrigin.from_element(iframe)270271wheel = WheelInput("test wheel")272273ActionChains(driver, devices=[wheel]).scroll_from_origin(scroll_origin, 0, 200).pause(0.2).perform()274275driver.switch_to.frame(iframe)276checkbox = driver.find_element(By.NAME, "scroll_checkbox")277assert _in_viewport(driver, checkbox)278279280@pytest.mark.xfail_firefox281@pytest.mark.xfail_remote282@pytest.mark.xfail_safari283def test_can_scroll_from_element_with_offset_by_amount_with_wheel(driver, pages):284pages.load("scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html")285footer = driver.find_element(By.TAG_NAME, "footer")286scroll_origin = ScrollOrigin.from_element(footer, 0, -50)287288wheel = WheelInput("test wheel")289290ActionChains(driver, devices=[wheel]).scroll_from_origin(scroll_origin, 0, 200).pause(0.2).perform()291292iframe = driver.find_element(By.TAG_NAME, "iframe")293driver.switch_to.frame(iframe)294checkbox = driver.find_element(By.NAME, "scroll_checkbox")295assert _in_viewport(driver, checkbox)296297298@pytest.mark.xfail_firefox299def test_errors_when_element_offset_not_in_viewport_with_wheel(driver, pages):300pages.load("scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html")301footer = driver.find_element(By.TAG_NAME, "footer")302scroll_origin = ScrollOrigin.from_element(footer, 0, 50)303304wheel = WheelInput("test wheel")305306with pytest.raises(MoveTargetOutOfBoundsException):307ActionChains(driver, devices=[wheel]).scroll_from_origin(scroll_origin, 0, 200).pause(0.2).perform()308309310@pytest.mark.xfail_firefox311@pytest.mark.xfail_remote312def test_can_scroll_from_viewport_by_amount_with_wheel(driver, pages):313pages.load("scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html")314footer = driver.find_element(By.TAG_NAME, "footer")315delta_y = footer.rect["y"]316317wheel = WheelInput("test wheel")318319ActionChains(driver, devices=[wheel]).scroll_by_amount(0, delta_y).pause(0.2).perform()320321assert _in_viewport(driver, footer)322323324@pytest.mark.xfail_firefox325@pytest.mark.xfail_safari326def test_can_scroll_from_viewport_with_offset_by_amount_with_wheel(driver, pages):327pages.load("scrolling_tests/frame_with_nested_scrolling_frame.html")328scroll_origin = ScrollOrigin.from_viewport(10, 10)329330wheel = WheelInput("test wheel")331332ActionChains(driver, devices=[wheel]).scroll_from_origin(scroll_origin, 0, 200).pause(0.2).perform()333334iframe = driver.find_element(By.TAG_NAME, "iframe")335driver.switch_to.frame(iframe)336checkbox = driver.find_element(By.NAME, "scroll_checkbox")337assert _in_viewport(driver, checkbox)338339340@pytest.mark.xfail_firefox341def test_errors_when_origin_offset_not_in_viewport_with_wheel(driver, pages):342pages.load("scrolling_tests/frame_with_nested_scrolling_frame.html")343scroll_origin = ScrollOrigin.from_viewport(-10, -10)344345wheel = WheelInput("test wheel")346347with pytest.raises(MoveTargetOutOfBoundsException):348ActionChains(driver, devices=[wheel]).scroll_from_origin(scroll_origin, 0, 200).pause(0.2).perform()349350351def _get_events(driver):352"""Return list of key events recorded in the test_keys_page fixture."""353events = driver.execute_script("return allEvents.events;") or []354# `key` values in `allEvents` may be escaped (see `escapeSurrogateHalf` in355# test_keys_wdspec.html), so this converts them back into unicode literals.356for e in events:357# example: turn "U+d83d" (6 chars) into u"\ud83d" (1 char)358if "key" in e and e["key"].startswith("U+"):359key = e["key"]360hex_suffix = key[key.index("+") + 1 :]361e["key"] = chr(int(hex_suffix, 16))362363# WebKit sets code as 'Unidentified' for unidentified key codes, but364# tests expect ''.365if "code" in e and e["code"] == "Unidentified":366e["code"] = ""367return events368369370def _in_viewport(driver, element):371script = (372"for(var e=arguments[0],f=e.offsetTop,t=e.offsetLeft,o=e.offsetWidth,n=e.offsetHeight;\n"373"e.offsetParent;)f+=(e=e.offsetParent).offsetTop,t+=e.offsetLeft;\n"374"return f<window.pageYOffset+window.innerHeight&&t<window.pageXOffset+window.innerWidth&&f+n>\n"375"window.pageYOffset&&t+o>window.pageXOffset"376)377return driver.execute_script(script, element)378379380