测试人员在进行软件测试时,常需要模拟键盘或鼠标的操作,用测试代码实现模拟键盘或鼠标操作时,一般使用 Python 的 ActionChains 和 Java 的 Actions 来处理。
模拟鼠标的操作包括单击、双击、拖动等。测试程序中使用 ActionChains 或者 Actions 方法,实现模拟鼠标的操作。具体应用是,可以先将(单击、双击、拖动等)一些列动作按操作顺序存入队列。此时还没有触发执行上模拟鼠标的操作。程序想要触发执行上模拟鼠标的操作,需要调用 ActionChains 或者 Actions 方法下的 perform()方法。perform()方法队列中的事件会一次执行上述操作。
1.引入依赖
我们使用 ActionChains 或者 Actions 方法时,需要先导入相对应的依赖,如下的 Python 版和 Java 版代码所示。
 #引入依赖from selenium.webdriver import ActionChains
       复制代码
 
 import org.openqa.selenium.interactions.Actions;
       复制代码
 2 实战演练
(1)点击操作演示
下面代码中,action 用以模拟键盘或鼠标的实例对象,on_element 用以传递一个元素,默认值为 None。
模拟键盘或鼠标操作时,如果在 on_element 中指定元素时,模拟操作时会单击指定位置的元素;如果不指定,模拟操作时会单击当前光标的位置。
 action.click(on_element=None)
       复制代码
 
 Actions action = new Action(webdriver);action.click(on_elenium=None);
       复制代码
 
用鼠标模拟长按某个元素的操作代码如下(Python 版和 Java 版)。
 action.click_and_hold(on_element=None)
       复制代码
 
 Actions action = new Actions(webdriver);action.clickAndHold(on_element=None);
       复制代码
 
用鼠标模拟执行右键操作的代码如下(Python 版和 Java 版)
 action.context_click(on_element=None)
       复制代码
 
 Actions action = new Actions(webdriver);action.contextClick(on_element=None)
       复制代码
 
用鼠标模拟执行左键双击的代码如下(Python 版和 Java 版)
 action.double_click(on_element=None)
       复制代码
 
 Actions action = new Actions(webdriver);action.doubleClick(on_element=None);
       复制代码
 
模拟用鼠标拖曳起始的元素到目标元素的操作的代码如下(Python 版和 Java 版)。
注:程序中的 source 代表起始元素,target 代表目标元素。
 action.drag_and_drop(source,target)
       复制代码
 
 Actions action = new Actions(webdriver);action.dragAndDrop(WebElement source,WebElement target);
       复制代码
 
模拟用鼠标将目标拖动到指定的位置的操作代码如下(Python 版和 Java 版)。
 # xoffset 和 yoffset 是相对于source左上角为原点的偏移量action.drag_and_drop_by_offset(source,xoffset,yoffset)
       复制代码
 
 Actions action = new Actions(webdriver);action.dragAndDropBy(WebElement source,int xoffset,int yoffset);
       复制代码
 
(2)按键操作的演示
我们编程中使用 key_down()或者 keyDown()方法可以模拟某些组合键事件,如按下 Ctrl+C 组合键,如下是 Python 版和 Java 版演示代码。
 action.key_down(value,element=None)
       复制代码
 
 Action action = new Action(webdriver);action.keyDown(element,value);
       复制代码
 
我们编程中使用 key_up()或者 keyUp()方法模拟松开键盘中某个按键,如按下 Ctrl+C 组合键并且释放,如下是 Python 版和 Java 版演示代码。
 ActionChains(driver).key_down(Keys.CONTROL\     .send_keys('c').key_up(keys.CONTROL).perform())
       复制代码
 
 Actions action = new Actions(webdriver);action.keyDown(keys.CONTROL).sendkeys("c").keysUp(keys.CONTROL).perform();
       复制代码
 
(3)移动光标操作的演示
把光标移动到某一个位置,需要用两个坐标标识起始和目标的位置,演示代码如下(Python 版和 Java 版)。
 # xoffset 和 yoffset 是相对于网页左上角的偏移量action.move_by_offset(xoffset,yoffset)
       复制代码
 
 Actions action = new Actions(webdriver);action.moveByoffset(xoffset,yoffset);
       复制代码
 
将鼠标指针移动到指定元素的位置,演示代码如下(Python 版和 Java 版)。
 action.move_to_element(to_element)
       复制代码
 
 Actions action = new Actions(webdriver);action.moveToElement(to_element);
       复制代码
 
移动鼠标光标到相对于某个元素的便宜位置,演示代码如下(Python 版和 Java 版)
 # xoffset 和 yoffset 是相对于网页窗口显示左上角的偏移量action.moveToElement(to_element);
       复制代码
 
 Action action = new Actions(webdriver);action.moveToElement(to_element,xoffset,yoffset);
       复制代码
 
(4)其他的操作
1)执行 ActionChains 中的操作
前面介绍的方法会将所有操作按顺序存入列队,要执行这些操作,需要调用 perform()方法,演示代码如下(Python 版和 Java 版)。
 action.move_to_element_with_offset(to_element,xoffset,yoffset).perform()
       复制代码
 
 Actions action = new Actions(webdriver);Action.moveToElement(to_element,int xoffset,int yoffset).perform();
       复制代码
 
2)释放按下鼠标键的操作,演示代码如下(Python 版和 Java 版)。
 Actions action = new Actions(webdriver);action.release(on_element=None)
       复制代码
 
3)向焦点元素位置输入值
焦点元素就是,我们使用 Tab 键移动光标时,那些被选中的元素。向焦点元素位置输入值的演示代码如下(Python 版和 Java 版)。
 action.send_keys(*keys_to_send)
       复制代码
 
 Actions action = new Actions(webDriver);Action.sendkeys(*keys_to_send)
       复制代码
 
4)向指向的元素输入数据,演示代码如下(Python 版和 Java 版)。
 Action.send_keys_to_element(element,*keys_to_send)
       复制代码
 
 Actions action = new Actions(webDriver);action.sendkeys(element,krys_to_send);
       复制代码
 搜索微信公众号:TestingStudio 霍格沃兹的干货都很硬核
评论