码迷,mamicode.com
首页 > Windows程序 > 详细

WebDriver API——鼠标及键盘操作Actions

时间:2017-02-28 00:12:56      阅读:611      评论:0      收藏:0      [点我收藏+]

标签:poi   unicode   represent   java   actions   ima   on()   bsp   can   

     在自动化中我们可能需要用到鼠标或者是键盘操作,在webdriver中是Actions类进行这些操作的。

代码如下:

Actions action = new Actions(driver);                     //-------定义一个action对象
        
        action.click();
        action.click(searchBt);                                   //-------单击操作
        action.doubleClick().perform();
        action.doubleClick(searchBt).perform();                             //-------双击操作
        action.clickAndHold().perform();
        action.clickAndHold(searchBt).perform();                            //-------悬停操作
        action.contextClick().perform();
        action.contextClick(searchBt).perform();                            //-------右击操作
        action.dragAndDrop(searchBt, searchBt).perform();                   //-------拖拽操作 从一个元素拖拽到目标元素
        

这是几个常用操作的简单用法,老规矩,看下源码是怎么定义的action类的:

技术分享

我们可以看到actons类有多种构造函数和方法,都是根据平时我们不同的需要来进行使用,我们也可以根据自己项目来封装这些操作,做个简单的例子:

package com.testngDemo;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.interactions.Actions;

public class Demo_ActionsHelper {
    protected WebDriver driver;
    public Demo_ActionsHelper(WebDriver driver)
    {
        this.driver = driver;
    }
    /**
     *  单击操作
     * @param by 定位元素
     */
    public void click(By by)
    {
        driver.findElement(by).click();
    }
    /**
     *  双击操作
     * @param by
     */
    public void doubleClick(By by)
    {
        new Actions(driver).doubleClick(driver.findElement(by)).perform();
    }
    /**
     *  右击点开菜单
     * @param by
     */
    public void contextmenu(By by)
    {
        new Actions(driver).contextClick(driver.findElement(by)).perform();
    }
    
}

然后调用自己封装好的类

技术分享

仅提供一个小的例子 ,具体的要根据自己项目的需要不断完善和维护脚本,丰富自己项目的脚本代码,提高自动化测试脚本开发的效率。

 

那么我们再来看下键盘是如何调用的?

键盘的调用我们是调用的Keys这个类

/*
Copyright 2007-2012 Selenium committers
Copyright 2013 Software Freedom Conservancy

Licensed 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.
*/

package org.openqa.selenium;

import java.util.Arrays;

/**
 * Representations of pressable keys that aren‘t text.  These are 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 enum Keys implements CharSequence {

  NULL         (‘\uE000‘),
  CANCEL       (‘\uE001‘), // ^break
  HELP         (‘\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 keys
  NUMPAD0      (‘\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 keys
  F1           (‘\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 char keyCode;

  Keys(Keys key) {
    this(key.charAt(0));
  }

  Keys(char keyCode) {
    this.keyCode = keyCode;
  }

  public char charAt(int index) {
    if (index == 0) {
      return keyCode;
    }

    return 0;
  }

  public int length() {
    return 1;
  }

  public CharSequence subSequence(int start, int end) {
    if (start == 0 && end == 1) {
      return String.valueOf(keyCode);
    }

    throw new IndexOutOfBoundsException();
  }

  @Override
  public String toString() {
    return String.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 string.
   *
   * 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 static String chord(CharSequence... value) {
    return chord(Arrays.asList(value));
  }

  /**
   * @see #chord(CharSequence...)
   */
  public static String chord(Iterable<CharSequence> value) {
    StringBuilder builder = new StringBuilder();

    for (CharSequence seq : value) {
      builder.append(seq);
    }

    builder.append(Keys.NULL);
    return builder.toString();
  }

  /**
   * Get the special key representation, {@link Keys}, of the supplied character if there is one. If
   * there is no special key tied to this character, null will be returned.
   *
   * @param key unicode character code
   * @return special key linked to the character code, or null if character is not a special key
   */
  public static Keys getKeyFromUnicode(char key) {
    for (Keys unicodeKey : values()) {
      if (unicodeKey.charAt(0) == key) {
        return unicodeKey;
      }
    }

    return null;
  }

}

它为我们提供了键盘上的绝大多数的键位,我们可以直接输入键位名称来实现键盘的调用

技术分享

当然我们也可以根据自己的需要将特定的操作、组合操作封装起来供我们自己使用。

另外在actions类中的keyup keydown修饰键方法,参数参数只能是修饰键:Keys.SHIFT、Keys.ALT、Keys.CONTROL, 否者将抛出 IllegalArgumentException 异常。 其次对于 action.keyDown(theKey) 方法的调用,如果没有显示的调用 action.keyUp(theKey) 或者 action.sendKeys(Keys.NULL) 来释放的话,这个按键将一直保持按住状态。

 

WebDriver API——鼠标及键盘操作Actions

标签:poi   unicode   represent   java   actions   ima   on()   bsp   can   

原文地址:http://www.cnblogs.com/dreamyu/p/6476805.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!