Selenium2.47.1 + Maven3.3.9 + TestNG6.8.8
windows准備好以下環境
1、 Jdk,環境變量配置
2、 maven環境
3、 eclipse 開發工具 ,eclipse安裝好testng插件
動手:
1、 Maven安裝配置,參考我的博客:http://www.cnblogs.com/lincj/p/5470032.html
2、 新建個文件夾test,在cmd進入到test目錄:
運行mvn archetype:generate
回車直到,看到:"Define value for groupId: :"停止
輸入groupID,格式:"com.test",回車
輸入artifactId,格式:"test",回車
回車
輸入package,格式:"com.test.test",回車 輸入Y,回車
看到BUILD SUCCESS,成功 maven工程創建完畢
3、工程做如下修改,把默認創建的src下目錄全部刪掉,在src下創建一個目錄testScript
4、 進入test文件夾,有個pom.xml打開,
<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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.test</groupId> <artifactId>test</artifactId> <packaging>jar</packaging> <version>1.0-SNAPSHOT</version> <name>test</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> </project>
把以上內容替換為:
<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.test</groupId> <artifactId>lincj_test</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <name>lincj_test</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> <version>2.47.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>6.8.8</version> </dependency> </dependencies> <build> <sourceDirectory>${basedir}/src</sourceDirectory> <testSourceDirectory>${basedir}/src</testSourceDirectory> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <version>2.7</version> <configuration> <encoding>UTF-8</encoding> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>1.7</source> <target>1.7</target> <encoding>UTF-8</encoding> </configuration> </plugin> <plugin> <artifactId>maven-surefire-plugin</artifactId> <version>2.19.1</version> <configuration> <suiteXmlFiles> <suiteXmlFile>testng.xml</suiteXmlFile> </suiteXmlFiles> </configuration> </plugin> </plugins> </build> </project>
5、在test文件夾下,新建一個批處理文件:build.bat
內容如下
@echo off
setlocal
call mvn eclipse:clean
call mvn -npu eclipse:eclipse -Dwtpversion=1.0 -DdownloadSources=true
pause
endloca
6、 打開eclipse,安裝testng插件(可參考我的博客:http://www.cnblogs.com/lincj/p/5470903.html),如果已經安裝跳過
7、打開eclipse—》 window—》Preferences,設置兩個地方
Name:M2_REPO Path:C:/Documents and Settings/Administrator/.m2/repository 注:Path為Maven本地倉庫
9、 重啟eclipse—》import—》existing projects into workspace,導入項目"test" 項目
在test項目下創建testng文件,內容如下:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"> <suite name="test"> <test name="version" preserve-order="true"> <classes> <class name="testScript.login"> <methods> <include name="login" /> </methods> </class> </classes> </test> </suite>
關於testng.xml文件各種標簽的意義大家自行上網上去查
10、 Maven是約定優於配置,把測試腳本創建在src/testScript下,創建以下腳本:login.java,代碼如下:
package testScript; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.testng.Assert; import org.testng.annotations.AfterClass; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; public class login { WebDriver driver; @Test public void login(){ driver=new FirefoxDriver(); driver.get("http://www.baidu.com"); driver.quit(); } }
11、到此項目搭建完成,可以通過dos窗口,進入test目錄,運行:mvn install 或者在eclipse中右鍵項目選擇maven->maven install