Selenium+Java——测试Web端的方法集合(二)
4、输入
//1.输入 driver.findElement(By.xpath("//xpathExpression")).sendKeys("test"); //2.输入 Actions actions = new Actions(driver); actions.sendKeys("test").perform(); //3.输入 // 创建一个StringSelection对象,用于存储要复制的文本内容 StringSelection selection = new StringSelection("文本"); // 获取系统剪贴板 Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard(); // 将StringSelection对象放入剪贴板 clipboard.setContents(selection, null); // 模拟Ctrl+V操作 Robot robot = new Robot(); robot.keyPress(KeyEvent.VK_CONTROL); robot.keyPress(KeyEvent.VK_V); robot.keyRelease(KeyEvent.VK_V); robot.keyRelease(KeyEvent.VK_CONTROL);
5、清空
//1.清空 driver.findElement(By.xpath("//xpathExpression")).clear(); //2.清空 Actions actions = new Actions(driver); actions.keyDown(Keys.CONTROL).perform(); actions.sendKeys("a").perform(); actions.keyUp(Keys.CONTROL).perform(); actions.sendKeys(Keys.DELETE).perform(); //3.清空 Robot robot=new Robot(); robot.keyPress(KeyEvent.VK_CONTROL); robot.keyPress(KeyEvent.VK_A); Thread.sleep(200); robot.keyRelease(KeyEvent.VK_A); robot.keyRelease(KeyEvent.VK_CONTROL); Thread.sleep(200); robot.keyPress(KeyEvent.VK_DELETE); robot.keyRelease(KeyEvent.VK_DELETE);
6、下拉框选择
import org.openqa.selenium.support.ui.Select; //1.使用Select类处理普通下拉框 WebElement dropdown = driver.findElement(By.id("dropdownId")); Select select = new Select(dropdown); select.selectByVisibleText("Option 1");// 通过可见文本选择选项 select.selectByValue("value1");// 通过值选择选项 select.selectByIndex(2);// 通过索引选择选项 //2.使用WebElement处理自定义下拉框或其他类型的下拉框 driver.findElement(By.id("dropdownId")).click(); driver.findElement(By.xpath("//option[text()='Option 1']")).click(); //3.处理多选下拉框 driver.findElement(By.id("multiDropdownId")).click(); driver.findElement(By.xpath("//option[text()='Option 1']")).click(); driver.findElement(By.xpath("//option[text()='Option 2']")).click(); //4.取消所有已选择的选项 WebElement dropdown = driver.findElement(By.id("dropdownId")); Select select = new Select(dropdown); select.deselectAll(); //5.或者取消单个已选择的选项 WebElement option1 = driver.findElement(By.xpath("//option[text()='Option 1']")); select.deselectByValue(option1.getAttribute("value")); select.deselectByVisibleText("Option 1"); select.deselectByValue("value1"); select.deselectByIndex(2); //6.获取所有已选中的选项 WebElement dropdown = driver.findElement(By.id("dropdownId")); Select select = new Select(dropdown); List<WebElement> selectedOptions = select.getAllSelectedOptions(); for (WebElement option : selectedOptions) { System.out.println("已选中选项:" + option.getText()); } //7.获取当前被选中的第一个选项 WebElement dropdown = driver.findElement(By.id("dropdownId")); Select select = new Select(dropdown); WebElement firstSelectedOption = select.getFirstSelectedOption(); System.out.println("当前被选中的选项:" + firstSelectedOption.getText()); //8.获取被Select对象包装的原始WebElement元素 WebElement dropdown = driver.findElement(By.id("dropdownId")); Select select = new Select(dropdown); WebElement wrappedElement = select.getWrappedElement(); String elementId = wrappedElement.getAttribute("id"); System.out.println("下拉框元素的ID:" + elementId); //9.检查下拉框元素是否支持多选 WebElement dropdown = driver.findElement(By.id("dropdownId")); Select select = new Select(dropdown); boolean isMultiple = select.isMultiple();
7、等待
//1.隐式等待(Implicit Wait) //隐式等待是设置一个全局的等待时间,在查找元素时如果找不到立即返回,并等待指定的时间再进行下一步操作。 driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); //2.显式等待(Explicit Wait) //显式等待是在特定条件满足之前等待。可以指定等待的最长时间,以及每隔一段时间检查一次条件是否满足 WebDriverWait wait = new WebDriverWait(driver, 10); WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("elementId"))); //3.暂停当前线程的执行一段指定的时间 //暂停执行 5 秒钟 Thread.sleep(5000);
8、悬浮元素
// 找到需要悬浮的元素 WebElement elementToHover = driver.findElement(By.id("elementId")); // 创建 Actions 对象 Actions actions = new Actions(driver); // 将鼠标悬停在该元素上 actions.moveToElement(elementToHover).perform();
9、拖拽元素
//1.拖拽 WebElement sourceElement = driver.findElement(By.id("sourceElementId")); WebElement targetElement = driver.findElement(By.id("targetElementId")); Actions actions = new Actions(driver); actions.dragAndDrop(sourceElement, targetElement).perform(); //2.拖拽,需要拖拽A点和B点的坐标 org.sikuli.script.Screen sikuli = new org.sikuli.script.Screen(); Location from = new Location(Integer.parseInt(locationA_X), Integer,parseInt(locationA_Y)); Location To = new Location(Integer,parseInt(locationB_X), Integer,parseInt(locationB_Y)); sikuli.dragDrop(from, To); //3.拖拽,需要拖拽A点的图片和B点的图片 org.sikuli.script.Screen sikuli = new org.sikuli.script.Screen(); Match matcheFrom = sikuli.find(imageFromFile); Match matcheTo = sikuli.find(imageTofile); Location from = matchefrom.getTarget(); Location To = matcheTo.getTarget(); sikuli.dragDrop(from,To);
如果有写的不对或者有追加的地方欢迎补充私聊
#自动化测试##selenium##测试#自动化测试学习 文章被收录于专栏
记录工作中的自动化测试学习内容,例如:手机自动化测试学习,web端自动化测试学习等..