自动化测试

一.Selenium

1.安装selenium

java 复制代码
<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-java</artifactId>
    <version>4.0.0</version>
</dependency>

2.使用selenium下载浏览器驱动

以Chrome浏览器为例,可以添加隐式等待------放视元素加载过慢/没出现时自动化报错

javascript 复制代码
WebDriverManager.chromedriver().setup();
ChromeOptions options = new ChromeOptions();
options.addArguments("--remote-allow-origins=*");

3.通过driver对象来创建

打开浏览器

javascript 复制代码
WebDriver driver = new ChromeDriver(options);

输入URL

javascript 复制代码
driver.get();

二.元素定位

1.cssSelector

选择第几个------nth-child()

下一级------'>'

2.xpath

1.获取HTML所有页面------//*

2.获取页面指定的元素------//body中的标签名

3.获取子节点------/

4.元素匹配@元素名=''

5.选择第几个用\[\](1为开始)

三.选择元素

使用cssSelector

复制代码
WebElement用于单个元素接收
javascript 复制代码
driver.findElement(By.cssSelector(""));

用List<WebElement>多个元素接收的参数

javascript 复制代码
driver.findElements(By.cssSelector(""));

使用xpath

WebElement用于单个元素接收

javascript 复制代码
driver.findElement(By.xpath(""));

用List<WebElement>多个元素接收的参数

javascript 复制代码
driver.findElements(By.xpath(""));

四.使用测试对象

点击------click()

输入------senKeys(" ")

清除------clear()

获取元素文本------getText()

获取当前页面的标题------getTitle()

获取当前页面URL------getCurrentUrl()

五.窗口切换

1.获取当前页面句柄

driver.getWindowHandle()

2.获取所有⻚⾯句柄

driver.getWindowHandles()

3.切换句柄

driver.switchTo().window(窗口句柄)

六.窗口大小切换

窗口最大化

driver.manage().window().maximize()

窗口最小化

driver.manage().window().minimize()

全屏窗口

driver.manage().window().fullscreen()

自定义窗口大小

driver.manage().window().setSize(new Dimension(长,宽))

七.屏幕截图

1.添加包

javascript 复制代码
<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.6</version>
</dependency>

2.操作

javascript 复制代码
File  file=((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(file,new File("路径+文件名.格式"));

八.等待

1.强制等待

Thread.sleep()

2.隐式等待

driver.manage().timeouts().implicitlyWait(Duration.选择的时间单位());、
隐式等待作用域是整个脚本的所有元素。即只要driver对象没有被释放掉,隐式等待就⼀直⽣效

3.显示等待

javascript 复制代码
WebDriverWait foo = new WebDriverWait(driver, Duration.ofSeconds(等待时间))
foo.until(ExpectedConditions.elementToBeClickable(By.cssSelector("")));

ExpectedConditions方法
1.elementToBeClickable(By locator)------用于检查想要查找的元素是否启用
2.textToBe(Bylocator,String str)------检查元素
3.presenceOfElementLocated(Bylocator)------检查⻚⾯的 DOM 上是否存在元素
4.urlToBe(java.lang.String url)------检查当前⻚⾯的 URL 是⼀个特定的 URL

九.浏览器操作

1.打开⽹站

javascript 复制代码
driver.navigate().to("URL");
driver.get("URL");

2.前进、后退、刷新

javascript 复制代码
driver.navigate().back();后退
driver.navigate().forward();前进
driver.navigate().refresh();刷新

十.弹窗

1.切换到弹窗窗口

javascript 复制代码
Alert alert = driver.switchTo.alert()

2.确认

javascript 复制代码
alert.accept()

3.取消

javascript 复制代码
alert.dismiss()

4.输入文本消息

javascript 复制代码
alert.sendKeys("输入内容")

5.获取弹窗上的提示文本

javascript 复制代码
String getText()

十一.关闭窗口

driver.close()

相关推荐
默_笙2 天前
🍙 给每个请求过安检:FastAPI 是怎么把校验写进类型注解的
python
小羊没烦恼!2 天前
初探性能优化——2个月到4小时的性能提升
java·开发语言·windows·算法·c#
qq_426003962 天前
启动playwright录制codegen生成自动化测试脚本
python·自动化
虎头金猫2 天前
4K 视频总卡在公网带宽?用 N1 + OpenList 把网盘播放链路重新理顺
运维·服务器·网络·python·容器·beautifulsoup·pandas
长沙三为智能科技2 天前
家政小程序开发从0到上线:五阶段交付流程与验收清单
python
伞伞悦读2 天前
【第38期】Python 模块与包详解:import、from、模块搜索路径、包结构和 __init__
开发语言·python
只睡四小时2 天前
Canvas 弹道联机实战:700 行 + 固定时间步长
python·websocket·html5·游戏开发·canvas
C语言小火车2 天前
C/C++ 为什么需要编译器?
开发语言·c++
奇思妙想聪明勤奋的小羊2 天前
DeepAgents第5章:子Agent 与上下文隔离—让 Agent学会委派
人工智能·python·学习·语言模型
lpfasd1232 天前
2026年第38周GitHub趋势周报
python·科技·github