自动化框架
- Base基类:启动、关闭浏览器,定义DataProvider()数据驱动
- 页面:页面元素单独定义,使用xpath、css、js等
- 操作:具有业务意义的操作独立封装
- 案例:按业务需求调用操作
- 工具包:数据库操作封装、读取Excel、字符串处理等
- 监听器
- 截图机制:
截图时间点:重写TestListenerAdapter接口中的onTestFailure、onTestSkipped方法;
(注:TestListenerAdapter实际实现的是 ITestListener,直接实现 ITestListener也可以)
截图方法:
File screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
filePath = "result/test-report/screenshot/"+ fileName + ".jpg";
-
重试机制:重写TestNG IRetryAnalyzer接口retry方法。重写IAnnotationTransformer接口transform方法检查是否有getRetryAnalyzer,如果没有,定义为自定义的analyzer。
(注:IAnnotationTransformer用来修改@test注解,IAnnotationTransformer2用来修改@DataProvider, @Factory 以及 @Configuration) -
报告部分:重写TestNG IReporter接口generateReport方法,testng.xml文件中配置listener
UI自动化之图像对比
package UITest;
import java.awt.image.BufferedImage;
import java.awt.image.DataBuffer;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class ImageCompare {
public static Boolean imageCompare() {
File fileInput = new File("D:\\2.png");
File fileOutput = new File("D:\\1.png");
try {
BufferedImage bufferInput = ImageIO.read(fileInput);
BufferedImage bufferOutput = ImageIO.read(fileOutput);
DataBuffer dafileInput= bufferInput.getData().getDataBuffer();
DataBuffer dafileOutput= bufferOutput.getData().getDataBuffer();
int sizeFileInput = dafileInput.getSize();
int sizeFileOutput = dafileOutput.getSize();
Boolean matchFlag = false;
if(sizeFileInput == sizeFileOutput) {
for(int i = 0; i < sizeFileInput; i ++) {
if(dafileInput.getElem(i) != dafileOutput.getElem(i)) {
matchFlag = false;
break;
}
}
matchFlag = true;
}
return matchFlag;
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
public static void main(String args[]) {
System.out.println(imageCompare());
}
}
UI自动化截图
File screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.***FILE***);
public class TestResultListener extends TestListenerAdapter {
private static Logger logger = Logger.getLogger(TestResultListener.class.getName());
protected ITestContext testContext = null;
String browser = null;
@Override
public void onStart(ITestContext testContext) {
this.testContext = testContext;
browser = String.valueOf(testContext.getCurrentXmlTest().getParameter("browserName"));
super.onStart(testContext);
}
@Override
public void onTestFailure(ITestResult tr) {
super.onTestFailure(tr);
WebDriver webDriver = (WebDriver) testContext.getAttribute("SELENIUM_DRIVER");
saveScreenShot(tr, webDriver,browser);
}
@Override
public void onTestSkipped(ITestResult tr) {
super.onTestSkipped(tr);
WebDriver webDriver = (WebDriver) testContext.getAttribute("SELENIUM_DRIVER");
saveScreenShot(tr, webDriver,browser);
}
失败用例重新运行
public class TestngRetry implements IRetryAnalyzer {
static {
LogConfiguration.initLog("TestngRetryPage_");
}
private static Logger logger = Logger.getLogger(TestngRetry.class);
private int retryCount = 1;
private static int maxRetryCount;
static {
ConfigReader config = ConfigReader.getInstance();
maxRetryCount = config.getRetryCount();
logger.info("RetryCount=" + maxRetryCount);
logger.info("SourceDir=" + config.getSourceCodeDir());
logger.info("SourceEncoding=" + config.getSrouceCodeEncoding());
}
public boolean retry(ITestResult result) {
if (retryCount <= maxRetryCount) {
String message = "Retry for: [" + result.getName() + "] on class [" + result.getTestClass().getName() + "] retry " + retryCount + " times";
logger.info(message);
Reporter.setCurrentTestResult(result);
Reporter.log("RunCount=" + (retryCount + 1));
retryCount++;
return true;
}
return false;
}
网友评论