Skip to content
This repository has been archived by the owner on Mar 20, 2024. It is now read-only.

Commit

Permalink
Merge pull request #87 from astappiev/feature/capture-dom-poll
Browse files Browse the repository at this point in the history
Use getCaptureDomAndPollScript
  • Loading branch information
astappiev committed Sep 2, 2019
2 parents 74bc61b + 91bfeb0 commit 12667b0
Showing 1 changed file with 50 additions and 13 deletions.
63 changes: 50 additions & 13 deletions packages/dom-utils/lib/DomCapture.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,22 @@ const axios = require('axios');
const { URL } = require('url');

const { ArgumentGuard, Location, GeneralUtils, PerformanceUtils } = require('@applitools/eyes-common');
const { getCaptureDomScript } = require('@applitools/dom-capture');
const { getCaptureDomAndPollScript } = require('@applitools/dom-capture');

const DomCaptureReturnType = {
OBJECT: 'OBJECT',
STRING: 'STRING',
};

const SCRIPT_RESPONSE_STATUS = {
WIP: 'WIP',
ERROR: 'ERROR',
SUCCESS: 'SUCCESS',
};

const DOM_EXTRACTION_TIMEOUT = 5 * 60 * 1000;
const DOCUMENT_LOCATION_HREF_SCRIPT = 'return document.location.href';

/**
* @ignore
*/
Expand Down Expand Up @@ -55,16 +64,12 @@ class DomCapture {
* @return {Promise<{string}>}
*/
async getWindowDom() {
const captureDomScript = await getCaptureDomScript();
const captureDomScript = await getCaptureDomAndPollScript();

const asyncCaptureDomScript =
`var callback = arguments[arguments.length - 1];
(${captureDomScript})().then(res => {
return callback(res)
})`;
const script = `${captureDomScript} return __captureDomAndPoll();`;
const url = await this._driver.getCurrentUrl();

return this.getFrameDom(asyncCaptureDomScript, url);
return this.getFrameDom(script, url);
}

/**
Expand All @@ -73,9 +78,28 @@ class DomCapture {
* @return {Promise<{string}>}
*/
async getFrameDom(script, url) {
const domSnapshotRaw = await this._driver.executeAsyncScript(script);
let result;

const domSnapshotRawArr = domSnapshotRaw ? domSnapshotRaw.split('\n') : [];
let isCheckTimerTimedOut = false;
setTimeout(() => {
isCheckTimerTimedOut = true;
}, DOM_EXTRACTION_TIMEOUT);

do {
const resultAsString = await this._driver.executeScript(script);
result = JSON.parse(resultAsString);
await this._driver.sleep(200);
} while (result.status === SCRIPT_RESPONSE_STATUS.WIP && !isCheckTimerTimedOut);

if (result.status === SCRIPT_RESPONSE_STATUS.ERROR) {
throw new Error('DomCapture Error: ' + result.error);
}

if (isCheckTimerTimedOut) {
throw new Error('DomCapture Timed out');
}

const domSnapshotRawArr = result && result.value ? result.value.split('\n') : [];

if (domSnapshotRawArr.length === 0) {
return {};
Expand Down Expand Up @@ -111,21 +135,34 @@ class DomCapture {

for (const iframeXpath of iframeArr) {
if (iframeXpath) {
const framesCount = await this._switchToFrame(iframeXpath);
let domIFrame;
try {
domIFrame = await this.getFrameDom(script, url);
const originLocation = await this.getLocation();

const framesCount = await this._switchToFrame(iframeXpath);

const locationAfterSwitch = await this.getLocation();
if (locationAfterSwitch === originLocation) {
this._logger.verbose('Switching to frame failed');
domIFrame = {};
} else {
domIFrame = await this.getFrameDom(script, url);
await this._switchToParentFrame(framesCount);
}
} catch (ignored) {
domIFrame = {};
}
await this._switchToParentFrame(framesCount);
domSnapshot = domSnapshot.replace(`"${separatorJson.iframeStartToken}${iframeXpath}${separatorJson.iframeEndToken}"`, domIFrame);
}
}

return domSnapshot;
}

async getLocation() {
return await this._driver.executeScript(DOCUMENT_LOCATION_HREF_SCRIPT);
}

/**
* @param {string|string[]} xpaths
* @return {Promise<number>}
Expand Down

0 comments on commit 12667b0

Please sign in to comment.