Make the most complete UI mouse keyboard events!

Keywords: Programming Selenium Windows Python

When using Selenium Web Driver for automated testing, it often simulates the behavior of the mouse and keyboard. For example, the use of mouse click, double-click, right-click, drag and other actions; or keyboard input, shortcut key use, combination key use and other simulation keyboard operations. In Web Deriver, there is a special class responsible for implementing these test scenarios, namely the ActionChains class. Keys data storage class is used in the process of using this class to do keyboard operations. Keys contains all the special keys on the keyboard.

I. Mouse Click Operation

click(element=None) left-click context_click(element=None) right-click
 double_click(element=None) double-click
 move_to_element(element) Move the mouse to the middle of the element (hover)
drag_and_drop(source,target)source by pressing the left key and dragging onto the target element
 click_and_hold(element=None) Press the left mouse button on the element
 release() release the mouse
 perform() executes actions stored in ActionChains

element has a default value of None to indicate that no parameter is passed in. This action is executed in place.

Examples of the specific use of mouse events are as follows:

Example 1: Left mouse click

action=ActionChains(driver)action.click() # Click at the current position of the mouse
action.perform() # Actions to execute action storage
action.click(driver.find_element_by_link_text('Journalism')).perform()#Mouse in the'News'element position Click
#Note: The action.click() action is not executed, it is only stored in the action instance and needs to be executed by the action.perform() method.
#Storage action; mouse keyboard event action can be stored more than one, and then executed at one time. Execute Cytl+C as follows:
ActionChains(driver).key_down(Keys.CONTROL).send_keys('c').key_up(Keys.CONTROL).perform()

Example 2: Right mouse click

action=ActionChains(driver)action.context_click().perform()
 # Right-click at the current position of the mouse
action.context_click(driver.find_element_by_link_text('Journalism')).perform()
# Right click on the'news'element

Example 3: Mouse double-click operation

action=ActionChains(driver)action.double_click().perform()
 # Double-click at the current position of the mouse
action.double_click(driver.find_element_by_link_text('Journalism')).perform()
# Double-click the mouse at the position of the'news'element

Example 4: Mouse movement

Mouse to the center of the'news'element
action.move_to_element(element).perform()
# Mouse moves xoffset and yoffset to x axis in situ; xoffset and yoffset can be positive and negative.
action.move_by_offset(-200,100).perform()
Mouse to element center offset xoffset, yoffset
action.move_to_element_with_offset(element,-500,600).perform()
action.move_by_offset(xoffset,yoffset) 
Note here that if xoffset is negative, it means that the abscissa moves to the left, and yoffset is negative, it means that the longitudinal coordinates move upward. And if the # values are larger than the size of the current screen, the mouse can only move to the most boundary of the screen.

The common scenario of mouse movement operation in test environment is to acquire flyover/tips of an element. In practical application, many flyover only occurs after the mouse moves to this element, so the expected effect can be achieved by performing the move_to_element(to_element) operation at this time. According to my own experience, this method does not work for flyover/tips of some product-specific icons. Although flyover can appear when you move your mouse over these icons manually, flyover does not appear when you use WebDriver to simulate this operation, although the method is successfully implemented. Therefore, in practical application, we also need to deal with specific product pages.

Example 5: Mouse hover

action.click_and_hold().perform()
 # The mouse is not released when it is pressed at the current position
action.click_and_hold(driver.find_element_by_link_text('Set up')).perform()
# Mouse hover over Settings
action.click_and_hold(element) This method actually performs two actions, the first is to move the mouse to the element. 
element,Then again click_and_hold, So this method can also be written as follows:
action.move_to_element(element).click_and_hold()

Example 6: Mouse dragging

target = driver.find_element_by_id("sk") # Get the target element
# Drag the element source to the target location
ActionChains(driver).drag_and_drop(source, target).perform()
# Drag the mouse and move the source element to the x and y axis (xoffset, yoffset), where xoffset is the abscissa and yoffset is the ordinate.
ActionChains(driver).drag_and_drop_by_offset(source, -100,100).perform()

In this drag and drop process, the mouse combination action has been used. First, the mouse clicks and holds the lick_and_hold (source) element, then executes the mouse movement action (move_to), moves to the target element position or (xoffset, yoffset) position, and then executes the mouse release action (release). So the above method can also be divided into the following execution actions to complete:

ActionChains(driver).click_and_hold(source).move_to_element(target).release().perform()

Example 7: Mouse Release Operations

action = ActionChains(driver)action.release().perform() # Release the pressed mouse

2. Keyboard operation

For keyboard simulation, the ActionChains class provides methods such as pressing key_down(keys), releasing key_up(keys), pressing and releasing send_keys(keys_to_send). There are two kinds of keyboard operation: ordinary keyboard and modified keyboard. Ordinary keyboards are commonly used letters and numbers, modified keyboards are Ctrl, Shift, Alt, etc., modified keyboards are commonly used in combination with other keys. Introduce from when using keyboard operation
The selenium.webdriver.common.keys import Keys package contains all the special Keys.

1. General keyboard operation

Keyboard operation uses send_keys(keys_to_send) method, which supports multiple keys continuous operation. If you need to perform key operation on an element, use send_keys_to_element (element, keys_to_send) method. Specifically, the following examples are used:

from selenium.webdriver.common.keys import Keys
action = ActionChains(driver)action.send_keys(Keys.SPACE).perform() 
# Press and release the space bar
action.send_keys(Keys.TAB).perform()
 # Press and release the Tab key
action.send_keys(Keys.BACKSPACE).perform() 
# Press and release the Backspace key
action.send_keys(Keys.BACKSPACE,Keys.SPACE).perform() 
# Continuous key action.send_keys(Keys.TAB).send_keys(Keys.TAB).perform() 
# You can also combine'''to issue a keyboard key operation for an element, or input operation'''
element = driver.find_element_by_id('query')
# Keyboard operations on an element
action.send_keys_to_element(element, 'selenium').perform()
# The upper action is disassembled into the following action
action.click(element).send_keys('selenium').perform()

Be careful:
In addition to the send_keys (keys_to_send) method in the ActionChains class, the WebElement class also has a send_keys_to_element (keys_to_send) method, which is basically the same for general input operations. The differences are as follows:
First point: The send_keys (* keys_to_send) in Actions do not release the modifier keys after operation. That is to say, when actions.send_keys(Keys.ALT), actions.send_keys(Keys.CONTROL), actions. send_keys (Keys. SHIFT) are called, it is equivalent to call actions.key_down(keys_to_send), and if you want to simulate pressing and releasing the modifier keys in real applications, action.reset_actions() should be reset first, and then action.send_keys(keys.NULL).perform() should be called to cancel the pressed modifier key.
Second, in WebDriver, we can use send_keys() of the WebElement class to upload attachments, such as files on element.send_keys("D: test\ uploadfile\ test. jpg"), but we can't use ActionChains to upload attachments, because the input box of type='file does not support keyboard input.

2. Use of modifier keys

Modified keys are one or a group of special keys on the keyboard. When they are used in conjunction with ordinary keys, they are used to temporarily change the general behavior of the general keyboard.

Modified keys are usually combined with ordinary keys, such as Ctrl+A, Alt+F4, etc.

The modifier keys in our computers are generally repaired as follows: Ctrl, Alt(Option), Shift, AltGr, Windows logo, Command, FN(Function). The first three are commonly used.

In Python selenium, the use of modifier keys is usually realized by pressing key_down(keys), releasing key_up(keys), pressing and releasing send_keys(keys_to_send).

action = ActionChains(driver)action.key_down(Keys.CONTROL).perform()
 # Press the ctrl key
action.key_up(Keys.CONTROL).perform() 
# Release ctrl bond
action.key_down(Keys.SHIFT).perform()
 # Press the shift key
action.key_up(Keys.SHIFT).perform()
 # Release shift key
action.key_down(Keys.ALT).perform()
 # Press the alt key.
action.key_up(Keys.ALT).perform()
 # Release the alt key.

Example: Copy text through ctrl+c

ActionChains(driver).key_down(Keys.CONTROL).send_keys('c').key_up(Keys.CONTROL).perform()

3. WebElement.send_keys() keyboard operation

send_keys under WebElement element objects also support composite keyboard operations.

The code example is as follows:

element = 
driver.find_element_by_id('query')element.send_keys('selenium')element.send_keys(Keys.BACK_SPACE)
 # Delete a character by BACKSPACE
element.send_keys(Keys.SPACE) 
# Space key
element.send_keys(Keys.CONTROL, 'a') 
# Total Selection (Ctrl+A)
element.send_keys(Keys.CONTROL, 'c') 
# Replication (Ctrl+C)
element.send_keys(Keys.CONTROL, 'v')
 # Paste (Ctrl+v)
element.send_keys(Keys.TAB)
# Tab
element.send_keys(Keys.ESCAPE) 
# Back key (Esc)
element.send_keys(Keys.ENTER) 
# Enter

For students interested in testing technology, welcome to add QQ group 706315665, study together and discuss with each other.

There are already small partners in the group to organize the knowledge system (source code, notes, PPT, learning videos). Welcome to the group for free.

QQ group 706315665, free of charge

Posted by winstond on Sun, 20 Jan 2019 08:54:12 -0800