[特别提示]:
除非特别声明,iquicktest均为原创报道,转载请注明作者及原文链接
[原文链接]: http://www.iquicktest.com/selenium-testng-maven.html
[原文链接]: http://www.iquicktest.com/selenium-testng-maven.html
上次的selenium教程中我们已经简单介绍了SELENIUM教程之MAVEN自动配置,那么这次的课程我们会在此基础上利用maven配置TestNG并运行自动化测试用例。TestNG是一款类似于Junit的测试工具,但是个人认为对于selenium自动化测试来说TestNG更加适合,那么说那么多我们来试试此工具吧。
一、在Eclipse中安装TestNG插件
进入到eclipse marketplace中下载并安装TestNG
具体安装步骤可以参照SELENIUM教程之MAVEN自动配置中安装maven插件的步骤。
二、修改maven配置文件pom.xml
添加一段TestNG的dependency
1
2
3
4
5
6
7
|
<
dependency
>
<
groupId
>org.testng</
groupId
>
<
artifactId
>testng</
artifactId
>
<
version
>6.1.1</
version
>
<
scope
>test</
scope
>
</
dependency
>
<
dependency
>
|
三、新建一个TestNG测试类
新建一个BaiduSearch类后输入以下脚本:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
package
iquicktest.com.selenium_maven_eclipse;
import
org.openqa.selenium.By;
import
org.openqa.selenium.WebDriver;
import
org.openqa.selenium.firefox.FirefoxDriver;
import
org.testng.Assert;
import
org.testng.annotations.Test;
public
class
BaiduSearch{
public
WebDriver driver;
public
BaiduSearch(){
driver =
new
FirefoxDriver();
}
@Test
public
void
SearchBaidu(){
driver.get(
"http://www.baidu.com"
);
driver.findElement(By.name(
"wd"
)).sendKeys(
"iquicktest"
);
Assert.assertTrue(driver.getTitle().contains(
"百度"
));
driver.quit();
}
}
|
四、运行测试
在脚本中右键debug as 选中TestNG test
运行结果:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
[TestNG] Running:
C:\Users\jerryc\AppData\Local\Temp\testng-eclipse--
59420497
\testng-customsuite.xml
PASSED: SearchBaidu
===============================================
Default test
Tests run:
1
, Failures:
0
, Skips:
0
===============================================
===============================================
Default suite
Total tests run:
1
, Failures:
0
, Skips:
0
===============================================
[TestNG] Time taken by org.testng.reporters.jq.Main
@2da5660
:
273
ms
[TestNG] Time taken by org.testng.reporters.XMLReporter
@62deaa2e
:
43
ms
[TestNG] Time taken by org.testng.reporters.JUnitReportReporter
@1ee8d9a5
:
32
ms
[TestNG] Time taken by org.testng.reporters.SuiteHTMLReporter
@1cb81be4
:
160
ms
[TestNG] Time taken by org.testng.reporters.EmailableReporter2
@37db1db7
:
15
ms
[TestNG] Time taken by [FailedReporter passed=
0
failed=
0
skipped=
0
]:
1
ms
|
五、查看测试报告
选中项目后点击F5刷新以下项目,之后项目中会新生成一个test-output文件夹,展开后点击index.html即可
点击后可以查看TestNG运行后生成的html报告。