【软件测试】_使用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秒的等待;

相关推荐
螺丝钉tina3 天前
[Playwright] [02]常用命令与调试:Debug UI、Mode和 Trace Viewer
开发语言·javascript·测试工具·ui·自动化
梧桐1684 天前
14天、31个缺陷、28个测试脚本—用 Claude Code 完成第三方软件测试
人工智能·测试工具
mewowaBc5 天前
postman使用细则
测试工具·postman·偏差
光影6275 天前
Unittest单元测试基础速记
测试工具·单元测试·json·测试覆盖率
xie881018025 天前
正则里的点号不匹配换行:多行模式的开关
运维·服务器·测试工具
2601_967987377 天前
Python在自动化测试中有哪些应用?
python自动化测试·selenium·appium·pytest·性能测试
东舟技术7 天前
从人工目检到AI视觉:智能座舱瞬态故障的自动化捕获与溯源
功能测试·测试工具·自动化
小猿A8 天前
【2026最新】Apifox下载、配置、实战教程(附官网安装包)
测试工具
泽众云测试8 天前
泽众软件与广州华南检验检测中心签约,PerformanceRunner性能测试工具助力第三方测评机构性能检测能力升级
软件测试·测试工具·性能测试
@zulnger9 天前
Charles抓包工具
测试工具