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("测试不通过");
        }
    }
}

运行结果:

  • 浏览器界面自动打开
  • 自动在搜索框中输入不孕不育
  • 自动点击 百度一下 按钮
  • 自动检测搜索结果是否含有 不孕不育,返回结果为 测试通过
相关推荐
巴里巴气1 小时前
selenium基础知识 和 模拟登录selenium版本
爬虫·python·selenium·爬虫模拟登录
2501_924064113 小时前
2025年跨端云真机测试平台深度测评:XR与折叠屏时代的兼容性之战
测试工具·移动端自动化测试·自动化测试脚本
Small black human12 小时前
HTTP-Postman的安装及其使用
测试工具·postman
AIZHINAN16 小时前
Appium 简介
自动化测试·测试工具·appium
吴free1 天前
mac电脑wireshark快速实现http接口抓包
网络·测试工具·http·wireshark
DeamoTech1 天前
ESCADA
物联网·测试工具
旷世奇才李先生2 天前
Selenium 安装使用教程
selenium·测试工具
巴里巴气2 天前
对selenium进行浏览器和驱动进行配置Windows | Linux
selenium·测试工具
q567315233 天前
Java Selenium反爬虫技术方案
java·爬虫·selenium
有趣的我3 天前
wireshark介绍和使用
网络·测试工具·wireshark