SpringBoot集成selenium实现自动化测试的代码工程

 更新时间:2024年08月16日 08:27:21   作者:HBLOG  
Selenium 是支持web 浏览器自动化的一系列工具和[库] 它提供了扩展来模拟用户与浏览器的交互,用于扩展浏览器分配的分发,本文给大家介绍了SpringBoot集成selenium实现自动化测试的代码工程,需要的朋友可以参考下

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.代码工程

实验目标

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

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();

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

代码仓库

3.测试

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

以上就是SpringBoot集成selenium实现自动化测试的代码工程的详细内容,更多关于SpringBoot selenium自动化测试的资料请关注脚本之家其它相关文章!

相关文章

  • maven项目无法读取到resource文件夹的问题

    maven项目无法读取到resource文件夹的问题

    这篇文章主要介绍了maven项目无法读取到resource文件夹的问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-11-11
  • Java之Spring Boot创建和使用

    Java之Spring Boot创建和使用

    Spring 的诞生就是为了简化 Java 程序的开发的.Spring Boot 的诞生就是为了简化 Spring 程序开发的,对Springboot感兴趣的同学可以借鉴本文
    2023-04-04
  • Jmeter中的timeshift()函数获取当前时间进行加减

    Jmeter中的timeshift()函数获取当前时间进行加减

    这篇文章主要介绍了Jmeter中的timeshift()函数获取当前时间进行加减,TimeShift(格式,日期,移位,语言环境,变量)可对日期进行移位加减操作,本文给大家详细讲解,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-10-10
  • JAVA String.valueOf()方法的用法说明

    JAVA String.valueOf()方法的用法说明

    这篇文章主要介绍了JAVA String.valueOf()方法的用法说明,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-09-09
  • 利用Intellij Idea连接远程服务器实现远程上传部署功能

    利用Intellij Idea连接远程服务器实现远程上传部署功能

    大家在使用Intellij Idea开发程序的时候,是不是需要部署到远程SSH服务器运行呢,当然也可以直接在idea软件内容实现配置部署操作,接下来通过本文给大家分享利用Intellij Idea连接远程服务器实现远程上传部署功能,感兴趣的朋友跟随小编一起看看吧
    2021-05-05
  • Java编程中的4种代码块详解

    Java编程中的4种代码块详解

    在本篇内容里小编个总结了Java编程中的4种代码块相关的知识点,有兴趣的朋友们可以学习下。
    2021-06-06
  • springboot+spring data jpa实现新增及批量新增方式

    springboot+spring data jpa实现新增及批量新增方式

    这篇文章主要介绍了springboot+spring data jpa实现新增及批量新增方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-11-11
  • 一文带你搞懂Java8的LocalDateTime

    一文带你搞懂Java8的LocalDateTime

    LocalDateTime 是Java8中新加入的日期时间类,现在都 Java20 了,不会还有人没用过 LocalDateTime 吧?今天给大家演示一下 LocalDateTime 的常用方法
    2023-04-04
  • Java学习之缓冲流的原理详解

    Java学习之缓冲流的原理详解

    为了提高其数据的读写效率,Java中又定义了四种缓冲流,分别是:字节缓冲输入流、字节缓冲输出流、字符缓冲输入流和字符缓冲输出流。本文主要来和大家聊聊这些缓冲流的原理,希望对大家有所帮助
    2023-01-01
  • Java 详解Map集合之HashMap和TreeMap

    Java 详解Map集合之HashMap和TreeMap

    本章具体介绍了HashMap、TreeMap两种集合的基本使用方法和区别,图解穿插代码实现。 JAVA成仙路从基础开始讲,后续会讲到JAVA高级,中间会穿插面试题和项目实战,希望能给大家带来帮助
    2022-03-03

最新评论