2023.8.24 关于 Selenium 的简单示例

目录

[Selenium 是什么](#Selenium 是什么)

[Selenium 特点](#Selenium 特点)

[Selenium 工作原理](#Selenium 工作原理)

流程图

[使用 Selenium 实现一个简单自动化测试用例](#使用 Selenium 实现一个简单自动化测试用例)


Selenium 是什么

  • Selenium 是用来测试 Web 应用程序的功能和用户界面的 开源自动化测试工具

Selenium 特点

  • 支持各种浏览器(Chrome、Firefox、Safari),支持各种平台(Windows、Mac、Linux),支持各种语言(Python、Java、C#、JavaScript),有丰富的 API

Selenium 工作原理

流程图

使用 Selenium 实现一个简单自动化测试用例

测试用例:

示例代码:

  1. 在 pom.xml 中导入相关依赖,且将对应浏览器驱动放入 java/jdk/bin 目录下
XML 复制代码
<dependencies>
        <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>3.141.59</version>
        </dependency>
    </dependencies>

2.编写测试用例的自动化代码

java 复制代码
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

import java.util.List;
import java.util.concurrent.TimeUnit;

import static java.lang.Thread.sleep;

public class Main {
    public static void main(String[] args) throws InterruptedException{
        test01();
    }

    private static void test01() throws InterruptedException{
        int flag = 0;
        ChromeOptions options = new ChromeOptions();
//        允许所有请求
        options.addArguments("--remote-allow-origins=*");
        WebDriver webDriver = new ChromeDriver();
//        打开百度首页
        webDriver.get("https://www.baidu.com");
//        找到百度搜索输入框
//        这是通过 Css 选择器进行选择
//        WebElement element = webDriver.findElement(By.cssSelector(".s_ipt"));
//        这是通过 xpath 选择器进行选择
        WebElement element = webDriver.findElement(By.xpath("//*[@id=\"kw\"]"));
//        在搜索框中输入
        element.sendKeys("不孕不育");
//        找到百度一下按钮 并 点击
        webDriver.findElement(By.cssSelector("#su")).click();
//        设置强制等待3秒 方便观察自动化过程
        sleep(3000);
//        校验 找到搜索结果
        List<WebElement> elements = webDriver.findElements(By.cssSelector("a em"));
        for (int i = 0; i < elements.size(); i++) {
//            如果返回的结果有不孕不育,证明测试通过,否则测试不通过
            if(elements.get(i).getText().equals("不孕不育")) {
                flag = 1;
                System.out.println("测试通过");
                break;
            }
        }
        if(flag == 0){
            System.out.println("测试不通过");
        }
    }
}

运行结果:

  • 浏览器界面自动打开
  • 自动在搜索框中输入不孕不育
  • 自动点击 百度一下 按钮
  • 自动检测搜索结果是否含有 不孕不育,返回结果为 测试通过
相关推荐
测试老哥3 小时前
pytest+requests+allure自动化测试接入Jenkins学习
自动化测试·软件测试·学习·测试工具·职场和发展·jenkins·pytest
Mike_Wuzy16 小时前
【音频信号发生器】基本应用
测试工具
程序员三藏1 天前
Selenium+python自动化测试:解决无法启动IE浏览器及报错问题
自动化测试·软件测试·python·selenium·测试工具·职场和发展·测试用例
Warren981 天前
软件测试-Selenium学习笔记
java·javascript·笔记·学习·selenium·测试工具·安全
zzc9211 天前
Wireshark获取数据传输的码元速率
网络·测试工具·wifi·wireshark·路由器·802.11n·物理层参数
小丁爱养花1 天前
接口性能测试工具 - JMeter
测试工具·jmeter
甄超锋2 天前
python sqlite3模块
jvm·数据库·python·测试工具·django·sqlite·flask
中草药z2 天前
【自动化测试】Selenium详解-WebUI自动化测试
前端·功能测试·selenium·自动化·html·web·测试
zzc9213 天前
TLSv1.2协议与TCP/UDP协议传输数据内容差异
网络·测试工具·安全·wireshark·ssl·密钥·tlsv1.2
Jinkxs3 天前
告别“测试滞后”:AI实时测试工具在敏捷开发中的落地经验
人工智能·测试工具·敏捷流程