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

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

边栏推荐
- speed or tempo in classical music
- The perfect car for successful people: BMW X7! Superior performance, excellent comfort and safety
- [groovy] string (string splicing | multi line string)
- Blue Bridge Cup single chip microcomputer -- PWM pulse width modulation
- Timing manager based on C #
- What is the most effective way to convert int to string- What is the most efficient way to convert an int to a String?
- How to define a unified response object gracefully
- Excuse me, my request is a condition update, but it is blocked in the buffer. In this case, can I only flush the cache every time?
- Flex flexible layout
- SQL performance optimization skills
猜你喜欢

Use of kubesphere configuration set (configmap)

Smart pointer shared_ PTR and weak_ Difference of PTR
![[positioning in JS]](/img/f1/02ce74fadc1f7524c7abca9db66c71.jpg)
[positioning in JS]

Web components series (VII) -- life cycle of custom components

Why do some programmers change careers before they are 30?

About MySQL database connection exceptions
![[untitled]](/img/53/f67c53d4ada382ec42f97cf68d9c71.jpg)
[untitled]

JWT漏洞复现

Yyds dry goods inventory embedded matrix

程序员的视力怎么样? | 每日趣闻
随机推荐
KVM virtualization
Quick start of UI component development of phantom engine [umg/slate]
[安洵杯 2019]不是文件上传
Multi person online anonymous chat room / private chat room source code / support the creation of multiple chat rooms at the same time
Cette ADB MySQL prend - elle en charge SQL Server?
Unity implements the code of the attacked white flash (including shader)
Usage scenarios and solutions of ledger sharing
Flex flexible layout
The perfect car for successful people: BMW X7! Superior performance, excellent comfort and safety
Multimedia query
Nmap使用手册学习记录
Leetcode92. reverse linked list II
speed or tempo in classical music
Easy processing of ten-year futures and stock market data -- Application of tdengine in Tongxinyuan fund
Basic function learning 02
Clean up PHP session files
Technology sharing swift defense programming
[system security] ten thousand words summary system virtualization container bottom layer principle experiment
Why do some programmers change careers before they are 30?
[untitled]