我们已经看过Selenium中的Actions课,并学习了有关它的基础知识。如果你想 在这里阅读 有关更多详细信息。基本动作类用于执行各种鼠标和键盘驱动的动作,例如单击和按住,鼠标悬停等。在本博客中,我们将使用Selenium中的Actions类来执行拖放动作。
许多网站都提供拖放功能,因此我们将看到如何使其自动化。
影片教学-
出于演示目的,我使用示例拖放-
//jqueryui.com/droppable/
让我们看一下执行拖放的简单测试用例-
import java.util.List; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.openqa.selenium.By; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.interactions.Actions; public class DragAndDrop { ChromeDriver driver; @Before public void setup(){ System.setProperty("webdriver.chrome.driver", "C:\\Softwares\\chromedriver_win32\\chromedriver.exe"); driver=new ChromeDriver(); driver.manage().window().maximize(); // start the application driver.get("//jqueryui.com/droppable/"); } @Test public void testBootStrap() throws Exception { // Add 10 seconds wait Thread.sleep(10000); // Create object of actions class Actions act=new Actions(driver); driver.switchTo().frame(driver.findElement(By.xpath("//iframe[@class='demo-frame']"))); // find element which we need to drag WebElement drag=driver.findElement(By.xpath("//*[@id='draggable']")); // find element which we need to drop WebElement drop=driver.findElement(By.xpath("//*[@id='droppable']")); // this will drag element to destination act.dragAndDrop(drag, drop).build().perform(); // Add 2 seconds wait Thread.sleep(2000); } @After public void tearDown() { driver.quit(); } }