Skip to content

Commit

Permalink
feat: wait for an element in an iframe. (#174)
Browse files Browse the repository at this point in the history
* feat: wait for an element in an iframe.

* test: add wait for iframe element tests, and refactored.

* fix: e2e tests.

* refactor: iframe tests.
  • Loading branch information
shendriksen authored Feb 25, 2019
1 parent cab8e2f commit 5be2c28
Show file tree
Hide file tree
Showing 10 changed files with 153 additions and 156 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ Example config to run Aye Spy:
}
],
"waitForElement": "#section-news", // explicitly wait for a selector to be visible before snap
"waitForIFrameElement": {
"frame": ".iframe", // the id of the iframe that you would like to change to.
"element": ".element-in-iframe" // the element in the iframe that you would like to wait for.
}
"onReadyScript": "./scripts/clickSelector.js", // run a script before snap
"wait": 2000 // implicitly wait before taking a snap
}
Expand Down
Binary file not shown.
3 changes: 3 additions & 0 deletions e2eTests/generic/onBeforeSuiteScript/onBeforeSuiteScript.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = function() {
console.log('Script has Run!');
};
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,4 @@ describe('e2e Tests running onBeforeSuiteScript', () => {
expect(stdout).toEqual(expect.stringContaining('Script has Run!'));
expect(exitCode).toEqual(0);
});

});
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"report": "./e2eTests/generic/onBeforeSuiteScript/reports",
"remoteBucketName": "aye-spy",
"remoteRegion": "eu-west-1",
"onBeforeSuiteScript": "./e2eTests/generic/onBeforeSuiteScript/script.js",
"onBeforeSuiteScript": "./e2eTests/generic/onBeforeSuiteScript/onBeforeSuiteScript.js",
"scenarios": [
{
"url": "http://ayespy_report:4000",
Expand Down
8 changes: 0 additions & 8 deletions e2eTests/generic/onBeforeSuiteScript/script.js

This file was deleted.

5 changes: 4 additions & 1 deletion src/__mocks__/selenium-webdriver.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class Builder {
return wrap;
},
findElement: element => new Element(element),
switchTo: jest.fn().mockReturnValue({ defaultContent: jest.fn() }),
setRect: jest.fn(),
getRect: jest.fn(),
get: jest.fn(),
Expand All @@ -55,7 +56,9 @@ const By = {
};

const until = {
elementIsVisible: selector => selector
elementIsVisible: selector => selector,
elementLocated: jest.fn(),
ableToSwitchToFrame: () => true
};

export default {
Expand Down
1 change: 1 addition & 0 deletions src/getScreenshots.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const generateSnapShotPromises = (SnapShotter, config) => {
removeElements: scenario.removeElements,
hideElements: scenario.hideElements,
waitForElement: scenario.waitForElement,
waitForIFrameElement: scenario.waitForIFrameElement,
url: scenario.url,
onBeforeScript: scenario.onBeforeScript,
onReadyScript: scenario.onReadyScript,
Expand Down
37 changes: 37 additions & 0 deletions src/snapshotter.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export default class SnapShotter {
removeElements,
hideElements,
waitForElement,
waitForIFrameElement,
wait,
url = 'http://localhost:80',
viewportLabel = 'viewportLabel',
Expand All @@ -42,6 +43,7 @@ export default class SnapShotter {
this._removeElements = removeElements;
this._hideElements = hideElements;
this._waitForElement = waitForElement;
this._waitForIFrameElement = waitForIFrameElement;
this._url = url;
this.wait = wait;
this._onBeforeScript = onBeforeScript;
Expand Down Expand Up @@ -133,6 +135,39 @@ export default class SnapShotter {
}
}

switchToIFrame(selector, timeout) {
return this.driver.wait(
this._until.ableToSwitchToFrame(this._By.css(selector)),
timeout
);
}

waitToLocateElement(selector, timeout) {
return this.driver.wait(
this._until.elementLocated(this._By.css(selector)),
timeout
);
}

async waitForIFrameElement() {
const timeout = 10000;

const { frame, element } = this._waitForIFrameElement;

try {
await this.switchToIFrame(frame, timeout);
await this.waitToLocateElement(element, timeout);
} catch (error) {
console.log(''); // eslint-disable-line no-console // space for progress bar
logger.error(
'snapshotter',
`❌ Unable to find the specified waitForIFrameElement element on the page! ❌ ${error}`
);
} finally {
await this.driver.switchTo().defaultContent();
}
}

getElementDimensions(selector) {
return this._driver
.findElement(this._By.css(selector))
Expand Down Expand Up @@ -210,6 +245,8 @@ export default class SnapShotter {

if (this._waitForElement) await this.waitForElement();

if (this._waitForIFrameElement) await this.waitForIFrameElement();

if (this._onReadyScript)
await executeScriptWithDriver(this._driver, this._onReadyScript).catch(
this.handleScriptError
Expand Down
Loading

0 comments on commit 5be2c28

Please sign in to comment.