Skip to content
This repository has been archived by the owner on Dec 24, 2023. It is now read-only.

Commit

Permalink
chore: code optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
wswebcreation committed Nov 8, 2020
1 parent 39e95d3 commit 99cfcab
Show file tree
Hide file tree
Showing 7 changed files with 137 additions and 91 deletions.
11 changes: 8 additions & 3 deletions lib/commands/saveElement.ts
Expand Up @@ -53,7 +53,7 @@ export default async function saveElement(
const enrichedInstanceData: BeforeScreenshotResult = await beforeScreenshot(methods.executor, beforeOptions, true);

// 3. Take the screenshot
const screenshot: string = await takeBase64Screenshot(methods.screenShot);
const base64Image: string = await takeBase64Screenshot(methods.screenShot);

// 4. Determine the rectangles
const elementRectangleOptions: ElementRectanglesOptions = {
Expand All @@ -63,10 +63,15 @@ export default async function saveElement(
isAndroid: enrichedInstanceData.isAndroid,
isIos: enrichedInstanceData.isIos,
};
const rectangles: RectanglesOutput = await determineElementRectangles(methods.executor, screenshot, elementRectangleOptions, element);
const rectangles: RectanglesOutput = await determineElementRectangles({
executor: methods.executor,
base64Image,
options: elementRectangleOptions,
element
});

// 5. Make a cropped base64 image with resizeDimensions
const croppedBase64Image = await makeCroppedBase64Image(screenshot, rectangles, logLevel, resizeDimensions);
const croppedBase64Image = await makeCroppedBase64Image({base64Image, rectangles, logLevel, resizeDimensions});

// 6. The after the screenshot methods
const afterOptions: AfterScreenshotOptions = {
Expand Down
6 changes: 3 additions & 3 deletions lib/commands/saveScreen.ts
Expand Up @@ -49,7 +49,7 @@ export default async function saveScreen(
const enrichedInstanceData: BeforeScreenshotResult = await beforeScreenshot(methods.executor, beforeOptions);

// 3. Take the screenshot
const screenshot:string = await takeBase64Screenshot(methods.screenShot);
const base64Image:string = await takeBase64Screenshot(methods.screenShot);

// Determine the rectangles
const screenRectangleOptions: ScreenRectanglesOptions = {
Expand All @@ -60,10 +60,10 @@ export default async function saveScreen(
isAndroidNativeWebScreenshot: enrichedInstanceData.isAndroidNativeWebScreenshot,
isIos: enrichedInstanceData.isIos,
};
const rectangles: RectanglesOutput = determineScreenRectangles(screenshot, screenRectangleOptions);
const rectangles: RectanglesOutput = determineScreenRectangles(base64Image, screenRectangleOptions);

// 4. Make a cropped base64 image
const croppedBase64Image: string = await makeCroppedBase64Image(screenshot, rectangles, logLevel);
const croppedBase64Image: string = await makeCroppedBase64Image({base64Image, rectangles, logLevel});

// 5. The after the screenshot methods
const afterOptions: AfterScreenshotOptions = {
Expand Down
12 changes: 10 additions & 2 deletions lib/methods/images.interfaces.ts
@@ -1,5 +1,6 @@
import {RectanglesOutput} from './rectangles.interfaces';
import {LogLevel} from "../helpers/options.interface";
import {DEFAULT_RESIZE_DIMENSIONS} from "../helpers/constants";

export interface ResizeDimensions {
// The bottom margin
Expand Down Expand Up @@ -89,7 +90,7 @@ export interface MethodImageCompareCompareOptions {
// Allowable value of misMatchPercentage that prevents saving image with differences
saveAboveTolerance?: number;
//Scale images to same size before comparison
scaleImagesToSameSize?:boolean;
scaleImagesToSameSize?: boolean;
}

export interface ImageCompareFolderOptions {
Expand Down Expand Up @@ -132,7 +133,7 @@ export interface CompareOptions {
output?: {
ignoredBoxes?: IgnoreBoxes[]
};
scaleToSameSize? : boolean;
scaleToSameSize?: boolean;
}

export interface IgnoreBoxes {
Expand All @@ -141,3 +142,10 @@ export interface IgnoreBoxes {
left: number;
top: number;
}

export interface CroppedBase64Image {
base64Image: string;
rectangles: RectanglesOutput;
logLevel: LogLevel;
resizeDimensions?: number | ResizeDimensions;
}
14 changes: 7 additions & 7 deletions lib/methods/images.ts
Expand Up @@ -7,7 +7,7 @@ import {DEFAULT_RESIZE_DIMENSIONS} from '../helpers/constants';
import {determineStatusAddressToolBarRectangles} from './rectangles';
import {RectanglesOutput} from './rectangles.interfaces';
import {
CompareOptions,
CompareOptions, CroppedBase64Image,
IgnoreBoxes,
ImageCompareOptions,
ImageCompareResult,
Expand Down Expand Up @@ -73,12 +73,12 @@ export async function checkBaselineImageExists(
/**
* Make a cropped image with Canvas
*/
export async function makeCroppedBase64Image(
base64Image: string,
rectangles: RectanglesOutput,
logLevel: LogLevel,
resizeDimensions: number | ResizeDimensions = DEFAULT_RESIZE_DIMENSIONS,
): Promise<string> {
export async function makeCroppedBase64Image({
base64Image,
rectangles,
logLevel,
resizeDimensions = DEFAULT_RESIZE_DIMENSIONS,
}: CroppedBase64Image): Promise<string> {
/**
* This is in for backwards compatibility, it will be removed in the future
*/
Expand Down
9 changes: 9 additions & 0 deletions lib/methods/rectangles.interfaces.ts
@@ -1,3 +1,5 @@
import {Executor} from "./methods.interface";

export interface RectanglesOptions {
// The device pixel ration of the screen / device
devicePixelRatio: number;
Expand Down Expand Up @@ -48,3 +50,10 @@ export interface StatusAddressToolBarRectanglesOptions {
export interface StatusAddressToolBarRectangles extends Array<RectanglesOutput>{

}

export interface ElementRectangles{
executor: Executor;
base64Image: string;
options: ElementRectanglesOptions;
element: any;
}
156 changes: 90 additions & 66 deletions lib/methods/rectangles.spec.ts
@@ -1,4 +1,8 @@
import {determineElementRectangles, determineScreenRectangles, determineStatusAddressToolBarRectangles} from './rectangles';
import {
determineElementRectangles,
determineScreenRectangles,
determineStatusAddressToolBarRectangles
} from './rectangles';
import {IMAGE_STRING} from '../mocks/mocks';

describe('rectangles', () => {
Expand All @@ -12,7 +16,7 @@ describe('rectangles', () => {
isIos: true,
};
const MOCKED_EXECUTOR = jest.fn()
// getElementPositionIos for: getIosStatusAddressToolBarHeight
// getElementPositionIos for: getIosStatusAddressToolBarHeight
.mockResolvedValueOnce(
{
statusAddressBar: {
Expand All @@ -37,7 +41,12 @@ describe('rectangles', () => {
y: 10,
});

expect(await determineElementRectangles(MOCKED_EXECUTOR, IMAGE_STRING, options, 'element')).toMatchSnapshot();
expect(await determineElementRectangles({
executor: MOCKED_EXECUTOR,
base64Image: IMAGE_STRING,
options,
element: 'element',
})).toMatchSnapshot();
});

it('should determine them for Android Native webscreenshot', async () => {
Expand All @@ -49,7 +58,7 @@ describe('rectangles', () => {
isIos: false,
};
const MOCKED_EXECUTOR = jest.fn()
// getElementPositionAndroid for: getAndroidStatusAddressToolBarHeight
// getElementPositionAndroid for: getAndroidStatusAddressToolBarHeight
.mockResolvedValueOnce(
{
statusAddressBar: {
Expand All @@ -74,7 +83,12 @@ describe('rectangles', () => {
y: 10,
});

expect(await determineElementRectangles(MOCKED_EXECUTOR, IMAGE_STRING, options, 'element')).toMatchSnapshot();
expect(await determineElementRectangles({
executor: MOCKED_EXECUTOR,
base64Image: IMAGE_STRING,
options,
element: 'element',
})).toMatchSnapshot();
});

it('should determine them for Android ChromeDriver', async () => {
Expand All @@ -86,7 +100,7 @@ describe('rectangles', () => {
isIos: false,
};
const MOCKED_EXECUTOR = jest.fn()
// getElementPositionAndroid for: getElementPositionTopWindow
// getElementPositionAndroid for: getElementPositionTopWindow
.mockResolvedValueOnce(
{
height: 20,
Expand All @@ -95,7 +109,12 @@ describe('rectangles', () => {
y: 0,
});

expect(await determineElementRectangles(MOCKED_EXECUTOR, IMAGE_STRING, options, 'element')).toMatchSnapshot();
expect(await determineElementRectangles({
executor: MOCKED_EXECUTOR,
base64Image: IMAGE_STRING,
options,
element: 'element',
})).toMatchSnapshot();
});

it('should determine them for a desktop browser', async () => {
Expand All @@ -107,7 +126,7 @@ describe('rectangles', () => {
isIos: false,
};
const MOCKED_EXECUTOR = jest.fn()
// getElementPositionDesktop for: getElementPositionTopWindow
// getElementPositionDesktop for: getElementPositionTopWindow
.mockResolvedValueOnce(
{
height: 20,
Expand All @@ -116,7 +135,12 @@ describe('rectangles', () => {
y: 34,
});

expect(await determineElementRectangles(MOCKED_EXECUTOR, IMAGE_STRING, options, 'element')).toMatchSnapshot();
expect(await determineElementRectangles({
executor: MOCKED_EXECUTOR,
base64Image: IMAGE_STRING,
options,
element: 'element',
})).toMatchSnapshot();
});
});

Expand Down Expand Up @@ -173,22 +197,22 @@ describe('rectangles', () => {
blockOutToolBar: true,
};
const MOCKED_EXECUTOR = jest.fn()
// determineStatusAddressToolBarRectangles for: getIosStatusAddressToolBarHeight
// determineStatusAddressToolBarRectangles for: getIosStatusAddressToolBarHeight
.mockResolvedValueOnce(
{
statusAddressBar: {
height: 94,
width: 375,
x: 0,
y: 0,
},
toolBar: {
height: 5,
width: 135,
x: 120,
y: 799,
},
});
statusAddressBar: {
height: 94,
width: 375,
x: 0,
y: 0,
},
toolBar: {
height: 5,
width: 135,
x: 120,
y: 799,
},
});

expect(await determineStatusAddressToolBarRectangles(MOCKED_EXECUTOR, options)).toMatchSnapshot();
});
Expand All @@ -204,29 +228,29 @@ describe('rectangles', () => {
blockOutToolBar: false,
};
const MOCKED_EXECUTOR = jest.fn()
// determineStatusAddressToolBarRectangles for: getIosStatusAddressToolBarHeight
// determineStatusAddressToolBarRectangles for: getIosStatusAddressToolBarHeight
.mockResolvedValueOnce(
{
statusAddressBar: {
height: 94,
width: 375,
x: 0,
y: 0,
},
toolBar: {
height: 5,
width: 135,
x: 0,
y: 799,
},
});
statusAddressBar: {
height: 94,
width: 375,
x: 0,
y: 0,
},
toolBar: {
height: 5,
width: 135,
x: 0,
y: 799,
},
});

expect(await determineStatusAddressToolBarRectangles(MOCKED_EXECUTOR, options)).toMatchSnapshot();
});

it('should determine the rectangles for Android with a status and toolbar blockout', async () => {
const options = {
isHybridApp:false,
isHybridApp: false,
isMobile: true,
isViewPortScreenshot: true,
platformName: 'Android',
Expand All @@ -235,22 +259,22 @@ describe('rectangles', () => {
blockOutToolBar: true,
};
const MOCKED_EXECUTOR = jest.fn()
// determineStatusAddressToolBarRectangles for: getAndroidStatusAddressToolBarHeight
// determineStatusAddressToolBarRectangles for: getAndroidStatusAddressToolBarHeight
.mockResolvedValueOnce(
{
statusAddressBar: {
height: 40,
width: 320,
x: 0,
y: 0,
},
toolBar: {
height: 100,
width: 320,
x: 0,
y: 600,
},
});
statusAddressBar: {
height: 40,
width: 320,
x: 0,
y: 0,
},
toolBar: {
height: 100,
width: 320,
x: 0,
y: 600,
},
});

expect(await determineStatusAddressToolBarRectangles(MOCKED_EXECUTOR, options)).toMatchSnapshot();
});
Expand All @@ -266,22 +290,22 @@ describe('rectangles', () => {
blockOutToolBar: false,
};
const MOCKED_EXECUTOR = jest.fn()
// determineStatusAddressToolBarRectangles for: getAndroidStatusAddressToolBarHeight
// determineStatusAddressToolBarRectangles for: getAndroidStatusAddressToolBarHeight
.mockResolvedValueOnce(
{
statusAddressBar: {
height: 40,
width: 320,
x: 0,
y: 0,
},
toolBar: {
height: 100,
width: 320,
x: 0,
y: 600,
},
});
statusAddressBar: {
height: 40,
width: 320,
x: 0,
y: 0,
},
toolBar: {
height: 100,
width: 320,
x: 0,
y: 600,
},
});

expect(await determineStatusAddressToolBarRectangles(MOCKED_EXECUTOR, options)).toMatchSnapshot();
});
Expand Down

0 comments on commit 99cfcab

Please sign in to comment.