当前位置:网站首页>Automated testing - web automation - first acquaintance with selenium
Automated testing - web automation - first acquaintance with selenium
2022-07-31 16:39:00 【Xiaopaicai learns java】
一,了解自动化和selenium
1,什么是自动化测试,为什么要做自动化测试
自动化测试能够代替一部分的手工测试.自动化测试能够提高测试效率.
With the superposition of versions,The pressure to come back is also growing,Regressing versions through human testing is not realistic,因此需要借助自动化测试.
2,为什么选择selenium作为web自动化测试
开源免费,支持多浏览器,支持多系统,支持多语言(java,python等),seleniumThe bottom layer of the package consists of many availableAPI,
3,selenium环境变量
selenium工具包,Chrome浏览器,ChromeDriver谷歌驱动,java8及以上
4,什么是驱动,驱动的工作原理
驱动:通过一些操作,A certain operation is done by the driver
驱动的工作原理:
二,下载安装selenium,创建工程,Install the automated test package
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.0.0</version>
</dependency>
三,测试自动化测试
public class TestSelectOnBaidu {
public void run() throws InterruptedException {
//Create Google Drive,Used to open Google Chrome
ChromeDriver driver = new ChromeDriver();
Thread.sleep(2000);
//Enter the URL to visit
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("News hyperlinked text" + text);
xpath语法
层级:/次级,//跳级
属性:@
函数:contains() ,.....
path选择器
//xpath选择器
driver.findElement(By.xpath("//*[@id=\"kw\"]")).sendKeys("yyk");
driver.findElement(By.xpath("//*[@id=\"su\"]")).click();
submit和click
使用submit 的方法都可以使用click,能使用clickplaces may not be availablesubmit.
clear():Clears the text entered on the object.
gettext():获取文本
getAttribute():获取属性值
System.out.println(driver.findElement(By.cssSelector("#su")).getAttribute("value"));
等待:
The code executes faster than the page renders,So while getting some element text attributes of the resulting page,会出现noSuchElementException,So you need to wait for the rendering of the result page to complete.
Waiting is divided into display waiting,隐式等待,强制等待
强制等待:
优点:语法简单,适合调试使用,
缺点:Waste a lot of time waiting for it,The use of automated testing is to improve efficiency
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());
Forced wait will wait for a while before fetching the content of the result page
隐式等待:
在规定的时间内,Polling ends as soon as it waits for an element to appear,If it does not appear within the specified time,则会抛出一个No Such Element Exception异常
作用在driver的整个生命周期
//隐式等待
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(3));
//Implicit wait action againdriver 的整个生命周期
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());
隐式等待的缺点:You need to wait for all elements to be displayed before proceeding to the next step,存在时间上的浪费
Advantages of implicit waits:节省时间.
显示等待
//显示等待
//Implicit wait action againdriver 的整个生命周期
driver.findElement(By.cssSelector("#kw")).sendKeys("ykz");
driver.findElement(By.cssSelector("#su")).click();
WebDriverWait foo = new WebDriverWait(driver,Duration.ofSeconds(3));//WebDriverWait(),传入一个driver,和超时时间
//until方法,Always inquiries need to waitcssid是否存在,存在立即返回,There is no timeout to exit
String text = foo.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("#\\33 > div > div:nth-child(1) > h3 > a"))).getText();
System.out.println(text);
优点:节省时间
缺点:写法复杂
边栏推荐
- 6-22 Vulnerability exploit - postgresql database password cracking
- 关于柱状图的经典画法总结
- Flutter set the background color of the statusbar status bar and APP method (AppBar) internal consistent color.
- 牛客网刷题(一)
- What is the difference between BI software in the domestic market?
- 动态规划之线性dp(下)
- 华为顶级工程师历时9年总结的“趣谈网络协议”PDF文档,太强了
- 牛客网刷题(二)
- 动态规划之线性dp(上)
- 在资源管理类中提供对原始资源的访问——条款15
猜你喜欢
深度学习机器学习理论及应用实战-必备知识点整理分享
GP 6 overall architecture study notes
What is the difference between BI software in the domestic market?
华为顶级工程师历时9年总结的“趣谈网络协议”PDF文档,太强了
二分查找的细节坑
C language - function
利用PHP开发具有注册、登陆、文件上传、发布动态功能的网站
第05章 存储引擎【1.MySQL架构篇】【MySQL高级】
i.MX6ULL driver development | 33 - NXP original network device driver reading (LAN8720 PHY)
Huawei's top engineers lasted nine years "anecdotal stories network protocol" PDF document summary, is too strong
随机推荐
Emmet syntax
牛客网刷题(四)
ansible学习笔记02
【Meetup预告】OpenMLDB+OneFlow:链接特征工程到模型训练,加速机器学习模型开发
动态规划之线性dp(上)
牛客 HJ17 坐标移动
update data table update
基于ABP实现DDD
MySQL multi-table union query
After the form is submitted, the page does not jump [easy to understand]
牛客网刷题(三)
使用互相关进行音频对齐
牛客网刷题(二)
多主复制下处理写冲突(4)-多主复制拓扑
【愚公系列】2022年07月 Go教学课程 020-Go容器之数组
多主复制的适用场景(2)-需离线操作的客户端和协作编辑
复杂高维医学数据挖掘与疾病风险分类研究
小程序:matlab解微分方程「建议收藏」
The arm button controls the flashing of the led light (embedded button experiment report)
在资源管理类中提供对原始资源的访问——条款15