Spring Boot集成selenium实现自动化测试

news2024/9/19 13:41:10

1.什么是selenium?

Selenium 是支持web 浏览器自动化的一系列工具和 库的综合项目。 它提供了扩展来模拟用户与浏览器的交互,用于扩展浏览器分配的分发 服务器, 以及用于实现W3C WebDriver 规范 的基础结构, 该规范允许您为所有主要Web 浏览器编写可互换的代码。 Selenium 不仅仅是一个工具或 API, 它还包含许多工具.

WebDriver

如果您开始使用桌面网站测试自动化, 那么您将使用 WebDriver APIs. WebDriver 使用浏览器供应商提供的浏览器自动化 API 来控制浏览器和运行测试. 这就像真正的用户正在操作浏览器一样. 由于 WebDriver 不要求使用应用程序代码编译其 API, 因此它本质上不具有侵入性. 因此, 您测试的应用程序与实时推送的应用程序相同.

Selenium IDE

Selenium IDE (Integrated Development Environment 集成 开发环境) 是用来开发 Selenium 测试用例的工具. 这是一个易于使用的 Chrome 和 Firefox 浏览器扩展, 通常是开发测试用例最有效率的方式. 它使用现有的 Selenium 命令记录用户在浏览器中的操作, 参数由元素的上下文确定. 这不仅节省了开发时间, 而且是学习 Selenium 脚本语法的一种很好的方法.

Grid

Selenium Grid允许您在不同平台的不同机器上运行测试用例. 可以本地控制测试用例的操作, 当测试用例被触发时, 它们由远端自动执行. 当开发完WebDriver测试之后, 您可能需要在多个浏览器和操作系统的组合上运行测试. 这就是 Grid 的用途所在.

2.代码工程

实验目标

  1. 打开chrome,自动输入Google网页并进行搜索
  2. 对搜索结果截图并保存
  3. 关闭浏览器

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>springboot-demo</artifactId>
        <groupId>com.et</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>Selenium</artifactId>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <selenium.version>3.141.59</selenium.version>
        <webdrivermanager.version>4.3.1</webdrivermanager.version>
        <testng.version>7.4.0</testng.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>${selenium.version}</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/io.github.bonigarcia/webdrivermanager -->
        <!--            https://github.com/bonigarcia/webdrivermanager-->
        <dependency>
            <groupId>io.github.bonigarcia</groupId>
            <artifactId>webdrivermanager</artifactId>
            <version>${webdrivermanager.version}</version>
        </dependency>


        <!-- https://mvnrepository.com/artifact/org.testng/testng -->
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>${testng.version}</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

测试主类

package com.et.selenium;

import com.et.selenium.page.google.GooglePage;
import com.et.selenium.util.ScreenShotUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.testng.Assert;
import org.testng.annotations.Test;

import java.io.IOException;

public class GoogleSearch1Test extends SpringBaseTestNGTest {

    @Autowired
    private GooglePage googlePage;

    @Lazy // only create the object when needed
    @Autowired
    private ScreenShotUtil screenShotUtil;

    @Test
    public void GoogleTest() throws IOException, InterruptedException {
        this.googlePage.goToGooglePage();
        Assert.assertTrue(this.googlePage.isAt());

        this.googlePage.getSearchComponent().search("spring boot");
        Assert.assertTrue(this.googlePage.getSearchResult().isAt());
        Assert.assertTrue(this.googlePage.getSearchResult().getCount() > 2);
        System.out.println("Number of Results: " + this.googlePage.getSearchResult().getCount());
        // wait 3 seconds
         Thread.sleep(3000);
        //take screenshot
        this.screenShotUtil.takeScreenShot("Test.png");
        this.googlePage.close();
    }
}

打开google网页

package com.et.selenium.page.google;


import com.et.selenium.annotation.Page;
import com.et.selenium.page.Base;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

// this is the main page class that uses search componet and search results componet
@Page // using custom annotation created; src/main/java/com/demo/seleniumspring/annotation/Page.java
public class GooglePage extends Base {

    @Autowired
    private SearchComponent searchComponent;

    @Autowired
    private SearchResult searchResult;

    @Value("${application.url}")
    private String url;

    //launch website
    public void goToGooglePage(){
        this.driver.get(url);
    }

    public SearchComponent getSearchComponent() {
        return searchComponent;
    }

    public SearchResult getSearchResult() {
        return searchResult;
    }

    @Override
    public boolean isAt() {
        return this.searchComponent.isAt();
    }

    public void close(){
        this.driver.quit();
    }
}

搜索“ Spring Boot”关键字

package com.et.selenium.page.google;


import com.et.selenium.annotation.PageFragment;
import com.et.selenium.page.Base;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;

import java.util.List;

@PageFragment// using custom annotation created; src/main/java/com/demo/seleniumspring/annotation/PageFragment.java
public class SearchComponent extends Base {

    @FindBy(name = "q")
    private WebElement searchBox;

    @FindBy(name="btnK")
    private List<WebElement> searchBtns;

    public void search(final String keyword) {
        this.searchBox.sendKeys(keyword);
        this.searchBox.sendKeys(Keys.TAB);
        // CLICK first search button
        this.searchBtns
                .stream()
                .filter(e -> e.isDisplayed() && e.isEnabled())
                .findFirst()
                .ifPresent(WebElement::click);
    }

    @Override
    public boolean isAt() {
        return this.wait.until(driver1 -> this.searchBox.isDisplayed());
    }
}

搜索结果截图

package com.et.selenium.util;

import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;
import org.springframework.util.FileCopyUtils;

import java.io.File;
import java.io.IOException;
import java.nio.file.Path;

@Lazy
@Component
public class ScreenShotUtil {

    @Autowired
    private TakesScreenshot driver;

    // location of screenshot file
    @Value("${screenshot.path}")
    private String path;

    public void takeScreenShot(final String imgName) throws IOException {
        // takes screenshot as saves to path in app properties file using given imgName ex. test.png
        if (System.getenv("CLOUD_RUN_FLAG") == null) {
            try {
                File sourceFile = this.driver.getScreenshotAs(OutputType.FILE);
                File  targetfile = new File(path+"/"+imgName);
                FileCopyUtils.copy(sourceFile, targetfile);
                System.out.println("Saving screenshot to " + path);
            } catch (Exception e) {
                System.out.println("Something went wrong with screenshot capture" + e);
            }
        }


    }
}

关闭chrome浏览器

this.googlePage.close();

以上只是一些关键代码,所有代码请参见下面代码仓 库

代码仓库

  • GitHub - Harries/springboot-demo: a simple springboot demo with some components for example: redis,solr,rockmq and so on.(selenium)

3.测试

启动测试方法GoogleTest,效果如下面动图

selenium

4.引用

  • Spring Boot集成selenium实现自动化测试 | Harries Blog™
  • Selenium

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2044214.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

全国各地认可再+4,美创入选ZJCERT等多省市网络数据安全支撑单位

近一个月以来&#xff0c;美创科技连获多省市认可&#xff0c;相继入选&#xff1a; ZJCERT网络安全应急服务支撑单位 杭州市委网信办网络安全技术服务单位 南通市网络和数据安全技术支撑单位 济南市卫生健康系统网络和数据安全应急技术支撑单位 ZJCERT第三届网络安全应急服…

力扣3148. 矩阵中的最大得分

题目 给你一个由 正整数 组成、大小为 m x n 的矩阵 grid。你可以从矩阵中的任一单元格移动到另一个位于正下方或正右侧的任意单元格&#xff08;不必相邻&#xff09;。从值为 c1 的单元格移动到值为 c2 的单元格的得分为 c2 - c1 。 你可以从 任一 单元格开始&#xff0c;并…

Ubuntu+QT编译QTXlsx库

1.在GitHub上下载QT Xlsx 的源码&#xff0c;网站链接如下&#xff08;需要科学上网&#xff09; https://github.com/dbzhang800/QtXlsxWriter 下载好的内容如下 然后在目录下右击启动终端 输入如下命令 先输入qmake qtxlsx.pro再输入make最后sudo make install 注意&…

医药企业如何选择数字化营销模式

有产品&#xff0c;有市场&#xff0c;便有了窜货这一现象&#xff0c;经销商之间窜货不仅伤害了生产企业的渠道和价格体系&#xff0c;还影响企业的形象&#xff0c;降低了企业品牌的价值。而这一问题的根源就是企业对产品的营销管理信息不对称&#xff0c;而数字化营销被视为…

【常见算法题】斐波那契数列(矩阵快速幂)

一、题目描述 大家都知道斐波那契数列&#xff0c;现在要求输入一个正整数 n &#xff0c;请你输出斐波那契数列的第 n 项。 斐波那契数列满足如下 二、解题思路 2.1 普通处理方式 使用递归直接计算 int fib(int n) {if (n 1 || n 2) return 1;return fib(n - 1) fib(n…

实现信创Linux麦克风摄像头录制(源码,银河麒麟、统信UOS)

随着信创国产化浪潮的来临&#xff0c;在国产操作系统上的应用开发的需求越来越多&#xff0c;其中一个就是需要在银河麒麟或统信UOS上实现录制摄像头视频和麦克风声音&#xff0c;将它们录制成一个mp4文件。那么这个要如何实现了&#xff1f; 一. 技术方案 要完成这些功能&a…

北大研究生公选课资料现已公开,数据库学习秘籍速来get!

为促进基础软件在中国高校的传播&#xff0c;进一步提高在校研究生对基础软件的学习和开发实践能力&#xff0c;拓数派与开源联盟 PG 分会携手合作&#xff0c;走进北京大学&#xff0c;进行了北大软件与微电子学院 2024 年《北京大学 PostgreSQL 内核开发&#xff1a;从入门到…

构建高效沃尔玛自养号测评系统:技术策略与实战指南

搭建沃尔玛自养号测评技术系统是一个涉及多方面技术和资源投入的过程&#xff0c;旨在通过自行构建和掌控测评环境&#xff0c;利用真实国外买家的信息和资料来创建买家账号&#xff0c;模拟真实的购买和评价过程&#xff0c;从而提升商品权重和销量。以下是搭建该系统的主要步…

mysql Ubuntu安装与远程连接配置

一、安装&#xff08;Ubuntu22环境安装mysql8&#xff09; 这里使用Xshell链接Ubuntu和mysql windows进行操作&#xff0c;特别提醒&#xff1a;安装之前建议对Ubuntu快照处理备份&#xff0c;避免安装中出错导致Ubuntu崩溃。 查看是否安装的有可以用指令&#xff1a;ps -ef|…

IOS 05 OC和Swift混合编程

为什么需要使用OC和Swift混合编程&#xff1f; 在真实项目开发过程中&#xff0c;大部分时候我们往往都会使用到OC和Swift混合编程&#xff0c;主要原因如下&#xff1a; 老项目是OC语言实现的&#xff0c;但需要引用Swift的框架&#xff1b;新项目是Swift实现的&#xff0c;…

【操作系统】二、进程管理:1.进程与线程(程序、进程(PCB、状态转换、原语、进程间通信)、线程(多线程模型))

二、进程与线程 文章目录 二、进程与线程1.程序1.1顺序执行的特征1.2并发执行的特征 2.进程Process2.1定义&#xff08;组织&#xff09;2.1.1程序段2.1.2数据段2.1.3进程控制块PCB1&#xff09;内容2&#xff09;作用3&#xff09;进程组织方式 2.2特征2.3进程的状态与转换2.3…

云服务器是什么?云服务器可以用来干什么?

云服务器&#xff0c;顾名思义&#xff0c;是指运行在云计算环境中的虚拟服务器。与传统的物理服务器相比&#xff0c;云服务器不需要用户自行购买、搭建和维护硬件设备&#xff0c;而是通过互联网从云服务提供商处获取计算资源、存储空间和网络服务。用户可以根据自己的需求&a…

spring揭秘05-ApplicationContext

文章目录 【README】【1】ApplicationContext概述【1.1】spring通过Resource对文件抽象【1.2】统一资源加载策略-ResourceLoader【1.2.1】 DefaultResourceLoader【1.2.2】FileSystemResourceLoader【1.2.3】 ResourcePatternResolver批量加载资源【1.2.4】Resource与ResourceL…

使用住宅代理抓取奥运奖牌新闻,全面掌握赛事精彩瞬间

引言 什么是新闻抓取&#xff1f;目的是什么&#xff1f; 新闻抓取有哪些好处&#xff1f; 为什么需要关注奥运奖牌新闻&#xff1f; 如何进行新闻抓取——以Google 新闻为例 总结 引言 近日&#xff0c;巴黎奥运会圆满落幕&#xff0c;在这16天中&#xff0c;全球顶尖运…

一问讲透什么是 RAG,为什么需要 RAG?

一. 为什么要用 RAG &#xff1f; 如果使用 pretrain 好的 LLM 模型&#xff0c;应用在你个人的情境中&#xff0c;势必会有些词不达意的地方&#xff0c;例如问 LLM 你个人的信息&#xff0c;那么它会无法回答;这种情况在企业内部也是一样&#xff0c;例如使用 LLM 来回答企业…

VTK—vtkRectilinearGrid学习

vtkRectilinearGrid理解为沿着坐标轴方向一系列规格的网格&#xff0c;但是网格间距可以不同。需要显式的提供各坐标轴的点数据。单元数据不用指定&#xff0c;会隐式生成。与前面提到的vtkStructuredGrid 类似&#xff0c;但是每个网格线都是直的。 1.给三个坐标分配点&#…

Golang基于DTM的分布式事务SAGA实战

SAGA介绍 SAGA是“长时间事务”运作效率的方法&#xff0c;大致思路是把一个大事务分解为可以交错运行的一系列子事务的集合。原本提出 SAGA 的目的&#xff0c;是为了避免大事务长时间锁定数据库的资源&#xff0c;后来才逐渐发展成将一个分布式环境中的大事务&#xff0c;分…

关于tresos Studio(EB)的MCAL配置之DIO

General Dio Development Error Detect开发者错误检测 Dio Flip Channel Api翻转通道电平接口Dio_FlipChannel是否启用 Dio Version Info Api决定Dio_GetVersionInfo接口是否启用&#xff0c;一般打开就行。 Dio Reverse Port Bits让端口的位&#xff08;通道&#xff09;进…

最新号卡推广单页源码/仿制手机卡流量卡号卡代理推广源码/简洁实用/带弹窗公告+后台管理

源码简介&#xff1a; 最新号卡推广单页源码&#xff0c;它是手机卡流量卡号卡代理推广源码量身打造的&#xff0c;不仅设计得简洁实用&#xff0c;而且还有炫酷的弹窗公告功能和强大的后台管理系统哦&#xff01; 一款号卡推广单页源码&#xff0c;自己仿制来的&#xff0c;…

arcgis-坡度坡向分析

坡向的描述有定性和定量两种方式&#xff0c;定量是以东为0&#xff0c;顺时针递增&#xff0c;南为90&#xff0c;西为180&#xff0c;北为270等&#xff0c;范围在0&#xff5e;35959′59″之间。 定性描述有8方向法和4方向法. 8 方向为东、东南、南、西南、西、西北、北、东…