本文补充Actions类中的拖拽操作,之前没有找到相应的demo网站来介绍拖拽操作,今天找到了一个,所以,补上一个Selenium中处理拖拽的场景例子。拖拽的动作定义是,一个元素从起始位置拖动到目标位置,这个过程叫拖拽。直接来看看下面的脚本实现过程。
package lessons;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
public class ElementOpration {
public static void main(String[] args) throws Exception {
System.setProperty("webdriver.chrome.driver", ".\\Tools\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(8, TimeUnit.SECONDS);
driver.get("http://jqueryui.com/resources/demos/droppable/default.html");
Thread.sleep(2000);
// 定位能拖拽的元素
WebElement move_ele = driver.findElement(By.id("draggable"));
// 定位拖拽目标位置元素
WebElement target_ele = driver.findElement(By.id("droppable"));
Actions action = new Actions(driver);
action.dragAndDrop(move_ele, target_ele).build().perform();
// 验证拖拽成功
assert(driver.findElement(By.xpath("//*[@id='droppable']/p[text()='Dropped!']")).isDisplayed() == true);
}
}
上面刚刚,拖拽后第二个元素内部的文字有变化,我们通过获取文字变化判断是否显示在该位置来断言拖拽是否成功。isDisplayed方法,前面文章介绍过。