【软件测试】_使用selenium进行自动化测试示例

目录

[1. 导入依赖](#1. 导入依赖)

[2. 使用selenium编写测试代码](#2. 使用selenium编写测试代码)

[3. 运行结果](#3. 运行结果)

[4. 关于浏览器驱动管理及浏览器驱动配置](#4. 关于浏览器驱动管理及浏览器驱动配置)


创建一个空项目用于进行selenium的自动化测试。

1. 导入依赖

XML 复制代码
    <dependencies>
        <!-- https://mvnrepository.com/artifact/io.github.bonigarcia/webdrivermanager -->
        <dependency>
            <groupId>io.github.bonigarcia</groupId>
            <artifactId>webdrivermanager</artifactId>
            <version>5.8.0</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>4.0.0</version>
        </dependency>
    </dependencies>

其中包含两个依赖:

(1)驱动管理:webdrivermanager;

(2) selenium库:selenium-java;

2. 使用selenium编写测试代码

在test包下的java包下创建测试类:FirstTest和含main方法的测试类调用类:runCase:

FirstTest类:

java 复制代码
import io.github.bonigarcia.wdm.WebDriverManager;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

public class FirstTest {
    // 测试百度搜索关键词:spring官网
    void test01() throws InterruptedException {
        // 1. 使用驱动打开浏览器
        WebDriverManager.chromedriver().setup();

        // 增加浏览器配置:创建驱动对象时强制指定允许访问所有链接
        ChromeOptions options=new ChromeOptions();
        options.addArguments("--remote-allow-origins=*");

        // 创建Chrome的驱动对象
        WebDriver driver = new ChromeDriver(options);

        // 2. 输入网址:https://www.baidu.com
        driver.get("https://www.baidu.com");
        Thread.sleep(1000);

        // 3. 找到输入框输入关键词
        driver.findElement(By.cssSelector("#kw")).sendKeys("spring官网");
        Thread.sleep(1000);
        // 4. 找到百度一下按钮并点击
        driver.findElement(By.cssSelector("#su")).click();
        Thread.sleep(3000);
        // 5. 关闭浏览器
        driver.quit();
    }
}

runCase类:

java 复制代码
public class runCase {
    public static void main(String[] args) throws InterruptedException {
        FirstTest test=new FirstTest();
        test.test01();
    }
}

3. 运行结果

4. 关于浏览器驱动管理及浏览器驱动配置

1、浏览器驱动管理程序与浏览器驱动

由于需要使用不同的浏览器驱动才能打开对应的浏览器,且需使用不同版本的浏览器驱动才能打开对应版本的浏览器。

为避免由于浏览器多版本而导致对应驱动多版本对测试造成的麻烦及困难,比如在浏览器更新而驱动尚未更新的时间差中,自动化运行就会报错,从而导致自动化的误报率。

为了降低由于浏览器版本的自动升级而导致的自动化误报率,可使用驱动管理进行对应驱动的正确下载;

在上文中:

(1)WebDriverManager就是一个驱动管理程序,它可以打开多个浏览器驱动:

WebDriverManager.chromedriver().setup()就是使用驱动管理程序打开Chrome的浏览器驱动;

(2)创建目标浏览器的驱动对象,本例采用Chrome浏览器,故使用WebDriver类创建一个ChromeDriver对象表示Chrome驱动对象。

2、浏览器配置问题

对于selenium实现自动化测试存在有些外部链接不可访问的问题,因此需要增加浏览器配置使得在创建浏览器驱动对象时强制指定允许访问所有的链接:

具体通过ChromeOptions类创建一个对象,调用addArguments方法实现:

令其参数为--remote-allow-origins=*,表示允许访问所有链接。

在创建驱动对象(WebDriver类)时,将ChromeOptions类创建的对象作为参数进行传递。

3、 其他细节:

(1)调用浏览器驱动对象的get方法时,传递的url参数必须是完整的url;

(2)由于代码执行较快,为了便于观察,采用sleep实现3秒的等待;

相关推荐
流㶡6 分钟前
Python爬虫:POST与Selenium
爬虫·python·selenium
写点什么呢32 分钟前
Ltspice_安装与使用
学习·测试工具
介一安全4 小时前
OSS-Fuzz 模糊测试使用指南
测试工具·安全性测试·fuzz
0思必得014 小时前
[Web自动化] Selenium处理动态网页
前端·爬虫·python·selenium·自动化
llilian_161 天前
信号发生器 多通道多功能脉冲信号发生器应用解决方案 多功能脉冲发生器
功能测试·单片机·嵌入式硬件·测试工具
程序员小远1 天前
使用Postman进行一次完整的接口测试
自动化测试·软件测试·python·测试工具·测试用例·接口测试·postman
我的xiaodoujiao1 天前
使用 Python 语言 从 0 到 1 搭建完整 Web UI自动化测试学习系列 44--将自动化测试结果自动推送至钉钉工作群聊
前端·python·测试工具·ui·pytest
生活很暖很治愈1 天前
GUI自动化测试[3]——控件&数鼠标操作
windows·python·功能测试·测试工具
我的xiaodoujiao1 天前
使用 Python 语言 从 0 到 1 搭建完整 Web UI自动化测试学习系列 45--生成项目需要的requirements.txt依赖文件
python·学习·测试工具·pytest
uyeonashi1 天前
Boost搜索引擎测试报告
功能测试·selenium·搜索引擎