This article summarizes the use of mouse and keyboard events in Selenium Webdriver, as well as the use of key combinations, and describes the extensions of keyboard events (keys not enumerated in the keys enumeration) that are not implemented in Webdriver. An example of an extended ALT+PRTSC key combination to intercept the currently active window and save the Clipboard image to a file. When you use Selenium Webdriver for automated testing, you often simulate some of the mouse and keyboard behavior. such as the use of mouse click, double-click, right-click, drag-and-drop actions, or keyboard input, shortcut keys use, key combinations, such as analog keyboard operation. In Webderiver, there is a specialized class that is responsible for implementing these test scenarios, which is the Actions class that uses the Keys enumeration along with classes such as Mouse, Keyboard, Compositeaction, and so on in the process of using the class. Secondly, in the actual testing process, you may encounter some keys do not use the Actions, keys and other classes to achieve the situation. For example, by using the ALT+PRTSC key combination to capture the image of the currently active window of the screen, in the keys enumeration, because there is no PrtSc key, there is no way to simulate the action by the Action's KeyDown (keys). Again in automated testing, you may encounter some attachments, file upload scenarios, or multiple file uploads, which can be implemented directly after Selenium2.0 using the SendKeys () method of the Webelement class. The specific use of these cases is described below. Mouse click Actions Mouse click events are available in the following categories: Listing 1. Left mouse button click actions = new actions (driver), Action.click ();//mouse button in the current position to do a click action action.click ( Driver.findelement (By.name (Element)))//left mouse button click on the specified element listing 2. Right-click actions = new Actions (driver); Action.contextclick ();//right mouse button in the current position to do a click Action.contextclick (driver.Findelement (By.name (Element)))//Right-click on the specified element listing 3. Mouse double-click actions actions = new Actions (driver); Action.doubleclick ();//mouse in the current position to do a double-click operation Action.doubleclick (Driver.findelement (By.name))//mouse double-click the specified element listing 4. Drag action actions = new Action (driver); //drag and drop the source element to the target element. Action.draganddrop (Source,target); //Mouse drag action, drag and drop the source element to (Xoffset, yoffset) position, where Xoffset is the horizontal axis, Yoffset is vertical Coordinate. Action.draganddrop (Source,xoffset,yoffset); in this drag process, has used the mouse combo action, the first is the mouse click and hold (click-and-hold ) source element, then perform a mouse move action (mouse move), move to the target element position or (Xoffset, yoffset) position, and then perform the Mouse release action (mouse release). So the above method can also be split into the following several execution actions to complete: action.clickandhold (source). Movetoelement (target). Perform (); Action.release (); listing 5. Hover actions actions = new Actions (driver); action.clickandhold ();//hover over the current position, Both clicks and does not release Action.clickandhold (OnelemENT);//hover over the position of the onelement element Action.clickandhold (onelement) This method actually executes two actions, first the mouse moves to the element onelement, and then Clickandhold, so this method can also be written as Action.movetoelement (onelement). Clickandhold (). Listing 6. Mouse Move action actions = new Actions (driver); action.movetoelement (toelement);//Move mouse over The toelement element points//moves the mouse to the element toelement (Xoffset, yoffset) position, //here (Xoffset, Yoffset) is starting with the upper left corner of the element toelement (0,0) (x, y) axis. action.movetoelement (Toelement,xoffset,yoffset) //At the current position of the mouse or (0,0) as the center to move to (Xoffset, Yoffset) Axes & nbsp Action.movebyoffset (Xoffset,yoffset); Action.movebyoffset (Xoffset,yoffset) It is important to note that if the xOffset is negative, the horizontal axis moves to the left, A negative yoffset indicates that the ordinate is moving upward. And if these two values are larger than the current screen size, the mouse can only move to the edge of the screen and throw an movetargetoutofboundsexecption exception. Mouse movement in the test environment is more commonly used in the scene is the need to get an element of the flyover/tips, the actual application of a lot of flyover only when the mouse moved to this element appears, so this time by the execution Movetoelement ( toelement) operation, you can achieve the desired results. However, according to my personal experience, this method does not work for certain product icons, images and other flyover/tips, although moving the mouse to these icons can appear flyover when manually operated, but when using WebdriVer to simulate this move operation, although the method was executed successfully, but flyover did not come out. So in the actual application, also need to the specific product page to do the corresponding processing. Listing 7. Mouse release action actions = new Actions (driver); action.release ();//release mouse keyboard emulation action For the simulation of the keyboard, the Actions class provides keyUp (Thekey), KeyDown (Thekey), SendKeys (Keystosend), and other methods to implement. The keyboard is operated with a normal keyboard and a modifier keyboard (Modifier keys, the following section will talk about the concept of modifier keys) two: 1. For the general keyboard, the use of SendKeys (Keystosend) can be achieved, such as key TAB, Backspace and so on. Listing 8. General Keyboard Simulation SendKeys (keystosend) actions = new Actions (driver); Action.sendkeys ( KEYS.TAB);//Simulate press and release TAB Action.sendkeys (keys.space);//Simulate press and release SPACEBAR/*** key operation for an element to issue a keyboard, or input operation, You can also use this method if you enter a character in the input box. This method can also be split into: Action.click (Element). SendKeys (Keystosend). */ Action.sendkeys (element,keystosend); Note that in addition to the Actions class there is a SendKeys (Keystosend) method, the Webelement class also has a SendKeys (Keystosend) method, these two methods are basically the same for the general input operation, the difference is in the following points: ? actions SendKeys (keystosend) for modifier keys (Modifier Keys) is not released, that is, when the call Actions.sendkeys (Keys.alt); AcTions.sendkeys (Keys.control); Action.sendkeys (Keys.shift); , it is equivalent to calling Actions.keydown (Keystosend), and if you want to simulate pressing and releasing these modifier keys in a real-world application, you should call Action.sendkeys (keys. NULL) to complete this action. ? The second is that when the Actions of SendKeys (keystosend) are executed, the focus is not on the current element. So we can use SendKeys (keys.tab) to switch the focus of the element, so as to achieve the role of the selection element, the most common scenario is in the user name and password input process. ? 3rd, in Webdriver, we can use the Webelement class of SendKeys (Keystosend) to upload attachments, such as Element.sendkeys ("c:\\test\\uploadfile\\test.jpg ”); This action uploads test.jpg to the server, but uses: actions = New actions (driver); Action.sendkeys (element, "C: \ \test\\upload\\test.jpg "); Action.click (Element). SendKeys (" c:\\test\\upload\\test.jpg "); This method is not a successful upload , although Webdriver does not make an error while executing this statement, it does not actually upload the file. So to upload a file, you should still use the previous method. 2. For modifier keys (Modifier keys), they are generally used in combination with common key combinations. such as CTRL + A, ALT+F4, shift+ctrl+f and so on. ? here to explain the concept of modifier keys, modifier keys are a keyboard or a special set of keys, when it is used in conjunction with the general key, used to temporarily change the general keyboard normal behavior. For individual presses the modifier key itself generally does not trigger any keyboard events. On a keyboard on a personal computer, there are several modifier keys: Shift, Ctrl, Alt (Option), AltGr, Windows logo, Command, FN (Function). But in WebdriIn Ver, the General modifier key refers to the first three. You can click on the Wiki link below to learn more about modifier keys, Modifier key. ? Back to the above topic, the use of modifier keys in webdriver requires a KeyDown (Thekey), KeyUp (Thekey) method to operate. Listing 9. Modifier key Methods KeyDown (Thekey), KeyUp (thekey) actions = new Actions (driver); Action.keydown ( Keys.control);//Press Ctrl Action.keydown (keys.shift);//Press SHIFT Action.keydown (Key.alt);//Press the ALT key Action.keyup (Keys.control);//Release the Ctrl key Action.keyup (keys.shift);//release SHIFT key Action.keyup (Keys.alt);//Release Alt key So to close the current active window through ALT+F4, you can do this by using the following statement: Action.keydown (Keys.alt). KeyDown (KEYS.F4). KeyUp (Keys.alt). ); And if it's for the letter key a,b,c,d like the keyboard ... Can be implemented using the following statement: Action.keydown (Keys.control). Sednkeys ("a"). Perform (); in Webdriver API, KeyDown (Keys Thekey ), the KeyUp (keys Thekey) method parameter can only be modifier keys: keys.shift, Keys.alt, Keys.control, no will throw illegalargumentexception exception. Next to the call of the Action.keydown (Thekey) method, if no call is displayed Action.keyup (thekey) or Action.sendkeys (keys.null) to release it, This button will remain held down. Use The Robot class to manipulate keys without enumerating keystrokes 1. In Webdriver, the keys list most of the non-alphanumeric keys on the keyboard, from F1 to F10,numpad0 to NUMPAD9, Alt\tab\ctrl\shift And so on, you can see all the keys enumerated by the keys by the following link, enum keys. But it doesn't list all the keys on the keyboard, such as the letter keys A, B, C, D ... z, some symbolic keys such as: "{}\[]?," \ ",".?, "??,":?, "+?,"-?, "=?、、" ""?, there are some less commonly used function keys such as PRTSC, scrlk/ Nmlk. For the letter keys and symbol keys, we have mentioned earlier that we can use SendKeys ("a") and SendKeys ("/") to trigger these keyboard events. For some function combination keys, such as Fn + NMLK to close or open the number keys, or ALT+PRTSC to grab the current screen of the active window and save to the picture, through the Webdriver keys is not able to operate. This time we need to use the Java Robot class to implement this kind of key combination operation.
Selenium Mouse Events