要想使用TestNG做自动化测试,首先要学会搭建环境。从网络上看了一下,基本上是使用eclipse+TestNG进行测试的。
1.首先去eclipse官网下载一个eclipse
2.去官网dowmload一个TestNG插件,这个工作在eclipse内完成,点击help->install new software , 输入 http://beust.com/eclipse。如图2.1所示。
图2.1 TestNG插件安装
选择TestNG,点击next,直到finish。TestNG就安装成功了。
3.所需包(前期刚学习时可以从官网下载,后期可以直接用管理工具进行配置自动加载)
4.新建工程
新建一个java proiect 取一个合适的名字,如Test.并将上面的包附加到工程中
5.在工程中创建testng测试用例。右键单击test工程的src文件夹,选择new->other->TestNG->TestNG class
6.输入对应的内容,finish.
7.首先写一段基础代码,能跑起来说明我们的环境没有问题。
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打开百度页面,然后可以根据我们的需要,做一些操作。具体代码如下:
public class HelloWorld {
@Test
public void f() throws Exception {
System.out.println("创建浏览器并打开百度");
// 创建一个 浏览器实例
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(); //不同的浏览器需要不同的驱动来实现,此处是谷歌浏览器
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)
最终发现是因为谷歌浏览器需要相应的驱动。而火狐浏览器自带驱动。下载驱动之后,在代码中加入:
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("创建浏览器并打开百度");
// 设置 chrome 的路径
System.setProperty(
"webdriver.chrome.driver",
"C:\Program Files (x86)\Google\Chrome\Application\chromedriver.exe");
// 创建一个 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");
}
}
运行起来啦~
还要注意运行时可能会被防火墙拦截而超时,连接不上,关闭防火墙即可。