WebUI 自動化配置
前言
由于項目情況以及時間問題,本篇暫時僅用于記錄當時web項目自動化配置相關信息(歷史篇:在草稿箱存了老久了??)
一、Selenium + TestNG 配置
Java 工程的 pom 文件配置信息
<?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">
<modelVersion>4.0.0</modelVersion>
<groupId>auto-tests</groupId>
<artifactId>auto-test-punish</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
<!-- 文件拷貝時的編碼 -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<!-- 編譯時的編碼 -->
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
<aspectj.version>1.8.10</aspectj.version>
<maven.build.timestamp.format>yyyyMMddHHmmss</maven.build.timestamp.format>
</properties>
<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>
<!--testng 配置, 配置最新版本報錯 RELEASE -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.14.3</version>
</dependency>
<!-- allure 生成報告 mvn io.qameta.allure:allure-maven:serve -->
<dependency>
<groupId>io.qameta.allure</groupId>
<artifactId>allure-testng</artifactId>
<version>2.13.0</version>
</dependency>
<!-- fast json -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.62</version>
</dependency>
<!-- slf4j 日志門面,無具體實現
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.2</version>
</dependency>
-->
<!-- log4J 無法格式化日志輸出
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
</dependency>
-->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.2</version>
</dependency>
<!-- apache io 處理 -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
</dependencies>
<!-- 打包配置 mvn package assembly:single; mvn clean install 會install 到 maven 倉庫-->
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>log4j.properties</include>
</includes>
</resource>
</resources>
<plugins>
<!-- 指定maven JDK 編譯版本 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF8</encoding>
</configuration>
</plugin>
<plugin>
<!-- maven-surefire-plugin 用于 allure 查找測試用例 -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
<configuration>
<!-- 測試失敗后,是否忽略并繼續測試 -->
<testFailureIgnore>true</testFailureIgnore>
<suiteXmlFiles>
<!-- testng 配置文件名稱 src 路徑后續修改-->
<suiteXmlFile>testng.xml</suiteXmlFile>
</suiteXmlFiles>
<!--設置參數命令行 -->
<argLine>
<!-- UTF-8編碼 -->
-Dfile.encoding=UTF-8
<!-- 配置攔截器 -->
-javaagent:"${settings.localRepository}/org/aspectj/aspectjweaver/${aspectj.version}/aspectjweaver-${aspectj.version}.jar"
</argLine>
<systemPropertyVariables>
<!-- 配置 allure 結果存儲路徑, 且會覆蓋 allure 中的配置-->
<allure.results.directory>${project.build.directory}/allure-results/${maven.build.timestamp}</allure.results.directory>
</systemPropertyVariables>
</configuration>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>${aspectj.version}</version>
</dependency>
</dependencies>
</plugin>
<!-- maven-assembly-plugin 支持自定義的打包結構插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.5.5</version>
<configuration>
<archive>
<manifest>
<mainClass>com.zzw.PunishCenterTest</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
二、Allure 報告
- 簡介
官網 http://allure.qatools.ru/
文檔 https://docs.qameta.io/allure/
下載 https://github.com/allure-framework/allure2/releases
下載后配置環境變量,即可使用 allure 命令
- 配置
pom 文件 allure-testng 配置, 參考文檔 https://docs.qameta.io/allure/#_testng
<properties>
<aspectj.version>1.8.10</aspectj.version>
<!-- 與下列 allure 結果生成有關 -->
<maven.build.timestamp.format>yyyyMMddHHmmss</maven.build.timestamp.format>
</properties>
<dependencies>
<dependency>
<groupId>io.qameta.allure</groupId>
<artifactId>allure-testng</artifactId>
<version>2.13.0</version>
<!-- <scope>test</scope> 只限于 test 測試包 -->
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.20</version>
<configuration>
<argLine> -javaagent:"${settings.localRepository}/org/aspectj/aspectjweaver/${aspectj.version}/aspectjweaver-${aspectj.version}.jar"</argLine>
<systemPropertyVariables>
<!-- 配置 allure 結果存儲路徑, 且會覆蓋 allure 中的配置-->
<allure.results.directory>${project.build.directory}/allure-results/${maven.build.timestamp}</allure.results.directory>
</systemPropertyVariables>
</configuration>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>${aspectj.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
- 使用
命令行幫助文檔
格式: allure [options] [command] [command options]
Options:
--help 打印命令行幫助信息
-q, --quiet 打開靜音模式,默認為 false
-v, --verbose 打開詳細模式,默認為 false
--version 打印 allure 版本號
Commands:
generate 生成報告命令
Usage: generate [options] 生成的 allure 結果目錄
Options:
-c, --clean 在生成新的 Allure 報告目錄之前,清除該目錄. 默認不清除
--config Allure 命令行配置路徑. 如果通過--profile and --configDirectory 覆蓋指定值
--configDirectory Allure命令行配置目錄。默認使用ALLURE_HOME 目錄.
--profile Allure 命令行配置文件
-o, --report-dir, --output 生成 Allure 報告的目錄. 默認為 allure-report
serve 報告服務命令
Usage: serve [options] The directories with allure results
Options:
--config Allure 命令行配置路徑. 如果通過--profile and --configDirectory 覆蓋指定值
--configDirectory Allure命令行配置目錄。默認使用ALLURE_HOME 目錄.
-h, --host 指定用于啟動報告 web 服務的主機
-p, --port 指定用于啟動報告 web 服務的端口,默認為 0
--profile Allure 命令行配置文件
open 打開生成的報告
Usage: open [options] 報告目錄
Options:
-h, --host 指定用于啟動報告 web 服務的主機
-p, --port T指定用于啟動報告 web 服務的端口,默認為 0
plugin 生成報告
Usage: plugin [options]
Options:
--config Allure 命令行配置路徑. 如果通過--profile and --configDirectory 覆蓋指定值
--configDirectory Allure命令行配置目錄。默認使用ALLURE_HOME 目錄.
--profile Allure 命令行配置文件
- 實例
手動生成測試報告,-c 清除 -o 指定輸出報告目錄
allure generate -c <directory-with-results> -o <directory-with-report>
三、ChromeDriver 相關(待歸納)
- chromedriver 下載
https://npm.taobao.org/mirrors/chromedriver/
http://chromedriver.storage.googleapis.com/index.html
ChromeOptions options = new ChromeOptions();
// Chrome 75 版本以下
// options.addArguments("disable-infobars");
// Chrome 75 版本以上, 去除自動化控制
options.setExperimentalOption("useAutomationExtension", false);
options.setExperimentalOption("excludeSwitches", Collections.singletonList("enable-automation"));
// 去除密碼保存提示
Map<String, Object> prefs = new HashMap<String, Object>();
prefs.put("credentials_enable_service", false);
prefs.put("profile.password_manager_enabled", false);
options.setExperimentalOption("prefs", prefs);
// 直接最大化
options.addArguments("start-maximized");
// 去除 [SEVERE]: Timed out receiving message from renderer: 0.100 信息
// https://stackoverflow.com/questions/61201270/timed-out-receiving-message-from-renderer-in-selenium
System.setProperty(ChromeDriverService.CHROME_DRIVER_SILENT_OUTPUT_PROPERTY, "true");
System.setProperty(ChromeDriverService.CHROME_DRIVER_EXE_PROPERTY, "D:\\tools\\Python\\Python37\\chromedriver.exe");
ChromeDriver chrome = new ChromeDriver(options);
// 會有個變化過程
// chrome.manage().window().maximize();
EventFiringWebDriver eventFiringWebDriver = new EventFiringWebDriver(chrome);
eventFiringWebDriver.register(new WebEventListener());
driver = eventFiringWebDriver;

浙公網安備 33010602011771號