WebDriver API——鼠标及键盘操作Actions
在自动化中我们可能需要用到鼠标或者是键盘操作,在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?&q=unicode+pua&btnG=Search">http://www.google.com.au/search?&q=unicode+pua&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的更多相关文章
- MFC--响应鼠标和键盘操作
一个程序最重要的部分之一是对鼠标和键盘操作的响应. 一. 理解鼠标事件.之前对鼠标事件的认识仅仅局限于处理控件的单击与双击事件.但实际鼠标的操作包含很多.这里将以一个画图的小程序讲解对鼠标的响应. ...
- Python模拟鼠标和键盘操作实现重复性操作
前言 由于工作需要,要利用某软件去采集数据,做重复的动作大概500多次.所以想写一个程序代替人,去点击和输入. 一开始的思路有两个:1.用Python或者windows对此软件直接操作.2.利用Pyt ...
- Qt之股票组件-股票检索--支持预览框、鼠标、键盘操作
目录 一.感慨一下 二.效果展示 三.搜索编辑框 1.编辑框 2.预览框 四.相关文章 原文链接:Qt之股票组件-股票检索--支持预览框.鼠标.键盘操作 一.感慨一下 之前做过一款炒股软件,个人觉着是 ...
- python + selenium webdriver 通过python来模拟鼠标、键盘操作,来解决SWFFileUpload调用系统底层弹出框无法定位问题
Webdriver是基于浏览器操作的,当页面上传文件使用的是flash的控件SWFFileUpload调用的时候,调用的是系统底层的文件选择弹出框 这种情况,Webdriver暂时是不支持除页面外的其 ...
- selenium 使用action进行鼠标,键盘操作
<!--test.html--> <html> <head> <title>Set Timeout</title> <script&g ...
- Selenium_Selenium WebDriver 中鼠标和键盘事件分析及扩展
在使用 Selenium WebDriver 做自动化测试的时候,会经常模拟鼠标和键盘的一些行为.比如使用鼠标单击.双击.右击.拖拽等动作:或者键盘输入.快捷键使用.组合键使用等模拟键盘的操作.在 W ...
- Selenium WebDriver 中鼠标和键盘事件分析及扩展(转)
本文将总结 Selenium WebDriver 中的一些鼠标和键盘事件的使用,以及组合键的使用,并且将介绍 WebDriver 中没有实现的键盘事件(Keys 枚举中没有列举的按键)的扩展.举例说明 ...
- Selenium WebDriver 中鼠标和键盘事件分析及扩展[转载]
原文:http://www.ibm.com/developerworks/cn/java/j-lo-keyboard/ 概念 在使用 Selenium WebDriver 做自动化测试的时候,会经常模 ...
- Selenium WebDriver 中鼠标和键盘事件分析及扩展
[From] http://www.51testing.com/html/18/631118-861557.html 在使用 Selenium WebDriver 做自动化测试的时候,会经常模拟鼠标和 ...
随机推荐
- Failed to check the status of the service报错解决
报这个错误是因为我的application_context.service.xml 文件里的的dubbo声明暴露口时的ref属性写错了. <dubbo:service interface=&qu ...
- iOS -- MBProgressHUB
高级: http://www.jianshu.com/p/485b8d75ccd4 //只有小菊花 - (void)indeterminateExample { // Show the HUD on ...
- jenkins执行单元测试,会产生大量临时文件,要及时删除,不然会把inode耗尽
jenkins的build命令:clean test -U findbugs:findbugs pmd:pmd sonar:sonar -Djava.io.tmpdir=/tmp/ -Dsonar.p ...
- Delphi图像处理 -- 颜色矩阵变换
转载自阿发伯:http://blog.csdn.net/maozefa/article/details/8316430 阅读提示: <Delphi图像处理>系列以效率为侧重点,一般 ...
- vim 树形菜单插件NERDTree 的安装
vim 树形菜单插件的安装 NERDTree 1. mkdir ~/.vim cd ~/.vim mkdir bundle mkdir autoload 2. curl -Sso ~/.vim/au ...
- Git以及github的使用方法(一)安装并设置git用户
最早Git是在Linux上开发的,很长一段时间内,Git也只能在Linux和Unix系统上跑.不过,慢慢地有人把它移植到了Windows上.现在,Git可以在Linux.Unix.Mac和Window ...
- 强化基础 Action ac = (System.Action)delegate() { Console.WriteLine("123456"); }; ac(); 委托间 也是 可以相互转换的
委托间 也是 可以相互转换的
- 手把手教你nginx/linux下如何增加网站
先进入到nginx的配置文件目录请输入以下命令 cd /alidata/server/nginx/conf/vhosts 再输入 ll 看看是不是像下面截图的一样 用神器xftp将default. ...
- centos7下MySQL的配置
1. 下载mysql的repo源 wget http:.noarch.rpm 2. 安装mysql-community-release-el7-5.noarch.rpm包 rpm .noarch.rp ...
- 关于erlang反编译的东西
在查阅了相关文档,想了解erlang反编译的东西.当然,源码可以打包成可以获取源码的,也可以保护源码的. 在ebin下,如果没有或者找不到源码,可以进行反编译,由beam文件得到erl文件. 可以通过 ...