定制元素属性检查,并写到ReportNG中

QTP 和Selenium 都会有这种要检查某一个控件元素属性的情况,比如去检查一个Button的显示文字是什么?

为了更方便的书写程序,并优美的显示到HTML测试报告中,做了以下几个小小的封装,只是小小的尝试,让大家做个参考,抛砖引玉了。。

脚本实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public void verifyAttribute(WebElement we, String weName, String propertyName, String expectValue)
{
String actualValue = we.getAttribute(propertyName);

log("INFO", "Verify {" + propertyName + "} attribute for element [" + weName + "]");

if (actualValue.equals(expectValue)){
Reporter.log("<TABLE border='1'><TR><TD>Actual Value</TD><TD>"+actualValue+"</TD></TR><TR><TD>Expected Value</TD><TD>"+expectValue+"</TD></TR><TR><TD>Checkpoint Status</TD><TD style='background-color:green'><b>Passed</b></TD></TR></TABLE>");

}
else {
Reporter.log("<TABLE border='1'><TR><TD>Actual Value</TD><TD>"+actualValue+"</TD></TR><TR><TD>Expected Value</TD><TD>"+expectValue+"</TD></TR><TR><TD>Checkpoint Status</TD><TD style='background-color:red'><b>Failed</b></TD></TR></TABLE>");
handleFailure("Error Case Research: ");
}

Assert.assertEquals(actualValue, expectValue);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public static void log(String logType, String logInfo){
System.setProperty("org.uncommons.reportng.escape-output", "false");
System.setProperty("com.testng.reporter.escape-output", "false");

switch(logType){
case "INFO":
Reporter.log(logTime + "&nbsp;&nbsp;&nbsp;<font color='gray'><strong>INFO&nbsp;&nbsp;&nbsp;</strong></font>" + logInfo);
break;
case "PASS":
Reporter.log(logTime + "&nbsp;&nbsp;&nbsp;<font color='green'><strong>PASS&nbsp;&nbsp;&nbsp;</strong></font>" + logInfo);
break;
case "FAIL":
Reporter.log(logTime + "&nbsp;&nbsp;&nbsp;<font color='red'><strong>FAIL&nbsp;&nbsp;&nbsp;</strong></font>" + logInfo);
break;
}
}

这里只是个例子,要区别一下getAttribute和getText。

脚本调用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 public void search()
{

openBrowser();
openURL();
BaiduSearch yy = new BaiduSearch(driver);
verifyAttribute(yy.searchButton, "Search Button", "type", "submit");
driver.quit();
}

@Test
public void aa()
{

openURL();
BaiduSearch yy = new BaiduSearch(driver);

verifyAttribute(yy.searchButton, "Search Button", "type", "sgubmit");
driver.close();
}

结果显示

Test Result

唐胡璐 wechat
欢迎您扫一扫上面的微信公众号,订阅我的博客!
分享创造价值,您的支持将鼓励我继续前行!