Skip to content

feat: add support for mochawesome reporter #216

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 26 commits into from
May 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
71cf3a3
feat: add report for mochawesome
SvetoslavTsenov Apr 22, 2019
96daf59
chore: deps
SvetoslavTsenov Apr 22, 2019
0df8308
fix: mochawesome version to 3.1.2
SvetoslavTsenov Apr 23, 2019
5a67ddd
include screenshots on start and quit driver
SvetoslavTsenov Apr 25, 2019
660e2af
test: fixes
SvetoslavTsenov Apr 25, 2019
bce06d1
update mocha opts
SvetoslavTsenov Apr 26, 2019
65265e7
include exit option to opts
SvetoslavTsenov Apr 26, 2019
86c1e48
include mocha options for windows
SvetoslavTsenov Apr 29, 2019
2c991e1
fix: postinstall script for win
SvetoslavTsenov May 3, 2019
a83250b
chore: include mochawesome examples
SvetoslavTsenov May 3, 2019
e2c3f25
chore: fix typo
SvetoslavTsenov May 3, 2019
11d76c3
chore: samples
SvetoslavTsenov May 4, 2019
4ad5584
respected reporters folder
SvetoslavTsenov May 4, 2019
9ad47f3
fix options
SvetoslavTsenov May 4, 2019
4feb372
fix: add check if reportDir exists
SvetoslavTsenov May 4, 2019
23ec097
reportsDir check
SvetoslavTsenov May 4, 2019
b1abf38
fix reporter
SvetoslavTsenov May 4, 2019
9b3c6fb
remove anonymous functions
SvetoslavTsenov May 4, 2019
67938a0
refactor(screenshot): if states
SvetoslavTsenov May 5, 2019
ef9454f
feat: log-image-type
SvetoslavTsenov May 5, 2019
c392b15
fix
SvetoslavTsenov May 5, 2019
14bc1cf
fix: javascript sample
SvetoslavTsenov May 5, 2019
c71b94c
fix: parserd.t.s
SvetoslavTsenov May 5, 2019
d3bbda7
fix: typo
SvetoslavTsenov May 5, 2019
1a25d19
chore: typo
SvetoslavTsenov May 5, 2019
924333a
fix: check for testReporter
SvetoslavTsenov May 5, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ export { LogType } from "./lib/log-types";
export { INsCapabilities } from "./lib/interfaces/ns-capabilities";
export { INsCapabilitiesArgs } from "./lib/interfaces/ns-capabilities-args";
export { logInfo, logError, logWarn } from "./lib/utils";
export { ITestReporter } from "./lib/interfaces/test-reporter";
export { screencapture } from "./lib/helpers/screenshot-manager";
export { LogImageType } from "./lib/enums/log-image-type";
export declare const nsCapabilities: INsCapabilities;
export declare function startServer(port?: number, deviceManager?: IDeviceManager): Promise<AppiumServer>;
export declare function stopServer(): Promise<void>;
Expand Down
11 changes: 8 additions & 3 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ import * as frameComparerHelper from "./lib/frame-comparer";
import { FrameComparer } from "./lib/frame-comparer";
import { DeviceManager } from "./lib/device-manager";
import { DeviceController } from "mobile-devices-controller";
import { logInfo, logError, logWarn } from "./lib/utils";
import { logInfo, logError } from "./lib/utils";
import { INsCapabilities } from "./lib/interfaces/ns-capabilities";
import { INsCapabilitiesArgs } from "./lib/interfaces/ns-capabilities-args";
import * as parser from "./lib/parser"
import { isWin } from "./lib/utils";
import { LogImageType } from "./lib/enums/log-image-type";

export { AppiumDriver } from "./lib/appium-driver";
export { AppiumServer } from "./lib/appium-server";
Expand All @@ -28,6 +29,9 @@ export { LogType } from "./lib/log-types";
export { INsCapabilities } from "./lib/interfaces/ns-capabilities";
export { INsCapabilitiesArgs } from "./lib/interfaces/ns-capabilities-args";
export { logInfo, logError, logWarn } from "./lib/utils";
export { ITestReporter } from "./lib/interfaces/test-reporter";
export { screencapture } from "./lib/helpers/screenshot-manager";
export { LogImageType } from "./lib/enums/log-image-type";

export const nsCapabilities: INsCapabilities = new NsCapabilities(parser);

Expand Down Expand Up @@ -80,8 +84,9 @@ export async function createDriver(args?: INsCapabilitiesArgs) {
if (args) {
nsCapabilities.extend(args);
}
const port = nsCapabilities.port || appiumServer.port;

if (!nsCapabilities.port) {
nsCapabilities.port = appiumServer.port;
}
if (nsCapabilities.attachToDebug) {
if (!appiumDriver) {
appiumDriver = await AppiumDriver.createAppiumDriver(nsCapabilities);
Expand Down
1 change: 1 addition & 0 deletions lib/appium-driver.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ export declare class AppiumDriver {
private compare;
prepareImageToCompare(filePath: string, rect: IRectangle): Promise<void>;
takeScreenshot(fileName: string): Promise<string>;
testReporterLog(log: any): any;
logScreenshot(fileName: string): Promise<string>;
getlog(logType: LogType): Promise<any>;
logPageSource(fileName: string): Promise<void>;
Expand Down
63 changes: 56 additions & 7 deletions lib/appium-driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ import {
logInfo,
prepareDevice,
getStorage,
encodeImageToBase64
encodeImageToBase64,
ensureReportsDirExists,
checkImageLogType
} from "./utils";

import { INsCapabilities } from "./interfaces/ns-capabilities";
Expand All @@ -45,8 +47,10 @@ import { ImageHelper } from "./image-helper";
import { ImageOptions } from "./image-options"
import { unlinkSync, writeFileSync, existsSync } from "fs";
import { DeviceManager } from "../lib/device-manager";
import { extname, basename } from "path";
import { extname, basename, join } from "path";
import { LogType } from "./log-types";
import { screencapture } from "./helpers/screenshot-manager";
import { LogImageType } from "./enums/log-image-type";

export class AppiumDriver {
private static pngFileExt = '.png';
Expand Down Expand Up @@ -274,8 +278,17 @@ export class AppiumDriver {
}
if (hasStarted) {
console.log("Appium driver has started successfully!");
if (checkImageLogType(args.testReporter, LogImageType.screenshots)) {
args.testReporterLog(`appium_driver_started`);
args.testReporterLog(screencapture(`${getReportPath(args)}/appium_driver_started.png`));
}
} else {
logError("Appium driver is NOT started!")
logError("Appium driver is NOT started!");
if (checkImageLogType(args.testReporter, LogImageType.screenshots)) {
ensureReportsDirExists(args);
args.testReporterLog(`appium_driver_boot_failure`);
args.testReporterLog(screencapture(`${getReportPath(args)}/appium_driver_boot_failure.png`));
}
}

retries--;
Expand Down Expand Up @@ -566,7 +579,6 @@ export class AppiumDriver {
}

private async compare(imageName: string, timeOutSeconds: number = 3, tolerance: number = 0.01, rect?: IRectangle, toleranceType?: ImageOptions) {

if (!this._logPath) {
this._logPath = getReportPath(this._args);
}
Expand All @@ -588,6 +600,9 @@ export class AppiumDriver {
copy(pathActualImage, pathActualImageToReportsFolder, false);

console.log("Remove the 'actual' suffix to continue using the image as expected one ", pathExpectedImage);
this._args.testReporterLog(basename(pathActualImage));
this._args.testReporterLog(join(this._logPath, basename(pathActualImage)));

return false;
}

Expand All @@ -609,8 +624,17 @@ export class AppiumDriver {

await this.prepareImageToCompare(pathActualImage, rect);
result = await this._imageHelper.compareImages(pathActualImage, pathExpectedImage, pathDiffImage, tolerance, toleranceType);
if (checkImageLogType(this._args.testReporter, LogImageType.everyImage)) {
this._args.testReporterLog("Actual image: ");
this._args.testReporterLog(join(this._logPath, basename(pathActualImage)));
}
counter++;
}

if (!checkImageLogType(this._args.testReporter, LogImageType.everyImage)) {
this._args.testReporterLog("Actual image: ");
this._args.testReporterLog(join(this._logPath, basename(pathDiffImage)));
}
} else {
if (existsSync(pathDiffImage)) {
unlinkSync(pathDiffImage);
Expand Down Expand Up @@ -653,15 +677,30 @@ export class AppiumDriver {
});
}

public testReporterLog(log: any): any {
if (this._args.testReporterLog) {
return this._args.testReporterLog(log);
}
return undefined;
}

public async logScreenshot(fileName: string) {
if (!this._logPath) {
this._logPath = getReportPath(this._args);
}
if (!fileName.endsWith(AppiumDriver.pngFileExt)) {
fileName = fileName.concat(AppiumDriver.pngFileExt);
fileName = fileName.concat(AppiumDriver.pngFileExt).replace(/\s+/ig, "_");
}

const imgPath = await this.takeScreenshot(resolvePath(this._logPath, fileName));
if (Object.getOwnPropertyNames(this._args.testReporter).length > 0) {
this.testReporterLog(fileName.replace(/\.\w+/ig, ""));
fileName = join(this._logPath, fileName);
fileName = this.testReporterLog(fileName);
}

fileName = resolvePath(this._logPath, fileName)

const imgPath = await this.takeScreenshot(fileName);
return imgPath;
}

Expand Down Expand Up @@ -741,6 +780,8 @@ export class AppiumDriver {
*/
public async backgroundApp(minutes: number) {
logInfo("Sending the currently active app to the background ...");
this._args.testReporterLog("Sending the currently active app to the background ...");

await this._driver.backgroundApp(minutes);
}

Expand All @@ -750,7 +791,7 @@ export class AppiumDriver {
public async hideDeviceKeyboard() {
try {
await this._driver.hideDeviceKeyboard();
} catch (error) {}
} catch (error) { }
}

public async isKeyboardShown() {
Expand All @@ -777,11 +818,19 @@ export class AppiumDriver {
await this._driver.quit();
this._isAlive = false;
console.log("Driver is dead!");
if (checkImageLogType(this._args.testReporter, LogImageType.screenshots)) {
this._args.testReporterLog(`appium_driver_quit`);
this._args.testReporterLog(screencapture(`${getReportPath(this._args)}/appium_driver_quit.png`));
}
} else {
//await this._webio.detach();
}
} catch (error) {
if (this._args.verbose) {
if (checkImageLogType(this._args.testReporter, LogImageType.screenshots)) {
this._args.testReporterLog(`appium_driver_quit_failure`);
this._args.testReporterLog(screencapture(`${getReportPath(this._args)}/appium_driver_quit_failure.png`));
}
console.dir(error);
}
}
Expand Down
39 changes: 32 additions & 7 deletions lib/appium-server.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ChildProcess, spawn, execSync } from "child_process";
import { ChildProcess, spawn } from "child_process";
import {
log,
resolvePath,
Expand All @@ -10,13 +10,18 @@ import {
logInfo,
prepareApp,
prepareDevice,
stopServerCommand
getReportPath,
ensureReportsDirExists,
logError,
checkImageLogType
} from "./utils";
import { INsCapabilities } from "./interfaces/ns-capabilities";
import { IDeviceManager } from "./interfaces/device-manager";
import { DeviceManager } from "./device-manager";
import { existsSync } from "fs";
import { killAllProcessAndRelatedCommand } from "mobile-devices-controller";
import { screencapture } from "./helpers/screenshot-manager";
import { LogImageType } from "./enums/log-image-type";

export class AppiumServer {
private _server: ChildProcess;
Expand Down Expand Up @@ -98,6 +103,19 @@ export class AppiumServer {
}

this.hasStarted = response;
try {
ensureReportsDirExists(this._args);
if (checkImageLogType(this._args.testReporter, LogImageType.screenshots)) {
this._args.testReporterLog(`on_server_started`);
this._args.testReporterLog(screencapture(`${getReportPath(this._args)}/on_server_started.png`));
}
} catch (error) {
logError(`Appium server is NOT started - ${error.message}`);
if (checkImageLogType(this._args.testReporter, LogImageType.screenshots)) {
this._args.testReporterLog(`on_start_server_failure`);
this._args.testReporterLog(screencapture(`${getReportPath(this._args)}/on_start_server_failure.png`));
}
}
return response;
} else if (!this._args.attachToDebug) {
return true;
Expand Down Expand Up @@ -158,6 +176,12 @@ export class AppiumServer {
this._server.kill("SIGKILL");
process.kill(this._server.pid, "SIGKILL");
shutdown(this._server, this._args.verbose);
try {
if (checkImageLogType(this._args.testReporter, LogImageType.screenshots)) {
this._args.testReporterLog(`on_server_stopped`);
this._args.testReporterLog(screencapture(`${getReportPath(this._args)}/on_server_stopped.png`));
}
} catch (error) { }
}
} catch (error) {
console.log(error);
Expand Down Expand Up @@ -195,14 +219,15 @@ export class AppiumServer {
logInfo("Using project-local Appium binary.", this._args.verbose);
appium = projectAppiumBinary;
} else {
// TODO: find faster and reliable way to check if appium is installed globally
//const result = executeCommand("npm list -g");
//if (result.includes("appium")) {
// if (result.includes("appium")) {
logWarn("Using global Appium binary.");
console.log('Please, make sure it is installed globally.');
// console.log('Please, make sure it is installed globally.');
//} else if (result.includes("appium")) {
// const msg = "Appium not found. Please install appium before runnig tests!";
// log(msg, this._args.verbose);
// new Error(msg);
// const msg = "Appium not found. Please install appium before runnig tests!";
// log(msg, this._args.verbose);
// new Error(msg);
// }
}

Expand Down
13 changes: 13 additions & 0 deletions lib/enums/log-image-type.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export declare enum LogImageType {
/**
* Setting this property to add each image
* during the image comparison into the report.
* If not set, it will be logged only the last image comparison.
*/
everyImage = "everyImage",
/**
* Setting this property to take screenshot on each hook
* and add the images into the report.
*/
screenshots = "screenshots"
}
13 changes: 13 additions & 0 deletions lib/enums/log-image-type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export enum LogImageType {
/**
* Setting this property to add each image
* during the image comparison into the report.
* If not set, it will be logged only the last image comparison.
*/
everyImage = "everyImage",
/**
* Setting this property to take screenshot on each hook
* and add the images into the report.
*/
screenshots = "screenshots"
}
1 change: 1 addition & 0 deletions lib/helpers/screenshot-manager.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export declare const screencapture: (imageFullName: string) => string;
31 changes: 31 additions & 0 deletions lib/helpers/screenshot-manager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@


import { isWin, isMac, isLinux } from "../utils";
import { execSync } from "child_process";

export const screencapture = (imageFullName: string) => {
let command = "", result = "";

if (isWin()) {
command = "";
} else if (isMac()) {
command = `screencapture -x -f '${imageFullName}'`;
} else if (isLinux()) {
command = `gnome-screenshot -f '${imageFullName}'`;
} else {
console.log("We could not capture the screen. Not supported OS!");
}

if (command) {
try {
execSync(command) + "";
result = imageFullName;
} catch (error) {
result = "We could not capture the screen!";
}
} else {
result = "No file name provided to catch the screen!";
}

return result;
}
12 changes: 8 additions & 4 deletions lib/image-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as PngJsImage from "pngjs-image";
import { ImageOptions } from "./image-options";
import { INsCapabilities } from "./interfaces/ns-capabilities";
import { IRectangle } from "./interfaces/rectangle";
import { LogImageType } from "./enums/log-image-type";

export class ImageHelper {

Expand Down Expand Up @@ -57,23 +58,26 @@ export class ImageHelper {
}

private runDiff(diffOptions: BlinkDiff, diffImage: string) {
var that = this;
return new Promise<boolean>((resolve, reject) => {
diffOptions.run(function (error, result) {
if (error) {
throw error;
} else {
let message;
let message: string;
let resultCode = diffOptions.hasPassed(result.code);
if (resultCode) {
message = "Screen compare passed!";
console.log(message);
console.log('Found ' + result.differences + ' differences.');
return resolve(true);
} else {
message = "Screen compare failed!";
message = `Screen compare failed! Found ${result.differences} differences.\n`;
console.log(message);
console.log('Found ' + result.differences + ' differences.');
console.log('Diff image: ' + diffImage);
if (Object.getOwnPropertyNames(that._args.testReporter).length > 0 && that._args.testReporter.logImageTypes && that._args.testReporter.logImageTypes.indexOf(LogImageType.everyImage) > -1) {
that._args.testReporterLog(message);
that._args.testReporterLog(diffImage);
}
return resolve(false);
}
}
Expand Down
Loading