当前位置:网站首页>UI自动化测试从此告别手动下载浏览器驱动
UI自动化测试从此告别手动下载浏览器驱动
2022-07-05 03:36:00 【小码哥说测试】
感谢您抽出


“大家在做UI自动化的时候,最烦的是什么呢,是元素定位还是浏览器与驱动兼容调试?我们接着往下看
”

一.SeleniumWebDriver
SeleniumWebDriver是一个允许以编程方式控制Web浏览器的库。它提供了一个跨浏览器的API,可用于驱动使用不同编程语言(如Java、JavaScript、Python、C# 或Ruby)的浏览器(如Chrome、Edge或Firefox等)。Selenium WebDriver 的主要用途是实现Web应用程序的自动化测试。
SeleniumWebDriver使用每个浏览器的本机支持实现自动化。出于这个原因,我们需要在使用Selenium WebDriver API的测试和要控制的浏览器之间放置一个名为Driver的二进制文件。现在主流网页浏览器的驱动程序有ChromeDriver (Chrome) ,geckoDriver (Firefox) ,或者msededriver (Edge)。WebDriver API和驱动程序二进制文件之间的通信是使用名为W3C WebDriver(以前称为JSON Wire Protocol)的标准协议完成的。然后,使用每个浏览器的本机功能完成驱动程序和浏览器之间的通信。
我们根据不同浏览器以及浏览器版本去手动下载对应浏览器的相应版本的driver,设置系统属性。
System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");
System.setProperty("webdriver.gecko.driver", "/path/to/geckodriver");
System.setProperty("webdriver.edge.driver", "/path/to/msedgedriver");
System.setProperty("webdriver.opera.driver", "/path/to/operadriver");
System.setProperty("webdriver.ie.driver", "C:/path/to/IEDriverServer.exe");
我们在做UI自动化的时候经常会遇到浏览器升级导致与driver版本不匹配的问题,解决办法就是手动下载对应版本的driver,对于初学者,这并不是一个简单的工作,有没有什么办法可以根据不同浏览器自动下载对应版本的driver呢?答案肯定是有的,就是我们本次要讲的WebDriverManager。对Java和Python都支持哦。
二.WebDriverManager简介
WebDriverManager是一个开源的Java库,当然也有Python版本,它以完全自动化的方式执行Selenium WebDriver所需的驱动程序的管理(例如下载、设置和维护)。另外,在版本5中,WebDriverManager还提供了其他相关功能,比如发现安装在本地系统中的浏览器,构建WebDriver对象(如ChromeDriver、FirefoxDriver、 EdgeDriver等) ,在Docker容器中无缝运行浏览器,以及监控功能。
三.WebDriverManager实战(Java版)
1.pom.xml中添加WebDriverManager依赖
<dependency>
<groupId>io.github.bonigarcia</groupId>
<artifactId>webdrivermanager</artifactId>
<version>5.0.3</version>
<exclusions>
<exclusion>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</exclusion>
</exclusions>
</dependency>
2.新建一个测试脚本,来测试下
import io.github.bonigarcia.wdm.WebDriverManager;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
/**
* @author 作者:测试工程师成长之路
* @version 创建时间:2022/7/3
* 类说明:自动下载Chrome浏览器驱动
*/
public class OperateBrowserDriver {
public static void main(String[] args) throws InterruptedException {
WebDriverManager.chromedriver().setup();
WebDriver driver = new ChromeDriver();
driver.get("http://www.cnblogs.com/mrjade/");
Thread.sleep(1000);
driver.quit();
}
}
3.执行结果

4.driver存放路径(以Macos为例)

四.WebDriverManager实战(Python版)
1.安装
pip install webdriver-manager
2.使用
# coding=utf-8
'''
@Author : 测试工程师成长之路
@Time : 2022/7/3 12:13
@Desc : 方法描述
@Software: PyCharm
'''
import time
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
driver = webdriver.Chrome(ChromeDriverManager().install())
driver.get("https://www.baidu.com/")
time.sleep(5)
driver.quit()
3.执行结果

4.driver存放路径(以Macos为例)

四.官方文档
更多浏览器操作请参考官网文档
https://bonigarcia.dev/webdrivermanager/
学习资源分享
最后感谢每一个认真阅读我文章的人,看着粉丝一路的上涨和关注,礼尚往来总是要有的,虽然不是什么很值钱的东西,如果你用得到的话可以直接拿走

这些资料,对于想【进阶测试开发】的朋友来说应该是最全面最完整的备战仓库,这个仓库也陪伴我走过了最艰难的路程,希望也能帮助到你!凡事要趁早,特别是技术行业,一定要提升技术功底。希望对大家有所帮助…….

边栏推荐
- [2022 repair version] community scanning code into group activity code to drain the complete operation source code / connect the contract free payment interface / promote the normal binding of subordi
- Subversive cognition: what does SRE do?
- Redis6-01nosql database
- 【web審計-源碼泄露】獲取源碼方法,利用工具
- LeetCode 234. Palindrome linked list
- ABP vNext microservice architecture detailed tutorial - distributed permission framework (Part 2)
- Pdf things
- 51 independent key basic experiment
- C # use awaiter
- Some enterprise interview questions of unity interview
猜你喜欢

Basic function learning 02

The perfect car for successful people: BMW X7! Superior performance, excellent comfort and safety

Share the newly released web application development framework based on blazor Technology
![Quick start of UI component development of phantom engine [umg/slate]](/img/8b/cee092ec1ab105a7e234143bd56861.jpg)
Quick start of UI component development of phantom engine [umg/slate]

The perfect car for successful people: BMW X7! Superior performance, excellent comfort and safety

A brief introduction to the behavior tree of unity AI
![[system security] ten thousand words summary system virtualization container bottom layer principle experiment](/img/c6/1bdb29a0acb0739f67b882fa6b3b47.jpg)
[system security] ten thousand words summary system virtualization container bottom layer principle experiment

Clickhouse物化视图

Basic knowledge of tuples

grandMA2 onPC 3.1.2.5的DMX参数摸索
随机推荐
天干地支纪年法中为什么是60年一个轮回,而不是120年
[learning notes] month end operation -gr/ir reorganization
Kbp206-asemi rectifier bridge kbp206
[untitled]
Ubantu disk expansion (VMware)
Jd.com 2: how to prevent oversold in the deduction process of commodity inventory?
Redis之Jedis如何使用
Hot knowledge of multithreading (I): introduction to ThreadLocal and underlying principles
Class inheritance in C #
About authentication services (front and back, login, registration and exit, permission management)
Multimedia query
The architect started to write a HelloWorld
【web源码-代码审计方法】审计技巧及审计工具
LeetCode 237. Delete nodes in the linked list
Share the newly released web application development framework based on blazor Technology
KVM virtualization
花了2晚,拿到了吴恩达@斯坦福大学的机器学习课程证书
有個疑問 flink sql cdc 的話可以設置並行度麼, 並行度大於1會有順序問題吧?
[安洵杯 2019]不是文件上传
error Couldn‘t find a package.json file in “你的路径“