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 做自动化测试的时候,会经常模拟鼠标和 ...
随机推荐
- 洛谷——P2149 [SDOI2009]Elaxia的路线
P2149 [SDOI2009]Elaxia的路线 题目描述 最近,Elaxia和w的关系特别好,他们很想整天在一起,但是大学的学习太紧张了,他们 必须合理地安排两个人在一起的时间.Elaxia和w每 ...
- ML | SVM
What's xxx An SVM model is a representation of the examples as points in space, mapped so that the e ...
- tcp异常断开的重连解决方法
1.select超时重连 http://bbs.chinaunix.net/thread-4162149-1-1.html 2.http://bbs.csdn.net/topics/350074818 ...
- POJ 3107 Godfather (树重心)
题目链接:http://poj.org/problem?id=3107 题意: 数重心,并按从小到大输出. 思路: dfs #include <iostream> #include < ...
- k8s之ingress及ingress controller
1.ingress概述 图解:第一个service起到的作用是:引入外部流量,也可以不用此方式,以DaemonSet控制器的方式让Pod共享节点网络,第二个service的作用是:对后端pod分组,不 ...
- JS没有contains方法,可以用indexof实现
我们很多时候会不自觉的在js代码里对一个字符串进行如下操作: str.contains("substr"); 但是js里面没有这个方法去判断字符串str是不是包含substr,而j ...
- [Bug]由于代码已经过优化或者本机框架位于调用堆栈之上,无法计算表达式的值"的解决方法(转)
原因 如果使用 Response.End.Response.Redirect 或 Server.Transfer 方法,将出现 ThreadAbortException 异常.您可以使用 try-ca ...
- 【java】spring项目中 对entity进行本类间的克隆
方法1: [使用spring自带BeanUtils实现克隆] [要求:需要被克隆的类实现Cloneable接口并且重写clone()方法] >例子: >>实体: package co ...
- 线性回归,logistic回归分类
学习过程 下面是一个典型的机器学习的过程,首先给出一个输入数据,我们的算法会通过一系列的过程得到一个估计的函数,这个函数有能力对没有见过的新数据给出一个新的估计,也被称为构建一个模型.就如同上面的线性 ...
- Linux 主机被入侵后的处理案例
Linux主机被入侵后的处理案例 提交 我的留言 加载中 已留言 一次Linux被入侵后的分析 下面通过一个案例介绍下当一个服务器被rootkit入侵后的处理思路和处理过程,rootkit攻击是Lin ...