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

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

边栏推荐
- Binary heap implementation (priority queue implementation)
- DMX parameter exploration of grandma2 onpc 3.1.2.5
- Basic authorization command for Curl
- Kbp206-asemi rectifier bridge kbp206
- JWT漏洞复现
- [web source code code code audit method] audit skills and tools
- 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
- v-if VS v-show 2.0
- Kubernetes - Multi cluster management
- SQL injection exercise -- sqli Labs
猜你喜欢

Redis6-01nosql database
![[software reverse analysis tool] disassembly and decompilation tool](/img/3e/d1ec8f8193c4fb2c790e7e467b4502.png)
[software reverse analysis tool] disassembly and decompilation tool

花了2晚,拿到了吴恩达@斯坦福大学的机器学习课程证书
![Yuancosmic ecological panorama [2022 latest]](/img/55/0901109e4c865b77137610b4fe0624.jpg)
Yuancosmic ecological panorama [2022 latest]
![[groovy] string (string splicing | multi line string)](/img/c7/2471d13e5d49cca3727d05b4f9fb36.jpg)
[groovy] string (string splicing | multi line string)

A brief introduction to the behavior tree of unity AI

postman和postman interceptor的安装

【软件逆向-基础知识】分析方法、汇编指令体系结构

VM in-depth learning (XXV) -class file overview
![[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
随机推荐
De debugging (set the main thread as hidden debugging to destroy the debugging Channel & debugger detection)
Some enterprise interview questions of unity interview
Share the newly released web application development framework based on blazor Technology
[web Audit - source code disclosure] obtain source code methods and use tools
Solve the problem that sqlyog does not have a schema Designer
[learning notes] month end operation -gr/ir reorganization
glibc strlen 实现方式分析
FBO and RBO disappeared in webgpu
MySQL winter vacation self-study 2022 11 (10)
Class inheritance in C #
Installation of postman and postman interceptor
程序员的视力怎么样? | 每日趣闻
001 chip test
VM in-depth learning (XXV) -class file overview
Logstash、Fluentd、Fluent Bit、Vector? How to choose the appropriate open source log collector
[punch in questions] integrated daily 5-question sharing (phase III)
LeetCode 234. Palindrome linked list
Anchor free series network yolox source code line by line explanation Part 2 (a total of 10, ensure to explain line by line, after reading, you can change the network at will, not just as a participan
[untitled]
SQL performance optimization skills