目錄
建立一個maven工程
pom.xml中的内容
編寫測試用例
testng.xml的内容
通過IDEA運作testng.xml
指令行運作testng.xml
建立一個maven工程
使用Idea建立maven工程
建立類似如上的工程結構,src/main/java,src/test/java,pom.xml,testng.xml,這裡由于我們使用工程是用來進行自動化測試的,是以實際這裡src/main/java是用不到的,隻是IDEA統一規則建立了而已。這裡個人還建立了一個bin目錄,用來存放chromedriver等浏覽器執行檔案,用來比對不同的作業系統及不同的浏覽器版本。
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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.wilmar.test</groupId>
<artifactId>woodpecker</artifactId>
<version>1.0-SNAPSHOT</version>
<!--<packaging>jar</packaging>-->
<dependencies>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.14.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.141.59</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.3</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>prepare-package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
<excludeScope>provided</excludeScope>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
<distributionManagement>
<repository>
<id>releases</id>
<name>Team nexus Release Repository</name>
<url>http://10.118.888.10:8888/nexus/content/repositories/releases</url>
</repository>
<snapshotRepository>
<id>snapshots</id>
<name>Team nexus Snapshot Repository</name>
<url>http://10.118.888.10:8888/nexus/content/repositories/snapshots</url>
</snapshotRepository>
</distributionManagement>
</project>
這裡計劃使用TestNG和Selenium 作為自動化測試的架構,是以添加了這兩個依賴包(根據需要添加)。另外repository鏡像源位址最好添加為公司私有的資源鏡像位址或者國内一些開源的鏡像位址,比如阿裡雲、網易等。
編寫測試用例
在src/test/java下建立一個測試類,包名com.nitb.demo(根據自己意願随意取),類名Demo1。
代碼如下:
package com.nitb.demo;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import java.io.File;
public class Demo1 {
private static WebDriver driver;
private static String executePath;
@BeforeClass
public void init() {
executePath = System.getProperty("user.dir") + File.separator + "bin" + File.separator + "chromedriver_mac";
System.setProperty("webdriver.chrome.driver", executePath);
driver = new ChromeDriver();
}
@Test
public void testDemoLogin() throws InterruptedException{
System.out.print("testDemoLogin");
}
@AfterClass
public void quit() {
driver.quit();
}
}
測試用例比較簡單,三個邏輯:
- 測試類執行個體化開始的時候,執行init,設定下目前系統應該使用哪個chromedriver,因為我是mac,且Chrome浏覽器的版本是72.0.3626.81,是以去selenium官網下載下傳了對應版本的driver放在bin下面使用。
- 使用testng.xml去運作類中有@Test注釋的測試方法。
- 測試類執行個體銷毀的時候,退出ChromeDriver。
testng.xml的内容
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite1" verbose="1" >
<test name="Regression1">
<classes>
<class name="com.wilmar.loms.testcase.ManualOrderTest"/>
</classes>
</test>
</suite>
這裡的demo屬于較簡單的:
- suite:代表一個測試集,裡面可以包含多個測試。
- test:代表一個測試事務,裡面可以運作多個測試類及多個測試方法。
- classes:包含測試事務要運作的所有測試類。
- classs:代表具體運作哪個類。
通過IDEA運作testng.xml
右擊testng.xml檔案,然後點選run xxx/testngxml即可。
指令行運作testng.xml
java -ea -cp $CLASSPATH org.testng.TestNG testng.xml
這裡需要注意的是$CLASSPATH指的是所有的依賴包,jar包較多,這裡不舉例,另外最後一個參數可以是testng.xml單個檔案,也可以是包含多個testng xml配置檔案的路徑。