WebDriver中的Actions对象
我们可以利用Actions对象来模拟鼠标的操作以及页面的拖拽
1、模拟鼠标的双击操作:
package com.example.tests; import static org.junit.Assert.*; import org.junit.*; import org.openqa.selenium.*; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.interactions.Actions; public class Selenium2 { WebDriver driver = new FirefoxDriver(); @Test public void tableTest() { driver.get("D:\\demo\\DoubleClickDemo.html"); WebElement message = driver.findElement(By.id("message")); // 验证初始字体为14px assertEquals("14px", message.getCssValue("font-size")); Actions builder = new Actions(driver); builder.doubleClick(message).build().perform(); // 验证点击后字体变为20px assertEquals("20px", message.getCssValue("font-size")); driver.close(); } }
2、模拟拖拽:
dragAndDrop(source, target)方法
package com.example.tests; import static org.junit.Assert.*; import org.junit.*; import org.openqa.selenium.*; import org.openqa.selenium.ie.InternetExplorerDriver; import org.openqa.selenium.interactions.Actions; public class Selenium2 { @Test public void testDragDrop() { WebDriver driver = new InternetExplorerDriver(); driver.get("D:\\demo\\DragAndDrop.html"); WebElement source = driver.findElement(By.id("draggable")); WebElement target = driver.findElement(By.id("droppable")); Actions builder = new Actions(driver); builder.dragAndDrop(source, target).perform(); try { assertEquals("Dropped!", target.getText()); } catch (Error e) { e.printStackTrace(); }finally{ driver.close(); } } }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。