当前位置:网站首页>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/
学习资源分享
最后感谢每一个认真阅读我文章的人,看着粉丝一路的上涨和关注,礼尚往来总是要有的,虽然不是什么很值钱的东西,如果你用得到的话可以直接拿走

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

边栏推荐
- [groovy] string (string type variable definition | character type variable definition)
- 【PHP特性-变量覆盖】函数的使用不当、配置不当、代码逻辑漏洞
- IPv6 experiment
- The architect started to write a HelloWorld
- grandMA2 onPC 3.1.2.5的DMX参数摸索
- Accuracy problem and solution of BigDecimal
- Redis之Jedis如何使用
- [wp]bmzclub几道题的writeup
- LeetCode 234. Palindrome linked list
- How to define a unified response object gracefully
猜你喜欢

Some enterprise interview questions of unity interview

Blue Bridge Cup single chip microcomputer -- PWM pulse width modulation

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

Subversive cognition: what does SRE do?
![[untitled]](/img/53/f67c53d4ada382ec42f97cf68d9c71.jpg)
[untitled]

De debugging (set the main thread as hidden debugging to destroy the debugging Channel & debugger detection)

Learning notes of raspberry pie 4B - IO communication (I2C)

深度学习——LSTM基础

Mongodb common commands

New interesting test applet source code_ Test available
随机推荐
Anchor free series network yolox source code line by line explanation four (a total of ten, ensure line by line explanation, after reading, you can change the network at will, not just as a participan
Assembly - getting started
深度学习——LSTM基础
[105] Baidu brain map - Online mind mapping tool
Basic function learning 02
Timing manager based on C #
Ask, does this ADB MySQL support sqlserver?
speed or tempo in classical music
特殊版:SpreadJS v15.1 VS SpreadJS v15.0
[software reverse analysis tool] disassembly and decompilation tool
Daily question 2 12
Why do some programmers change careers before they are 30?
Yuancosmic ecological panorama [2022 latest]
The perfect car for successful people: BMW X7! Superior performance, excellent comfort and safety
Easy processing of ten-year futures and stock market data -- Application of tdengine in Tongxinyuan fund
FBO and RBO disappeared in webgpu
Leetcode42. connect rainwater
The perfect car for successful people: BMW X7! Superior performance, excellent comfort and safety
[groovy] string (string splicing | multi line string)
Learning notes of raspberry pie 4B - IO communication (I2C)