要想使用TestNG做自動(dòng)化測(cè)試,首先要學(xué)會(huì)搭建環(huán)境。從網(wǎng)絡(luò)上看了一下,基本上是使用eclipse+TestNG進(jìn)行測(cè)試的。
1.首先去eclipse官網(wǎng)下載一個(gè)eclipse
2.去官網(wǎng)dowmload一個(gè)TestNG插件,這個(gè)工作在eclipse內(nèi)完成,點(diǎn)擊help->install new software , 輸入 http://beust.com/eclipse。如圖2.1所示。

圖2.1 TestNG插件安裝
選擇TestNG,點(diǎn)擊next,直到finish。TestNG就安裝成功了。
3.所需包(前期剛學(xué)習(xí)時(shí)可以從官網(wǎng)下載,后期可以直接用管理工具進(jìn)行配置自動(dòng)加載)

4.新建工程
新建一個(gè)java proiect 取一個(gè)合適的名字,如Test.并將上面的包附加到工程中

5.在工程中創(chuàng)建testng測(cè)試用例。右鍵單擊test工程的src文件夾,選擇new->other->TestNG->TestNG class

6.輸入對(duì)應(yīng)的內(nèi)容,finish.

7.首先寫一段基礎(chǔ)代碼,能跑起來說明我們的環(huán)境沒有問題。
public class HelloWorld {
@Test
public void f() throws Exception {
System.out.println("hello world");
}
@BeforeTest
public void beforeTest() {
System.out.println("hellobefore");
}
@AfterTest
public void afterTest() {
System.out.println("helloafter");
}
}
8.接下來,使用webdriver+TestNG測(cè)試用例檢測(cè),我們使用webdriver打開百度頁面,然后可以根據(jù)我們的需要,做一些操作。具體代碼如下:
public class HelloWorld {
@Test
public void f() throws Exception {
System.out.println("創(chuàng)建瀏覽器并打開百度");
// 創(chuàng)建一個(gè) 瀏覽器實(shí)例
WebDriver driver = new FirefoxDriver();
driver.get("http://www.baidu.com");
Thread.sleep(5000);
driver.quit();
}
@BeforeTest
public void beforeTest() {
System.out.println("hellobefore");
}
@AfterTest
public void afterTest() {
System.out.println("helloafter");
}
}
在此說一下我遇到的問題:
WebDriver driver = new ChromeDriver(); //不同的瀏覽器需要不同的驅(qū)動(dòng)來實(shí)現(xiàn),此處是谷歌瀏覽器
WebDriver driver = new FirefoxDriver();//此處是火狐瀏覽器
當(dāng)我切換為谷歌瀏覽器的時(shí)候,運(yùn)行這段代碼遇到了以下錯(cuò)誤:
java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.chrome.driver system property; for more information, see https://github.com/SeleniumHQ/selenium/wiki/ChromeDriver. The latest version can be downloaded from http://chromedriver.storage.googleapis.com/index.html
at com.google.common.base.Preconditions.checkState(Preconditions.java:847)
at org.openqa.selenium.remote.service.DriverService.findExecutable(DriverService.java:125)
at org.openqa.selenium.chrome.ChromeDriverService.accessBuilder.findDefaultExecutable(ChromeDriverService.java:156)
at org.openqa.selenium.remote.service.DriverService$Builder.build(DriverService.java:346)
at org.openqa.selenium.chrome.ChromeDriverService.createDefaultService(ChromeDriverService.java:91)
at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:123)
at TestClass.HelloWorld.f(HelloWorld.java:27)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:124)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:583)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:719)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:989)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
at org.testng.TestRunner.privateRun(TestRunner.java:648)
at org.testng.TestRunner.run(TestRunner.java:505)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:455)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:415)
at org.testng.SuiteRunner.run(SuiteRunner.java:364)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1208)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1137)
at org.testng.TestNG.runSuites(TestNG.java:1049)
at org.testng.TestNG.run(TestNG.java:1017)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:114)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)
最終發(fā)現(xiàn)是因?yàn)楣雀铻g覽器需要相應(yīng)的驅(qū)動(dòng)。而火狐瀏覽器自帶驅(qū)動(dòng)。下載驅(qū)動(dòng)之后,在代碼中加入:
System.setProperty(
"webdriver.chrome.driver",
"C:\Program Files (x86)\Google\Chrome\Application\chromedriver.exe");
運(yùn)行即可。
谷歌瀏覽器驅(qū)動(dòng)下載地址:http://chromedriver.storage.googleapis.com/index.html
谷歌瀏覽器最終運(yùn)行代碼:
public class HelloWorld {
@Test
public void f() throws Exception {
System.out.println("創(chuàng)建瀏覽器并打開百度");
// 設(shè)置 chrome 的路徑
System.setProperty(
"webdriver.chrome.driver",
"C:\Program Files (x86)\Google\Chrome\Application\chromedriver.exe");
// 創(chuàng)建一個(gè) Chrome 的瀏覽器實(shí)例
WebDriver driver = new ChromeDriver();
driver.get("http://www.baidu.com");
Thread.sleep(5000);
driver.quit();
}
@BeforeTest
public void beforeTest() {
System.out.println("hellobefore");
}
@AfterTest
public void afterTest() {
System.out.println("helloafter");
}
}
運(yùn)行起來啦~

還要注意運(yùn)行時(shí)可能會(huì)被防火墻攔截而超時(shí),連接不上,關(guān)閉防火墻即可。