当前位置:网站首页>自动化测试—web自动化—selenium初识
自动化测试—web自动化—selenium初识
2022-07-31 16:31:00 【小趴菜学java】
一,了解自动化和selenium
1,什么是自动化测试,为什么要做自动化测试
自动化测试能够代替一部分的手工测试。自动化测试能够提高测试效率。
随着版本的叠加,版本回归的压力也越来越大,通过人力测试回归版本是不现实的,因此需要借助自动化测试。
2,为什么选择selenium作为web自动化测试
开源免费,支持多浏览器,支持多系统,支持多语言(java,python等),selenium包底层由很多可使用的API,
3,selenium环境变量
selenium工具包,Chrome浏览器,ChromeDriver谷歌驱动,java8及以上
4,什么是驱动,驱动的工作原理
驱动:通过一些操作,通过驱动程序完成某一种操作
驱动的工作原理:
二,下载安装selenium,创建工程,安装自动化测试包
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.0.0</version>
</dependency>
三,测试自动化测试
public class TestSelectOnBaidu {
public void run() throws InterruptedException {
//创建谷歌驱动,用来打开谷歌浏览器
ChromeDriver driver = new ChromeDriver();
Thread.sleep(2000);
//输入访问的网址
driver.get("https://www.baidu.com");
Thread.sleep(2000);
//找到搜索框,输入
driver.findElement(By.cssSelector("#kw")).sendKeys("王力宏");
Thread.sleep(2000);
//找到搜索按钮,点击
driver.findElement(By.cssSelector("#su")).click();
Thread.sleep(2000);
//关闭驱动
driver.quit();
}
}
四,定位元素的方法
1,
css选择器
//css选择器
String text = driver.findElement(By.cssSelector("#s-top-left > a:nth-child(1)")).getText();
System.out.println("新闻超连接的文本" + text);
xpath语法
层级:/次级,//跳级
属性:@
函数:contains() ,.....
path选择器
//xpath选择器
driver.findElement(By.xpath("//*[@id=\"kw\"]")).sendKeys("yyk");
driver.findElement(By.xpath("//*[@id=\"su\"]")).click();
submit和click
使用submit 的方法都可以使用click,能使用click的地方不一定能使用submit。
clear():清除对对象输入的文本。
gettext():获取文本
getAttribute():获取属性值
System.out.println(driver.findElement(By.cssSelector("#su")).getAttribute("value"));
等待:
代码执行速度大于页面渲染的速度,所以在获取结果页面的一些元素文本属性时,会出现noSuchElementException,所以需要等待结果页面渲染完成。
等待分为显示等待,隐式等待,强制等待
强制等待:
优点:语法简单,适合调试使用,
缺点:浪费大量时间在等待上面,而使用自动化测试就是为了提高效率
driver.findElement(By.cssSelector("#kw")).sendKeys("ykz");
driver.findElement(By.cssSelector("#su")).click();
System.out.println(driver.findElement(By.cssSelector("#\\33 > div > div:nth-child(1) > h3 > a")).getText());
//强制等待
driver.findElement(By.cssSelector("#kw")).sendKeys("ykz");
driver.findElement(By.cssSelector("#su")).click();
Thread.sleep(2000);
System.out.println(driver.findElement(By.cssSelector("#\\33 > div > div:nth-child(1) > h3 > a")).getText());
强制等待会等待一段时间后再获取结果页面的内容
隐式等待:
在规定的时间内,轮询等待元素出现后立即结束,如果规定时间内还没有出现,则会抛出一个No Such Element Exception异常
作用在driver的整个生命周期
//隐式等待
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(3));
//隐式等待作用再driver 的整个生命周期
driver.findElement(By.cssSelector("#kw")).sendKeys("ykz");
driver.findElement(By.cssSelector("#su")).click();
System.out.println(driver.findElement(By.cssSelector("#\\33 > div > div:nth-child(1) > h3 > a")).getText());
隐式等待的缺点:需要等待所有的元素都展现才会执行下一步,存在时间上的浪费
隐式等待的优点:节省时间。
显示等待
//显示等待
//隐式等待作用再driver 的整个生命周期
driver.findElement(By.cssSelector("#kw")).sendKeys("ykz");
driver.findElement(By.cssSelector("#su")).click();
WebDriverWait foo = new WebDriverWait(driver,Duration.ofSeconds(3));//WebDriverWait(),传入一个driver,和超时时间
//until方法,一直查询需要等待的cssid是否存在,存在立即返回,不存在超时退出
String text = foo.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("#\\33 > div > div:nth-child(1) > h3 > a"))).getText();
System.out.println(text);
优点:节省时间
缺点:写法复杂
边栏推荐
- Intelligent bin (9) - vibration sensor (raspberries pie pico implementation)
- 无主复制系统(1)-节点故障时写DB
- tooltips使用教程(鼠标悬停时显示提示)
- 基于Redis(SETNX)实现分布式锁,案例:解决高并发下的订单超卖,秒杀
- 牛客 HJ19 简单错误记录
- The new BMW 3 Series is on the market, with safety and comfort
- 单细胞测序流程(单细胞rna测序)
- C语言-函数
- 牛客网刷题(三)
- Implementing distributed locks based on Redis (SETNX), case: Solving oversold orders under high concurrency
猜你喜欢
[pytorch] pytorch automatic derivation, Tensor and Autograd
T - sne + data visualization parts of the network parameters
LevelSequence源码分析
i.MX6ULL驱动开发 | 33 - NXP原厂网络设备驱动浅读(LAN8720 PHY)
Kubernetes common commands
上传图片-微信小程序(那些年的坑记录2022.4)
智能垃圾桶(九)——震动传感器(树莓派pico实现)
t-sne 数据可视化网络中的部分参数+
[Meetup Preview] OpenMLDB+OneFlow: Link feature engineering to model training to accelerate machine learning model development
[TypeScript] In-depth study of TypeScript type operations
随机推荐
动态规划(一)
C language - function
华为顶级工程师历时9年总结的“趣谈网络协议”PDF文档,太强了
ML.NET相关资源整理
动态规划之线性dp(下)
Applicable scenario of multi-master replication (2) - client and collaborative editing that require offline operation
【7.29】Code Source - 【Arrangement】【Stone Game II】【Cow and Snacks】【Minimum Number of Spawns】【Sequence】
MySQL常用语句整理
【7.29】代码源 - 【排列】【石子游戏 II】【Cow and Snacks】【最小生成数】【数列】
[7.28] Code Source - [Fence Painting] [Appropriate Pairs (Data Enhanced Version)]
最新神作!阿里巴巴刚出炉的面试参考指南(泰山版),我直接狂刷29天
After Grafana is installed, the web opens and reports an error
基于C语言的编译器设计与实现
js的toString方法
adb shell 报错error: device unauthorized
jeecg主从数据库读写分离配置「建议收藏」
利用PHP开发具有注册、登陆、文件上传、发布动态功能的网站
牛客网刷题(三)
第05章 存储引擎【1.MySQL架构篇】【MySQL高级】
2022年整理LeetCode最新刷题攻略分享(附中文详细题解)