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·测试工具
起个破名想半天了4 小时前
五秒盾解决方案之Selenium
selenium·cloudflare·反爬
robinspada4 小时前
用mitmproxy替代selenium-wire
selenium·测试工具·mitmproxy·selenium-wire
我的xiaodoujiao4 小时前
从 0 到 1 搭建完整 Python 语言 Web UI自动化测试学习系列 17--测试框架Pytest基础 1--介绍使用
python·学习·测试工具·pytest
Bellafu6664 小时前
selenium的css定位方式有哪些
css·selenium·tensorflow
Bellafu6664 小时前
selenium对每种前端控件的操作,python举例
前端·python·selenium
Bellafu6665 小时前
下载selenium-ide及使用
ide·selenium·测试工具
将车2445 小时前
自动化测试脚本环境搭建
python·测试工具·自动化
天才测试猿7 小时前
WebUI自动化测试:POM设计模式全解析
自动化测试·软件测试·python·selenium·测试工具·设计模式·测试用例
郁大锤12 小时前
在 Windows 下安装与快速上手 Wireshark(抓包工具)
windows·测试工具·wireshark