您好,登錄后才能下訂單哦!
失敗自動截圖
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("<font color=\"#FF0000\">截圖位置</font><br /> " + targetFile.getPath());
LogUtil.info("------file is ---- " + targetFile.getPath());
try {
FileUtils.copyFile(srcFile, targetFile);
} catch (IOException e) {
e.printStackTrace();
}
logTestEnd(result, "Failed");
//報告截圖后面顯示
Reporter.log("<img src=\"./screenshots/" + s + "\" width=\"64\" height=\"64\" alt=\"***\" onMouseover=\"this.width=353; this.height=613\" onMouseout=\"this.width=64;this.height=64\" />");
}
/**
* 在用例執行結束時,打印用例的執行結果信息
*/
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<ITestResult> testsToBeRemoved = new ArrayList<ITestResult>();
// collect all id's from passed test
Set<Integer> passedTestIds = new HashSet<Integer>();
for (ITestResult passedTest : testContext.getPassedTests().getAllResults()) {
logger.info("PassedTests = " + passedTest.getName());
passedTestIds.add(getId(passedTest));
}
Set<Integer> failedTestIds = new HashSet<Integer>();
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<ITestResult> 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<AndroidElement> driver;
/**
* @param port :服務器啟動的端口號,系統自動獲取
* @param udid :手機設備號:系統自動化獲取
* @param apk :自動化運行的APK包,系統會根據該地址獲取包名與actiber
* @param serverUrl :客戶端ip地址默認 127.0.0.1
* @param flag :true 卸掉有重新安裝與運行后自動化卸掉包。false 直接安裝運行
* @return
*/
public static AndroidDriver<AndroidElement> initDriver(String port, String udid, String apk, String serverUrl, boolean flag) {
ArrayList<String> packAct = OperationalCmd.getPackAct(apk);
// File app = new File(".\\apk\\20171026.apk");
DesiredCapabilities caps = new DesiredCapabilities();
//自動安裝
if (flag) {
caps.setCapability(MobileCapabilityType.APP, apk);
//結束后會卸載程序
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:設置操作手機的唯一標識,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應用的包名
caps.setCapability(AndroidMobileCapabilityType.APP_PACKAGE, packAct.get(0));
//APP_ACTIVITY :啟動app的起始activity
caps.setCapability(AndroidMobileCapabilityType.APP_ACTIVITY, packAct.get(1));
//UNICODE_KEYBOARD:1、中文輸入不支持,2、不用它鍵盤會彈出來,說不定會影響下一步操作.需要注意設置后,需要將手機的輸入法進行修改
caps.setCapability(AndroidMobileCapabilityType.UNICODE_KEYBOARD, AndroidCapabilityType.UNICODE_KEY_BOARD);
//Reset_KEYBOARD:是否重置輸入法
caps.setCapability(AndroidMobileCapabilityType.RESET_KEYBOARD, AndroidCapabilityType.RESET_KEY_BOARD);
//NO_SIGN:跳過檢查和對應用進行 debug 簽名的
caps.setCapability(AndroidMobileCapabilityType.NO_SIGN, AndroidCapabilityType.NO_SIGN);
try {
//appium測試服務的地址
driver = new AndroidDriver<>(new URL(serverUrl + ":" + port + "/wd/hub"), caps);
} catch (MalformedURLException e) {
e.printStackTrace();
}
return driver;
}
/**
* 截圖使用
*
* @return
*/
public static AppiumDriver getDriver() {
return driver;
}
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。