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

Commit

Permalink
fix: fix failing gracefully when elements that need to be removed don…
Browse files Browse the repository at this point in the history
…'t exist
  • Loading branch information
wswebcreation committed Apr 18, 2020
1 parent 06d8d62 commit 932520c
Show file tree
Hide file tree
Showing 5 changed files with 1,675 additions and 846 deletions.
15 changes: 14 additions & 1 deletion lib/helpers/afterScreenshot.ts
Expand Up @@ -7,6 +7,7 @@ import {join} from 'path';
import {Executor} from '../methods/methods.interface';
import {AfterScreenshotOptions, ScreenshotOutput} from './afterScreenshot.interfaces';
import hideRemoveElements from '../clientSideScripts/hideRemoveElements';
import {yellow} from "chalk";

/**
* Methods that need to be executed after a screenshot has been taken
Expand Down Expand Up @@ -43,7 +44,19 @@ export default async function afterScreenshot(executor: Executor, options: After
// Show elements again
/* istanbul ignore else */
if (hideElements.length > 0 || removeElements.length > 0) {
await executor(hideRemoveElements, {hide: hideElements, remove: removeElements}, false);
try {
await executor(hideRemoveElements, {hide: hideElements, remove: removeElements}, false);
} catch (e) {
console.log(yellow(`
#####################################################################################
WARNING:
(One of) the elements that needed to be hidden or removed could not be found on the
page and caused this error
Error: ${e}
We made sure the test didn't break.
#####################################################################################
`));
}
}

// Remove the custom set css
Expand Down
15 changes: 14 additions & 1 deletion lib/helpers/beforeScreenshot.ts
Expand Up @@ -6,6 +6,7 @@ import getEnrichedInstanceData from '../methods/instanceData';
import {BeforeScreenshotOptions, BeforeScreenshotResult} from './beforeScreenshot.interface';
import {Executor} from '../methods/methods.interface';
import hideRemoveElements from '../clientSideScripts/hideRemoveElements';
import {yellow} from "chalk";

/**
* Methods that need to be executed before a screenshot will be taken
Expand Down Expand Up @@ -41,7 +42,19 @@ export default async function beforeScreenshot(

// Hide and or Remove elements
if (hideElements.length > 0 || removeElements.length > 0) {
await executor(hideRemoveElements, {hide: hideElements, remove: removeElements}, true);
try {
await executor(hideRemoveElements, {hide: hideElements, remove: removeElements}, true);
} catch (e) {
console.log(yellow(`
#####################################################################################
WARNING:
(One of) the elements that needed to be hidden or removed could not be found on the
page and caused this error
Error: ${e}
We made sure the test didn't break.
#####################################################################################
`));
}
}

// Set some custom css
Expand Down
86 changes: 80 additions & 6 deletions lib/methods/screenshots.ts
@@ -1,3 +1,5 @@
// @ts-ignore
import {yellow} from 'chalk';
import scrollToPosition from '../clientSideScripts/scrollToPosition';
import getDocumentScrollHeight from '../clientSideScripts/getDocumentScrollHeight';
import getAndroidStatusAddressToolBarHeight from '../clientSideScripts/getAndroidStatusAddressToolBarHeight';
Expand Down Expand Up @@ -116,7 +118,19 @@ export async function getFullPageScreenshotsDataNativeMobile(

// Elements that need to be hidden after the first scroll for a fullpage scroll
if (i === 1 && hideAfterFirstScroll.length > 0) {
await executor(hideRemoveElements, {hide: hideAfterFirstScroll, remove: []}, true);
try {
await executor(hideRemoveElements, {hide: hideAfterFirstScroll, remove: []}, true);
} catch (e) {
console.log(yellow(`
#####################################################################################
WARNING:
(One of) the elements that needed to be hidden or removed could not be found on the
page and caused this error
Error: ${e}
We made sure the test didn't break.
#####################################################################################
`));
}
}

// Take the screenshot and get the width
Expand Down Expand Up @@ -155,7 +169,19 @@ export async function getFullPageScreenshotsDataNativeMobile(

// Put back the hidden elements to visible
if (hideAfterFirstScroll.length > 0) {
await executor(hideRemoveElements, {hide: hideAfterFirstScroll, remove: []}, false);
try {
await executor(hideRemoveElements, {hide: hideAfterFirstScroll, remove: []}, false);
} catch (e) {
console.log(yellow(`
#####################################################################################
WARNING:
(One of) the elements that needed to be hidden or removed could not be found on the
page and caused this error
Error: ${e}
We made sure the test didn't break.
#####################################################################################
`));
}
}

return {
Expand Down Expand Up @@ -196,7 +222,19 @@ export async function getFullPageScreenshotsDataAndroidChromeDriver(

// Elements that need to be hidden after the first scroll for a fullpage scroll
if (i === 1 && hideAfterFirstScroll.length > 0) {
await executor(hideRemoveElements, {hide: hideAfterFirstScroll, remove: []}, true);
try {
await executor(hideRemoveElements, {hide: hideAfterFirstScroll, remove: []}, true);
} catch (e) {
console.log(yellow(`
#####################################################################################
WARNING:
(One of) the elements that needed to be hidden or removed could not be found on the
page and caused this error
Error: ${e}
We made sure the test didn't break.
#####################################################################################
`));
}
}

// Take the screenshot
Expand Down Expand Up @@ -233,7 +271,19 @@ export async function getFullPageScreenshotsDataAndroidChromeDriver(

// Put back the hidden elements to visible
if (hideAfterFirstScroll.length > 0) {
await executor(hideRemoveElements, {hide: hideAfterFirstScroll, remove: []}, false);
try {
await executor(hideRemoveElements, {hide: hideAfterFirstScroll, remove: []}, false);
} catch (e) {
console.log(yellow(`
#####################################################################################
WARNING:
(One of) the elements that needed to be hidden or removed could not be found on the
page and caused this error
Error: ${e}
We made sure the test didn't break.
#####################################################################################
`));
}
}

return {
Expand Down Expand Up @@ -272,7 +322,19 @@ export async function getFullPageScreenshotsDataDesktop(

// Elements that need to be hidden after the first scroll for a fullpage scroll
if (i === 1 && hideAfterFirstScroll.length > 0) {
await executor(hideRemoveElements, {hide: hideAfterFirstScroll, remove: []}, true);
try {
await executor(hideRemoveElements, {hide: hideAfterFirstScroll, remove: []}, true);
} catch (e) {
console.log(yellow(`
#####################################################################################
WARNING:
(One of) the elements that needed to be hidden or removed could not be found on the
page and caused this error
Error: ${e}
We made sure the test didn't break.
#####################################################################################
`));
}
}

// Take the screenshot
Expand Down Expand Up @@ -320,7 +382,19 @@ export async function getFullPageScreenshotsDataDesktop(

// Put back the hidden elements to visible
if (hideAfterFirstScroll.length > 0) {
await executor(hideRemoveElements, {hide: hideAfterFirstScroll, remove: []}, false);
try {
await executor(hideRemoveElements, {hide: hideAfterFirstScroll, remove: []}, false);
} catch (e) {
console.log(yellow(`
#####################################################################################
WARNING:
(One of) the elements that needed to be hidden or removed could not be found on the
page and caused this error
Error: ${e}
We made sure the test didn't break.
#####################################################################################
`));
}
}

return {
Expand Down

0 comments on commit 932520c

Please sign in to comment.