import static org.junit.jupiter.api.Assertions.*;
import java.util.concurrent.TimeUnit;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
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;
import Test1.ChromeDriveDemo;
class DragAnddropElements {
/*
* 拖拽页面的元素
*/
WebDriver driver;
String baseurl;
@BeforeEach
void setUp() throws Exception {
//谷歌浏览器驱动
System.setProperty("webdriver.chrome.driver", "/Users/lisen/webselenium/selenium/chromedriver");
//初始化谷歌浏览器
driver=new ChromeDriver();
//定义访问网址
baseurl="https://jqueryui.com/droppable";
//设置隐性等待
driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
//窗口最大化
driver.manage().window().maximize();
}
@Test
void test() throws Exception {
//打开网址
driver.get(baseurl);
//设置隐性等待
Thread.sleep(3000);
//切换到iframe
driver.switchTo().frame(0);
//查找可拖拽元素
WebElement droptest=driver.findElement(By.id("draggable"));
//目标位置
WebElement droptesttext=driver.findElement(By.id("droppable"));
//新建Actions类
Actions actions= new Actions(driver);
//简单拖拽
// actions.dragAndDrop(droptest,droptesttext).build().perform();
// //点击之后稳住鼠标进行拖拽
// System.out.println("拖拽完成,测试正常");
//点住元素进行拖拽
actions.clickAndHold(droptest).moveToElement(droptesttext).release().build().perform();
System.out.println("拖拽功能完成,测试正常");
}
@AfterEach
void tearDown() throws Exception {
//等待
Thread.sleep(3000);
//关闭浏览器
driver.quit();
}
}
来源:CSDN
作者:weixin_43487218
链接:https://blog.csdn.net/weixin_43487218/article/details/104014748