当前位置:网站首页>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);
优点:节省时间
缺点:写法复杂
边栏推荐
- The arm button controls the flashing of the led light (embedded button experiment report)
- 在资源管理类中提供对原始资源的访问——条款15
- 【pytorch】1.7 pytorch与numpy,tensor与array的转换
- LeetCode_733_Image rendering
- What is the difference between BI software in the domestic market?
- Graham‘s Scan法求解凸包问题
- adb shell 报错error: device unauthorized
- 第05章 存储引擎【1.MySQL架构篇】【MySQL高级】
- Anaconda如何顺利安装CV2
- 6-22 Vulnerability exploit - postgresql database password cracking
猜你喜欢
C language "the third is" upgrade (mode selection + AI chess)
你辛辛苦苦写的文章可能不是你的原创
[TypeScript]OOP
[pytorch] pytorch automatic derivation, Tensor and Autograd
.NET 20th Anniversary Interview - Zhang Shanyou: How .NET technology empowers and changes the world
Dialogue with Zhuang Biaowei: The first lesson of open source
6-22漏洞利用-postgresql数据库密码破解
外媒所言非虚,苹果降价或许是真的在清库存
Implementing distributed locks based on Redis (SETNX), case: Solving oversold orders under high concurrency
华为顶级工程师历时9年总结的“趣谈网络协议”PDF文档,太强了
随机推荐
js的toString方法
牛客 HJ20 密码验证合格程序
字符串反转的实现方法总结「建议收藏」
Flutter 获取状态栏statusbar的高度
牛客 HJ19 简单错误记录
研发过程中的文档管理与工具
6-22漏洞利用-postgresql数据库密码破解
2.索引及调优篇【mysql高级】
C language - function
单细胞测序流程(单细胞rna测序)
华为顶级工程师历时9年总结的“趣谈网络协议”PDF文档,太强了
i.MX6ULL driver development | 33 - NXP original network device driver reading (LAN8720 PHY)
tensorflow2.0 cnn(layerwise)
第二届中国PWA开发者日
Snake Project (Simple)
牛客 HJ18 识别有效的IP地址和掩码并进行分类统计
How to install CV2 smoothly in Anaconda
tooltips使用教程(鼠标悬停时显示提示)
T - sne + data visualization parts of the network parameters
上传图片-微信小程序(那些年的坑记录2022.4)