失敗自動截圖
十多年專業(yè)網(wǎng)站建設(shè)公司歷程,堅持以創(chuàng)新為先導(dǎo)的網(wǎng)站服務(wù),服務(wù)超過數(shù)千家企業(yè)及個人,涉及網(wǎng)站設(shè)計、重慶APP開發(fā)、微信開發(fā)、平面設(shè)計、互聯(lián)網(wǎng)整合營銷等多個領(lǐng)域。在不同行業(yè)和領(lǐng)域給人們的工作和生活帶來美好變化。
public class MyTestngListener extends TestListenerAdapter {
private static Logger logger = Logger.getLogger(MyTestngListener.class);
public static final String CONFIG = "config.properties";
@Override
public void onTestFailure(ITestResult result) {
super.onTestFailure(result);
logger.info(result.getName() + " Failure");
WaitUtil.sleep(2000);
AppiumDriver driver = DriverBase.getDriver();
File srcFile = driver.getScreenshotAs(OutputType.FILE);
DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd_hhmmss");
File location = new File(AndroidCapabilityType.LOCAL_SCREEN_FILE_URL);
if (!location.exists()) {
location.mkdirs();
}
String dest = result.getMethod().getRealClass().getSimpleName() + "." + result.getMethod().getMethodName();
String s = dest + "_" + dateFormat.format(new Date()) + ".png";
File targetFile =
new File(location + "/" + s);
LogUtil.info("截圖位置:");
Reporter.log("截圖位置
" + targetFile.getPath());
LogUtil.info("------file is ---- " + targetFile.getPath());
try {
FileUtils.copyFile(srcFile, targetFile);
} catch (IOException e) {
e.printStackTrace();
}
logTestEnd(result, "Failed");
//報告截圖后面顯示
Reporter.log("");
}
/**
* 在用例執(zhí)行結(jié)束時,打印用例的執(zhí)行結(jié)果信息
*/
protected void logTestEnd(ITestResult tr, String result) {
Reporter.log(String.format("=============Result: %s=============", result), true);
}
@Override
public void onTestSkipped(ITestResult tr) {
super.onTestSkipped(tr);
logger.info(tr.getName() + " Skipped");
//takeScreenShot(tr);
}
@Override
public void onTestSuccess(ITestResult tr) {
super.onTestSuccess(tr);
logger.info(tr.getName() + " Success");
}
@Override
public void onTestStart(ITestResult tr) {
super.onTestStart(tr);
logger.info(tr.getName() + " Start");
}
@Override
public void onFinish(ITestContext testContext) {
super.onFinish(testContext);
ArrayList testsToBeRemoved = new ArrayList();
// collect all id's from passed test
Set passedTestIds = new HashSet();
for (ITestResult passedTest : testContext.getPassedTests().getAllResults()) {
logger.info("PassedTests = " + passedTest.getName());
passedTestIds.add(getId(passedTest));
}
Set failedTestIds = new HashSet();
for (ITestResult failedTest : testContext.getFailedTests().getAllResults()) {
logger.info("failedTest = " + failedTest.getName());
// id = class + method + dataprovider
int failedTestId = getId(failedTest);
// if we saw this test as a failed test before we mark as to be deleted
// or delete this failed test if there is at least one passed version
if (failedTestIds.contains(failedTestId) || passedTestIds.contains(failedTestId)) {
testsToBeRemoved.add(failedTest);
} else {
failedTestIds.add(failedTestId);
}
}
// finally delete all tests that are marked
for (Iterator iterator = testContext.getFailedTests().getAllResults().iterator(); iterator.hasNext(); ) {
ITestResult testResult = iterator.next();
if (testsToBeRemoved.contains(testResult)) {
logger.info("Remove repeat Fail Test: " + testResult.getName());
iterator.remove();
}
}
}
private int getId(ITestResult result) {
int id = result.getTestClass().getName().hashCode();
id = id + result.getMethod().getMethodName().hashCode();
id = id + (result.getParameters() != null ? Arrays.hashCode(result.getParameters()) : 0);
return id;
}
}
通過啟動后獲取driver
public static AndroidDriver driver;
/**
* @param port :服務(wù)器啟動的端口號,系統(tǒng)自動獲取
* @param udid :手機設(shè)備號:系統(tǒng)自動化獲取
* @param apk :自動化運行的APK包,系統(tǒng)會根據(jù)該地址獲取包名與actiber
* @param serverUrl :客戶端ip地址默認 127.0.0.1
* @param flag :true 卸掉有重新安裝與運行后自動化卸掉包。false 直接安裝運行
* @return
*/
public static AndroidDriver initDriver(String port, String udid, String apk, String serverUrl, boolean flag) {
ArrayList packAct = OperationalCmd.getPackAct(apk);
// File app = new File(".\\apk\\20171026.apk");
DesiredCapabilities caps = new DesiredCapabilities();
//自動安裝
if (flag) {
caps.setCapability(MobileCapabilityType.APP, apk);
//結(jié)束后會卸載程序
caps.setCapability(MobileCapabilityType.FULL_RESET, AndroidCapabilityType.FULL_RESET);
}
caps.setCapability(AndroidMobileCapabilityType.APPLICATION_NAME, udid);
//PLATFORM_NAME: 平臺名稱
caps.setCapability(AndroidMobileCapabilityType.PLATFORM_NAME, AndroidCapabilityType.PLATFORM_NAME);
// UDID:設(shè)置操作手機的唯一標識,android手機可以通過adb devices查看
caps.setCapability(MobileCapabilityType.DEVICE_NAME, udid);
//NEW_COMMAND_TIMEOUT: appium server和腳本之間的 session超時時間
caps.setCapability(AndroidCapabilityType.NEW_COMMAND_TIMEOUT, AndroidCapabilityType.NEW_COMMAND_TIMEOUT);
//APP_PACKAG:Android應(yīng)用的包名
caps.setCapability(AndroidMobileCapabilityType.APP_PACKAGE, packAct.get(0));
//APP_ACTIVITY :啟動app的起始activity
caps.setCapability(AndroidMobileCapabilityType.APP_ACTIVITY, packAct.get(1));
//UNICODE_KEYBOARD:1、中文輸入不支持,2、不用它鍵盤會彈出來,說不定會影響下一步操作.需要注意設(shè)置后,需要將手機的輸入法進行修改
caps.setCapability(AndroidMobileCapabilityType.UNICODE_KEYBOARD, AndroidCapabilityType.UNICODE_KEY_BOARD);
//Reset_KEYBOARD:是否重置輸入法
caps.setCapability(AndroidMobileCapabilityType.RESET_KEYBOARD, AndroidCapabilityType.RESET_KEY_BOARD);
//NO_SIGN:跳過檢查和對應(yīng)用進行 debug 簽名的
caps.setCapability(AndroidMobileCapabilityType.NO_SIGN, AndroidCapabilityType.NO_SIGN);
try {
//appium測試服務(wù)的地址
driver = new AndroidDriver<>(new URL(serverUrl + ":" + port + "/wd/hub"), caps);
} catch (MalformedURLException e) {
e.printStackTrace();
}
return driver;
}
/**
* 截圖使用
*
* @return
*/
public static AppiumDriver getDriver() {
return driver;
}