最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringBoot集成selenium實(shí)現(xiàn)自動(dòng)化測(cè)試的代碼工程

 更新時(shí)間:2024年08月16日 08:27:21   作者:HBLOG  
Selenium?是支持web?瀏覽器自動(dòng)化的一系列工具和[庫]?它提供了擴(kuò)展來模擬用戶與瀏覽器的交互,用于擴(kuò)展瀏覽器分配的分發(fā),本文給大家介紹了SpringBoot集成selenium實(shí)現(xiàn)自動(dòng)化測(cè)試的代碼工程,需要的朋友可以參考下

1.什么是selenium?

Selenium 是支持web 瀏覽器自動(dòng)化的一系列工具和[庫] 它提供了擴(kuò)展來模擬用戶與瀏覽器的交互,用于擴(kuò)展瀏覽器分配的分發(fā)[服務(wù)器] 以及用于實(shí)現(xiàn)W3C WebDriver 規(guī)范 的基礎(chǔ)結(jié)構(gòu), 該規(guī)范允許您為所有主要Web 瀏覽器編寫可互換的代碼。 Selenium 不僅僅是一個(gè)工具或 API, 它還包含許多工具.

WebDriver

如果您開始使用桌面網(wǎng)站測(cè)試自動(dòng)化, 那么您將使用 WebDriver APIs. WebDriver 使用瀏覽器供應(yīng)商提供的瀏覽器自動(dòng)化 API 來控制瀏覽器和運(yùn)行測(cè)試. 這就像真正的用戶正在操作瀏覽器一樣. 由于 WebDriver 不要求使用應(yīng)用程序代碼編譯其 API, 因此它本質(zhì)上不具有侵入性. 因此, 您測(cè)試的應(yīng)用程序與實(shí)時(shí)推送的應(yīng)用程序相同.

Selenium IDE

Selenium IDE (Integrated Development Environment 集成[開發(fā)]是用來開發(fā) Selenium 測(cè)試用例的工具. 這是一個(gè)易于使用的 Chrome 和 Firefox 瀏覽器擴(kuò)展, 通常是開發(fā)測(cè)試用例最有效率的方式. 它使用現(xiàn)有的 Selenium 命令記錄用戶在瀏覽器中的操作, 參數(shù)由元素的上下文確定. 這不僅節(jié)省了開發(fā)時(shí)間, 而且是學(xué)習(xí) Selenium 腳本語法的一種很好的方法.

Grid

Selenium Grid允許您在不同平臺(tái)的不同機(jī)器上運(yùn)行測(cè)試用例. 可以本地控制測(cè)試用例的操作, 當(dāng)測(cè)試用例被觸發(fā)時(shí), 它們由遠(yuǎn)端自動(dòng)執(zhí)行. 當(dāng)開發(fā)完WebDriver測(cè)試之后, 您可能需要在多個(gè)瀏覽器和操作系統(tǒng)的組合上運(yùn)行測(cè)試. 這就是 Grid 的用途所在.

2.代碼工程

實(shí)驗(yàn)?zāi)繕?biāo)

  • 打開chrome,自動(dòng)輸入Google網(wǎng)頁并進(jìn)行搜索
  • 對(duì)搜索結(jié)果截圖并保存
  • 關(guān)閉瀏覽器

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>

測(cè)試主類

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網(wǎng)頁

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”關(guān)鍵字

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

搜索結(jié)果截圖

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


    }
}

關(guān)閉chrome瀏覽器

this.googlePage.close();

以上只是一些關(guān)鍵代碼,所有代碼請(qǐng)參見下面代碼倉庫

代碼倉庫

3.測(cè)試

啟動(dòng)測(cè)試方法GoogleTest,效果如下面動(dòng)圖

以上就是SpringBoot集成selenium實(shí)現(xiàn)自動(dòng)化測(cè)試的代碼工程的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot selenium自動(dòng)化測(cè)試的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • maven項(xiàng)目無法讀取到resource文件夾的問題

    maven項(xiàng)目無法讀取到resource文件夾的問題

    這篇文章主要介紹了maven項(xiàng)目無法讀取到resource文件夾的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-11-11
  • Java之Spring Boot創(chuàng)建和使用

    Java之Spring Boot創(chuàng)建和使用

    Spring 的誕生就是為了簡化 Java 程序的開發(fā)的.Spring Boot 的誕生就是為了簡化 Spring 程序開發(fā)的,對(duì)Springboot感興趣的同學(xué)可以借鑒本文
    2023-04-04
  • Jmeter中的timeshift()函數(shù)獲取當(dāng)前時(shí)間進(jìn)行加減

    Jmeter中的timeshift()函數(shù)獲取當(dāng)前時(shí)間進(jìn)行加減

    這篇文章主要介紹了Jmeter中的timeshift()函數(shù)獲取當(dāng)前時(shí)間進(jìn)行加減,TimeShift(格式,日期,移位,語言環(huán)境,變量)可對(duì)日期進(jìn)行移位加減操作,本文給大家詳細(xì)講解,對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-10-10
  • JAVA String.valueOf()方法的用法說明

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

    這篇文章主要介紹了JAVA String.valueOf()方法的用法說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-09-09
  • 利用Intellij Idea連接遠(yuǎn)程服務(wù)器實(shí)現(xiàn)遠(yuǎn)程上傳部署功能

    利用Intellij Idea連接遠(yuǎn)程服務(wù)器實(shí)現(xiàn)遠(yuǎn)程上傳部署功能

    大家在使用Intellij Idea開發(fā)程序的時(shí)候,是不是需要部署到遠(yuǎn)程SSH服務(wù)器運(yùn)行呢,當(dāng)然也可以直接在idea軟件內(nèi)容實(shí)現(xiàn)配置部署操作,接下來通過本文給大家分享利用Intellij Idea連接遠(yuǎn)程服務(wù)器實(shí)現(xiàn)遠(yuǎn)程上傳部署功能,感興趣的朋友跟隨小編一起看看吧
    2021-05-05
  • Java編程中的4種代碼塊詳解

    Java編程中的4種代碼塊詳解

    在本篇內(nèi)容里小編個(gè)總結(jié)了Java編程中的4種代碼塊相關(guān)的知識(shí)點(diǎn),有興趣的朋友們可以學(xué)習(xí)下。
    2021-06-06
  • springboot+spring?data?jpa實(shí)現(xiàn)新增及批量新增方式

    springboot+spring?data?jpa實(shí)現(xiàn)新增及批量新增方式

    這篇文章主要介紹了springboot+spring?data?jpa實(shí)現(xiàn)新增及批量新增方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-11-11
  • 一文帶你搞懂Java8的LocalDateTime

    一文帶你搞懂Java8的LocalDateTime

    LocalDateTime?是Java8中新加入的日期時(shí)間類,現(xiàn)在都?Java20?了,不會(huì)還有人沒用過?LocalDateTime?吧?今天給大家演示一下?LocalDateTime?的常用方法
    2023-04-04
  • Java學(xué)習(xí)之緩沖流的原理詳解

    Java學(xué)習(xí)之緩沖流的原理詳解

    為了提高其數(shù)據(jù)的讀寫效率,Java中又定義了四種緩沖流,分別是:字節(jié)緩沖輸入流、字節(jié)緩沖輸出流、字符緩沖輸入流和字符緩沖輸出流。本文主要來和大家聊聊這些緩沖流的原理,希望對(duì)大家有所幫助
    2023-01-01
  • Java?詳解Map集合之HashMap和TreeMap

    Java?詳解Map集合之HashMap和TreeMap

    本章具體介紹了HashMap、TreeMap兩種集合的基本使用方法和區(qū)別,圖解穿插代碼實(shí)現(xiàn)。?JAVA成仙路從基礎(chǔ)開始講,后續(xù)會(huì)講到JAVA高級(jí),中間會(huì)穿插面試題和項(xiàng)目實(shí)戰(zhàn),希望能給大家?guī)韼椭?/div> 2022-03-03

最新評(píng)論

嘉祥县| 资源县| 禹城市| 东海县| 佛冈县| 西乌珠穆沁旗| 海原县| 怀安县| 西峡县| 潼南县| 历史| 永安市| 永寿县| 承德市| 共和县| 阜城县| 沧州市| 静海县| 吐鲁番市| 乐平市| 九龙坡区| 博湖县| 昭觉县| 韶关市| 渑池县| 长岛县| 石阡县| 伊川县| 荣成市| 汾西县| 错那县| 东明县| 湖州市| 和田市| 孟津县| 榆林市| 黄冈市| 孟连| 炉霍县| 比如县| 鄂托克前旗|