Webdriver api--Mouse and keyboard operation actions

Source: Internet
Author: User

In automation we may need to use a mouse or a keyboard operation, which is performed by the actions class in Webdriver.

The code is as follows:

Actions action =NewActions (driver);//-------Define an Action objectAction.click ();                                   Action.click (SEARCHBT); //-------Click actionAction.doubleclick (). perform ();                             Action.doubleclick (SEARCHBT). Perform (); //-------Double-click actionAction.clickandhold (). perform ();                            Action.clickandhold (SEARCHBT). Perform (); //-------Hover OperationsAction.contextclick (). perform ();                            Action.contextclick (SEARCHBT). Perform (); //-------Right-click actionAction.draganddrop (SEARCHBT, SEARCHBT). Perform ();//-------drag-and-drop operation from an element to the target element        

This is a simple use of several common operations, the usual, see how the source code is defined by the action class:

We can see that the Actons class has a number of constructors and methods that are used in accordance with our different needs, and we can encapsulate these operations according to our own projects, and make a simple example:

 PackageCom.testngdemo;Importorg.openqa.selenium.By;ImportOrg.openqa.selenium.WebDriver;Importorg.openqa.selenium.interactions.Actions; Public classDemo_actionshelper {protectedWebdriver Driver;  Publicdemo_actionshelper (webdriver driver) { This. Driver =driver; }    /*** Click action *@paramBy positioning elements*/     Public voidClick (by) {driver.findelement (by). Click (); }    /*** Double-click Operation *@param by*/     Public voidDoubleClick (by) {NewActions (Driver). DoubleClick (Driver.findelement (by)). Perform (); }    /*** Right click to open menu *@param by*/     Public voidContextMenu (by) {NewActions (Driver). Contextclick (Driver.findelement (by)). Perform (); }    }

Then call your own packaged class.

Provide only a small example, in accordance with the needs of their own projects to continuously improve and maintain the script, enrich their own project script code, improve the efficiency of automated test script development.

So let's see how the keyboard is called?

The Call of the keyboard we are calling the keys this class

/*Copyright 2007-2012 Selenium committerscopyright software Freedom conservancylicensed under the Apache License, V Ersion 2.0 (the "License"); If you are not use this file except in compliance with the license.you may obtain a copy of the Li Cense athttp://www.apache.org/licenses/LICENSE-2.0unless required by applicable law or agreed to writing, softwaredistributed under the License was 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 andlimitations under the License.*/ PackageOrg.openqa.selenium;Importjava.util.Arrays;/*** Representations of pressable keys that aren ' t text. These is stored in the Unicode PUA (Private * Use area) code points, 0xe000-0xf8ff. * * @see<a href= "Http://www.google.com.au/search?&amp;q=unicode+pua&amp;btng=search ">Http://www.google.com.au/search?&amp;q=unicode+pua&amp;btnG=Search</a>*/ Public enumKeysImplementscharsequence {NULL (' \ue000 '), CANCEL (' \ue001 '),//^breakHelp (' \ue002 '), Back_space (' \ue003 '), TAB (' \ue004 '), CLEAR (' \ue005 '), RETURN (' \ue006 '), ENTER (' \ue007 '), SHIFT (' \ue008 '), Left_shift (Keys.shift), CONTROL (' \ue009 '), Left_control (Keys.control), ALT (' \ue00a '), Left_alt (Keys.alt), PAUSE (' \ue00b '), ESCAPE (' \ue00c '), SPACE (' \ue00d '), Page_up (' \ue00e '), Page_down (' \ue00f '), END (' \ue010 '), HOME (' \ue011 '), left (' \ue012 '), Arrow_left (Keys.left), Up (' \ue013 '), Arrow_up (Keys.up), right (' \ue014 '), Arrow_right (Keys.right), Down (' \ue015 '), Arrow_down (Keys.down), INSERT (' \ue016 '), DELETE (' \ue017 '), semicolon (' \ue018 '), EQUALS (' \ue019 '),  //Number pad keysNUMPAD0 (' \ue01a '), NUMPAD1 (' \ue01b '), NUMPAD2 (' \ue01c '), NUMPAD3 (' \ue01d '), NUMPAD4 (' \ue01e '), NUMPAD5 (' \ue01f '), NUMPAD6 (' \ue020 '), NUMPAD7 (' \ue021 '), NUMPAD8 (' \ue022 '), NUMPAD9 (' \ue023 '), MULTIPLY (' \ue024 '), ADD (' \ue025 '), SEPARATOR (' \ue026 '), SUBTRACT (' \ue027 '), DECIMAL (' \ue028 '), DIVIDE (' \ue029 '),  //Function KeysF1 (' \ue031 '), F2 (' \ue032 '), F3 (' \ue033 '), F4 (' \ue034 '), F5 (' \ue035 '), F6 (' \ue036 '), F7 (' \ue037 '), F8 (' \ue038 '), F9 (' \ue039 '), F10 (' \ue03a '), F11 (' \ue03b '), F12 (' \ue03c '), META (' \ue03d '), COMMAND (Keys.meta), Zenkaku_hankaku (' \ue040 '); Private Final CharKeyCode; Keys (key) { This(Key.charat (0)); } Keys (Charkeycode) {     This. keycode =KeyCode; }   Public CharCharAt (intindex) {    if(Index = = 0) {      returnKeyCode; }    return0; }   Public intLength () {return1; }   PublicCharsequence subsequence (intStartintend) {    if(start = = 0 && End = = 1) {      returnstring.valueof (keycode); }    Throw Newindexoutofboundsexception (); } @Override PublicString toString () {returnstring.valueof (keycode); }  /*** Simulate pressing many keys at once in a "chord".   Takes a sequence of keys.xxxx or strings; * Appends each of the values to a string, and adds the chord termination key (Keys.null) and * Returns the resultant str   Ing.  * * Note:when the low-level webdriver key handlers see Keys.null, active modifier Keys * (CTRL/ALT/SHIFT/ETC) Release   via a KeyUp event. * * Issue:http://code.google.com/p/webdriver/issues/detail?id=79   */   Public StaticString chord (Charsequence ... value) {returnChord (Arrays.aslist (value)); }  /**   * @see#chord (Charsequence ...) */   Public StaticString Chord (iterable<charsequence>value) {StringBuilder Builder=NewStringBuilder ();  for(charsequence seq:value) {builder.append (seq);    } builder.append (Keys.null); returnbuilder.tostring (); }  /*** Get The Special key representation, {@linkKeys}, the of the supplied character if there is one.   If * There is no special key tied to this character, NULL would be returned. *   * @paramkey Unicode character code *@returnspecial key linked to the character code, or null if character are not a special key*/   Public StaticKeys Getkeyfromunicode (Charkey) {     for(Keys unicodekey:values ()) {if(Unicodekey.charat (0) = =key) {        returnUnicodekey; }    }    return NULL; }}

It provides us with the majority of keys on the keyboard, we can directly enter the key name to implement the keyboard call

Of course, we can also according to our own needs to the specific operation, combined operation package for our own use.

In addition to the KeyUp KeyDown modifier key method in the actions class, parameter parameters can only be modifier keys: keys.shift, Keys.alt, Keys.control, no will throw illegalargumentexception exception. Next to the Action.keydown (Thekey) method call, if there is no display call Action.keyup (Thekey) or Action.sendkeys (keys.null) to release, this button will remain held down.

Webdriver api--Mouse and keyboard operation actions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.