从0开始搭建自动化测试框架(一)
此系列是自己尝试搭建web自动化测试框架之路的记录,不定时更新。
1. IDE: IntelliJ IDEA(version 2020.1.3)
2. 框架: Selenium(Java) + TestNG + allure
3. 搭建过程:
IntelliJ IDEA 下载地址:https://www.jetbrains.com/idea/download
Maven:
下载地址:https://maven.apache.org/download.cgi
安装:这里以3.6.0版本为例, 下载.zip后, 解压到任意目录,我解压目录是D:\apache-maven-3.6.0
配置:关于maven的详细配置可以参考我另一个随笔: https://www.cnblogs.com/qianjin100/p/14842181.html
IDEA 自带maven, 设置是在 file -> setting, 打开后找到maven,如下:
Maven home directory: maven 的安装目录,这里是IDEA自带的maven, 所以目录比较奇怪
User settings file: 用户设置文件所在目录
Local repository: 用户本地仓库,用来存放从远程仓库下载下来的文件。
Maven project: IDEA里依次点击 file -> New - > Project ,如下所示, 在Maven里选中Create from archetype,然后选择maven-archetype-quickstart,点击next
进入如下页面后,依次填入必要信息,这里project 名称我直接写成automation, GroupId根据个人情况填写,这里以Test为例。点击next
在如下页面填写对应信息,这里项目名就叫AutoTest. 点击next, 进入如下页面, 我勾选了Local repository的 Override 复选框,即使用我指定的本地仓库,点击Finish完成项目创建。
项目创建完成后,IDEA会执行Maven初始化命令下载必要的包,下载完成后,进行pom.xml配置,如下:
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>11.0</maven.compiler.source> <maven.compiler.target>11.0</maven.compiler.target> <xmlFileName>testng.xml</xmlFileName> <test.report.dir>test-output</test.report.dir> <aspectj.version>1.9.6</aspectj.version> <ashot-version>1.5.4</ashot-version> <allure-testng-version>2.13.10</allure-testng-version> </properties> <dependencies> <dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>6.9.10</version> <scope>compile</scope> </dependency> <dependency> <groupId>com.oracle.database.jdbc</groupId> <artifactId>ojdbc8</artifactId> <version>19.7.0.0</version> </dependency> <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> <version>3.141.59</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.6</version> </dependency> <dependency> <groupId>com.google.inject</groupId> <artifactId>guice</artifactId> <version>4.2.2</version> </dependency> <dependency> <groupId>io.qameta.allure</groupId> <artifactId>allure-testng</artifactId> <version>${allure-testng-version}</version> </dependency> </dependencies> <build> <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) --> <plugins> <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle --> <plugin> <artifactId>maven-clean-plugin</artifactId> <version>3.1.0</version> </plugin> <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging --> <plugin> <artifactId>maven-resources-plugin</artifactId> <version>3.0.2</version> </plugin> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.0</version> </plugin> <plugin> <artifactId>maven-surefire-plugin</artifactId> <version>3.0.0-M4</version> <configuration> <testFailureIgnore>true</testFailureIgnore> <skipTests>false</skipTests> <suiteXmlFiles> <suiteXmlFile>${xmlFileName}</suiteXmlFile> </suiteXmlFiles> <argLine> -javaagent:"${settings.localRepository}/org/aspectj/aspectjweaver/${aspectj.version}/aspectjweaver-${aspectj.version}.jar" </argLine> <!-- path to generate allure-result --> <systemProperties> <property> <name>allure.results.directory</name> <value>./allure/allure-results</value> </property> </systemProperties> <properties> <property> <name>usedefaultlisteners</name> <value>false</value> </property> </properties> </configuration> <dependencies> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>${aspectj.version}</version> </dependency> </dependencies> </plugin> <plugin> <artifactId>maven-jar-plugin</artifactId> <version>3.0.2</version> </plugin> <plugin> <artifactId>maven-install-plugin</artifactId> <version>2.5.2</version> </plugin> <plugin> <artifactId>maven-deploy-plugin</artifactId> <version>2.8.2</version> </plugin> <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle --> <plugin> <artifactId>maven-site-plugin</artifactId> <version>3.7.1</version> </plugin> <plugin> <artifactId>maven-project-info-reports-plugin</artifactId> <version>3.0.0</version> </plugin> </plugins> </pluginManagement> </build>
log4j.xml 配置文件:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE log4j:configuration PUBLIC "-//log4j/log4j Configuration//EN" "log4j.dtd"> <log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/"> <!-- output log to file --> <appender name="fileAppender" class="org.apache.log4j.FileAppender"> <!-- log file name --> <param name="File" value="./src/main/java/com/changefinancial/log/logfile.log" /> <param name="Append" value="false"/> <param name="Threshold" value="INFO"/> <!-- style of output log -->` <layout class="org.apache.log4j.PatternLayout"> <!-- format of output log --> <param name="ConversionPattern" value="[%d{MMdd HH:mm:ss SSS\} %-5p] [%t] %c{3\} - %m%n" /> </layout> </appender> <root> <!-- set log level --> <level value="INFO" /> <appender-ref ref="fileAppender" /> </root> </log4j:configuration>