《手把手教你》系列技巧篇(二十九)-java+ selenium自动化测试- Actions的相关操作上篇(详解教程)

2023-04-24,,

1.简介

  有些测试场景或者事件,Selenium根本就没有直接提供方法去操作,而且也不可能把各种测试场景都全面覆盖提供方法去操作。比如:就像鼠标悬停,一般测试场景鼠标悬停分两种常见,一种是鼠标悬停在某一个元素上方,然后会出现下拉子菜单,第二种就是在搜索输入过程,选择自动补全的字段。关于鼠标悬停,selenium把这个方法放在了Actions.java文件中,先来看看鼠标悬停出现下拉菜单的情况。

2.鼠标悬停出现下拉菜单

  鼠标悬停出现下拉菜单,顾名思义就是:鼠标悬停在某一元素上出现下拉菜单。

2.1项目实战

  宏哥这里用百度首页的更多元素,悬停出现拉来菜单,然后点击“音乐”为例进行实战。

2.2代码设计

  代码设计如下:

2.3参考代码

  参考代码如下:

package lessons;

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 org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait; /**
* @author 北京-宏哥
*
* 《手把手教你》系列技巧篇(二十九)-java+ selenium自动化测试- Actions的相关操作上篇(详解教程)
*
* 2021年9月26日
*/
public class ActionMusic { public static void main(String[] args) throws InterruptedException { System.setProperty("webdriver.gecko.driver",".\\Tools\\chromedriver.exe"); // 指定驱动路径 WebDriver driver = new ChromeDriver();
// 最大化窗口
driver.manage().window().maximize();
// 打开百度首页
driver.get("http://wwww.baidu.com");
// 声明一个Action对象
Actions action = new Actions(driver);
// 鼠标移动到 更多产品 上
action.moveToElement(driver.findElement(By.xpath("//a[text()='更多']")))
.perform();
// 显示等待时间10s 等 全部产品>> 出现
WebDriverWait w = new WebDriverWait(driver, 10);
w.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By
.xpath("//a[text()='查看全部百度产品 >']")));
// 等待的元素出现后点击 音乐
WebElement cp = driver.findElement(By.xpath("//a/div[text()='音乐']"));
cp.click();
}
}

2.4运行代码

1.运行代码,右键Run AS->java Application,控制台输出,如下图所示:

2.运行代码后电脑端的浏览器的动作,如下小视频所示:

3.搜索输入过程,选择自动补全的字段

  在搜索框输入关键词后,后提示相关内容,然后将其补全进行搜索。

3.1项目实战

  宏哥这里就以百度搜索为例,进行实战。

3.2代码设计

  代码设计如下:

3.3参考代码

  参考代码如下:

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; /**
* @author 北京-宏哥
*
* 《手把手教你》系列技巧篇(二十九)-java+ selenium自动化测试- Actions的相关操作上篇(详解教程)
*
* 2021年9月26日
*/
public class SearchOpration { 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(5, TimeUnit.SECONDS); driver.get("https://www.baidu.com/"); Thread.sleep(1000); // 设置
WebElement inputbox = driver.findElement(By.id("kw"));
inputbox.sendKeys("selenium a"); // 自动补全其中一个选择项
WebElement auto_text = driver.findElement(By.xpath("//*[@id='form']/div/ul/li[@data-key='selenium appium']")); Actions action = new Actions(driver);
action.moveToElement(auto_text).click().perform();
}
}

3.4运行代码

1.运行代码,右键Run AS->java Application,控制台输出,如下图所示:

2.运行代码后电脑端的浏览器的动作,如下小视频所示:

4.小结

  好了时间也不早了,Actions类中鼠标悬停方法就介绍到这里。提前祝大家、小伙伴和童鞋们国庆节快乐!!!

《手把手教你》系列技巧篇(二十九)-java+ selenium自动化测试- Actions的相关操作上篇(详解教程)的相关教程结束。

《《手把手教你》系列技巧篇(二十九)-java+ selenium自动化测试- Actions的相关操作上篇(详解教程).doc》

下载本文的Word格式文档,以方便收藏与打印。