要想使用TestNG做自動化測試橡疼,首先要學會搭建環(huán)境援所。從網(wǎng)絡上看了一下,基本上是使用eclipse+TestNG進行測試的欣除。
1.首先去eclipse官網(wǎng)下載一個eclipse
2.去官網(wǎng)dowmload一個TestNG插件住拭,這個工作在eclipse內完成,點擊help->install new software , 輸入 http://beust.com/eclipse历帚。如圖2.1所示滔岳。
圖2.1 TestNG插件安裝
選擇TestNG,點擊next,直到finish。TestNG就安裝成功了挽牢。
3.所需包(前期剛學習時可以從官網(wǎng)下載谱煤,后期可以直接用管理工具進行配置自動加載)
4.新建工程
新建一個java proiect 取一個合適的名字,如Test.并將上面的包附加到工程中
5.在工程中創(chuàng)建testng測試用例禽拔。右鍵單擊test工程的src文件夾刘离,選擇new->other->TestNG->TestNG class
6.輸入對應的內容,finish.
7.首先寫一段基礎代碼,能跑起來說明我們的環(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測試用例檢測太惠,我們使用webdriver打開百度頁面磨淌,然后可以根據(jù)我們的需要,做一些操作凿渊。具體代碼如下:
public class HelloWorld {
@Test
public void f() throws Exception {
System.out.println("創(chuàng)建瀏覽器并打開百度");
// 創(chuàng)建一個 瀏覽器實例
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(); //不同的瀏覽器需要不同的驅動來實現(xiàn)梁只,此處是谷歌瀏覽器
WebDriver driver = new FirefoxDriver();//此處是火狐瀏覽器
當我切換為谷歌瀏覽器的時候,運行這段代碼遇到了以下錯誤:
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)是因為谷歌瀏覽器需要相應的驅動埃脏。而火狐瀏覽器自帶驅動搪锣。下載驅動之后,在代碼中加入:
System.setProperty(
"webdriver.chrome.driver",
"C:\Program Files (x86)\Google\Chrome\Application\chromedriver.exe");
運行即可彩掐。
谷歌瀏覽器驅動下載地址:http://chromedriver.storage.googleapis.com/index.html
谷歌瀏覽器最終運行代碼:
public class HelloWorld {
@Test
public void f() throws Exception {
System.out.println("創(chuàng)建瀏覽器并打開百度");
// 設置 chrome 的路徑
System.setProperty(
"webdriver.chrome.driver",
"C:\Program Files (x86)\Google\Chrome\Application\chromedriver.exe");
// 創(chuàng)建一個 Chrome 的瀏覽器實例
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");
}
}
運行起來啦~
還要注意運行時可能會被防火墻攔截而超時构舟,連接不上,關閉防火墻即可堵幽。