[三分钟]web自动化测试(二):selenium自动化测试常用函数(上)
文章目录
- 1.元素定位
- 1.1 cssSelector(选择器)
- 1.2 xpath
- 1.3小示例
- 2.操作测试对象
- 2.1点击/提交对象-click()
- 2.2 模拟按键输入-sendKeys("")
- 2.3 清除文本内容-clear()
- 2.4 获取文本信息-getText()
- 2.5 获取当前页面标题-getTitle()
- 2.6获取当前页面URL-getCurrentUrl()
- 3.窗口
- 3.1 切换窗口
- 3.2 窗口设置大小
- 3.3 屏幕截图
- 3.4关闭窗口
1.元素定位
web自动化测试的操作核心是能够找到页面对应的元素。
如何手动在网页页面中查找元素
-
打开开发者工具
方法一:右键---->检查
方法二:ctrl + shift + i
方法三:F12
-
点击右上角的箭头
-
例如找搜索框对应的元素
在web自动化测试中常见的元素定位方式非常多,如id,classname,tagname,xpath,cssSelector。常用的主要由cssSelector和xpath
1.1 cssSelector(选择器)
功能:选中页面中指定的标签元素
选择器的种类分为基础选择器和复合选择器常见的元素定位方式可以通过id选择器和子类选择器来
进行定位。
- id选择器
功能:选取某个id的元素
格式:#id
例如:百度搜索框的id选择器,该元素的id为kw,id选择器为#kw
- 子类选择器
功能:选中某个元素下的直接子元素。
格式:div > p
例如:寻找百度热搜元素
复制到搜索框:#s-hotsearch-wrapper > div > a.hot-title > div
#s-hotsearch-wrapper > div > a.hot-title > div ==> 往#s-hotsearch-wrapper的子类里面找
1.2 xpath
XML路径语言,不仅可以在XML文件中查找信息,还可以在HTML页面中选取元素。
语法:
1.2.1 获取HTML页面所有的节点
//*
1.2.2 获取HTML页面指定的节点
//[指定节点]
//ul :获取HTML⻚⾯所有的ul节点
//input:获取HTML⻚⾯所有的input节点
1.2.3 获取⼀个节点中的直接子节点
/
//span/input
1.2.4 获取⼀个节点的父节点
..
//input/.. 获取input节点的⽗节点
1.2.5 实现节点属性的匹配
[@...]
//*[@id='kw'] 匹配HTML⻚⾯中id属性为kw的节点
1.2.6 使用指定索引的方式获取对应的节点内容
注意:xpath的索引是从1开始的。
百度首页通过://div/ul/li[3] 定位到第三个百度热搜标签
1.3小示例
使用选择器和xpath定位百度热搜,并查找所有的热搜词条
方法名 | 作用 | 返回值 |
---|---|---|
findElement(By) | 在页面中查找元素 | WebElement |
findElements(By) | 在页面中查找多个元素 | List< WebElement > |
public class FirstAutoTest {WebDriver driver = null;void createDriver() {// 1.打开谷歌浏览器WebDriverManager.chromedriver().setup();// 浏览器配置:允许访问所有链接ChromeOptions options = new ChromeOptions();options.addArguments("--remote-allow-origins=*");driver = new ChromeDriver(options);// 2.输入网址:https://www.baidu.comdriver.get("https://www.baidu.com");}// 测试元素定位void test02() {createDriver();// 选择器driver.findElement(By.cssSelector("#s-hotsearch-wrapper > div > a.hot-title > div"));// xpathdriver.findElement(By.xpath("//*[@id=\"s-hotsearch-wrapper\"]/div/a[1]/div"));// 查找多个热搜词条List<WebElement> list = driver.findElements(By.cssSelector("#hotsearch-content-wrapper > li > a > span.title-content-title"));for (WebElement x:list) {System.out.println(x.getText());}driver.quit();}public static void main(String[] args) throws InterruptedException {FirstAutoTest test = new FirstAutoTest();test.test02();}
}
运行结果
2.操作测试对象
获取到了元素,我们便可以对元素进行操作了。
常见的操作有点击、提交、输入、清除、获取文本。
2.1点击/提交对象-click()
click()
//找到百度⼀下按钮并点击
driver.findElement(By.cssSelector("#su")).click();
注意: 页面隐藏的标签,不可见的标签不能点击。
2.2 模拟按键输入-sendKeys(“”)
sendKeys("")
// 在百度搜索框中输入“CSDN”
driver.findElement(By.cssSelector("#kw")).sendKeys("CSDN");
2.3 清除文本内容-clear()
clear()
driver.findElement(By.cssSelector("#kw")).sendKeys("玩游戏");
driver.findElement(By.cssSelector("#kw")).clear(); // 清空搜索框
driver.findElement(By.cssSelector("#kw")).sendKeys("写作业");
2.4 获取文本信息-getText()
getText()
// 查找并打印热搜词条List<WebElement> list = driver.findElements(By.cssSelector("#hotsearch-content-wrapper > li > a > span.title-content-title"));for (WebElement x : list) {System.out.println(x.getText());}
注意:文本和属性值不要混淆了。获取属性值需要使用方法 getAttribute(“属性名称”) ;
2.5 获取当前页面标题-getTitle()
getTitle()
2.6获取当前页面URL-getCurrentUrl()
getCurrentUrl()
举例
String title = driver.getTitle();
String currentUrl = driver.getCurrentUrl();
System.out.println("title = " + title);
System.out.println("Url = " + currentUrl);
3.窗口
我们可以通过眼睛来判断当前的窗口是哪一个。对于程序而言,每个浏览器窗口的唯⼀的属性==句柄(handle)==就是它的眼睛。我们可以通过句柄来切换窗口。
3.1 切换窗口
1)获取当前页面句柄:
driver.getWindowHandle();
3)获取所有页面句柄:
driver.getWindowHandles();
3)切换当前句柄为最新页面
String curWindow = driver.getWindowHandle();
Set<String> allWindow = driver.getWindowHandles();
for( String w : allWindow){if(w!=curWindow){driver.switchTo().window(w);}
}
3.2 窗口设置大小
//窗⼝最⼤化
driver.manage().window().maximize();
//窗⼝最⼩化
driver.manage().window().minimize();
//全屏窗⼝
driver.manage().window().fullscreen();
//⼿动设置窗⼝⼤⼩
driver.manage().window().setSize(new Dimension(1024, 768));
3.3 屏幕截图
如果程序出现了报错,我们可以通过抓拍来记录当时的错误场景。
要使用FileUtils类,先在pom.xml中额外导入包
<dependency><groupId>commons-io</groupId><artifactId>commons-io</artifactId><version>2.6</version></dependency>
- 低级屏幕截图:
File file = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(file, new File("test.png"));
2. 高级屏幕截图
将图片保存到:./src/test/image/年月日/方法名-时分秒毫秒.png
public class FirstAutoTest {WebDriver driver = null;void createDriver() {// 1.打开谷歌浏览器WebDriverManager.chromedriver().setup();// 浏览器配置:允许访问所有链接ChromeOptions options = new ChromeOptions();options.addArguments("--remote-allow-origins=*");driver = new ChromeDriver(options);// 2.输入网址:https://www.baidu.comdriver.get("https://www.baidu.com");}// 高级屏幕截图void getScreenShot(String str) throws IOException {// 截屏时间SimpleDateFormat sim1 = new SimpleDateFormat("yyyy-MM-dd");SimpleDateFormat sim2 = new SimpleDateFormat("HHmmssSS"); // 精确到毫秒String dirTime = sim1.format(System.currentTimeMillis());String fileTime = sim2.format(System.currentTimeMillis());// ./src/test/image/年月日/方法名-时分秒毫秒.pngString fileName = "./src/test/image/" + dirTime + "/" + str + "-" + fileTime + ".png";System.out.println("fileName: " + fileName);File file = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);FileUtils.copyFile(file, new File(fileName));}void test05() throws IOException {createDriver();getScreenShot(getClass().getName());getScreenShot(getClass().getName());}public static void main(String[] args) throws InterruptedException, IOException {FirstAutoTest test = new FirstAutoTest();test.test05();}
}
结果
3.4关闭窗口
driver.close(); // 关闭当前的标签页
// 窗⼝关闭后driver要重新定义