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

相关推荐
巴里巴气18 小时前
selenium基础知识 和 模拟登录selenium版本
爬虫·python·selenium·爬虫模拟登录
2501_9240641121 小时前
2025年跨端云真机测试平台深度测评:XR与折叠屏时代的兼容性之战
测试工具·移动端自动化测试·自动化测试脚本
Small black human1 天前
HTTP-Postman的安装及其使用
测试工具·postman
AIZHINAN1 天前
Appium 简介
自动化测试·测试工具·appium
吴free2 天前
mac电脑wireshark快速实现http接口抓包
网络·测试工具·http·wireshark
DeamoTech2 天前
ESCADA
物联网·测试工具
旷世奇才李先生2 天前
Selenium 安装使用教程
selenium·测试工具
巴里巴气3 天前
对selenium进行浏览器和驱动进行配置Windows | Linux
selenium·测试工具
q567315233 天前
Java Selenium反爬虫技术方案
java·爬虫·selenium
有趣的我4 天前
wireshark介绍和使用
网络·测试工具·wireshark