Skip to content

Commit

Permalink
Merge pull request #222 from angrykoala/dev
Browse files Browse the repository at this point in the history
1.4.2
  • Loading branch information
angrykoala authored Oct 13, 2018
2 parents bc80e35 + ff2cf0f commit cb0535a
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 27 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
1.4.2 / 2018-10-14
==================

* AddScript method
* After Open hook fix
* Minor improvements to some error messages

1.4.1 / 2018-10-13
==================

Expand Down
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,11 @@ elements[0].textContent; // "My first paragraph"

> The DomElement class returned by all query methods provides an interface to Puppeteer's ElementHandle class, it can be accesed with the property `element`
**addScript(scriptPath)**
Executes the given script in the browser context. Useful to set helper methods and functions. This method must be called after the page is already loaded, if another page is loaded, the scripts won't be re-executed. If these scripts are required for a plugin to work, remember to execute this method on the `_afterOpen` hook.

It is heavily recommended to only use this to load helper functions, and not execute anything that might cause side effects. Anything loaded as a script may interfere with the behavior of the page or Wendigo. It is recommended to **always** check if the object of function you are loading already exists before loading, remember that `WendigoUtils` and `WendigoQuery` objects in `window` are required for Wendigo to work, so do not override them.

**class(selector)**
Returns and array with the classes of the first element returned from the given css selector. Throws if no element is found.

Expand Down Expand Up @@ -1269,7 +1274,7 @@ class MyPlugin {
// not on any page loading
}

_afterOpen() { // This hook will be called after the page is opened and _loaded
_afterOpen() { // This hook will be called after the page is opened and loaded
// You can use this hook to start performing actions with evaluate or
// adding custom scripts with this._browser.page.addScriptTag
}
Expand Down
8 changes: 4 additions & 4 deletions config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ const path = require('path');
module.exports = {
injectionScripts: {
path: path.join(__dirname, "injection_scripts"),
files: {
WendigoQuery: "selector_query.js",
WendigoUtils: "wendigo_utils.js"
}
files: [
"selector_query.js",
"wendigo_utils.js"
]
}
};
32 changes: 14 additions & 18 deletions lib/browser_core.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ module.exports = class BrowserCore {
return this._afterPageLoad();
});
}).catch((err) => {
return Promise.reject(new FatalError(`Failed to open ${url}. ${err.message}`));
return Promise.reject(new FatalError(`Failed to open "${url}". ${err.message}`));
});
}

Expand Down Expand Up @@ -94,6 +94,13 @@ module.exports = class BrowserCore {
return this.page.frames();
}

addScript(scriptPath) {
this._failIfNotLoaded();
return this.page.addScriptTag({
path: scriptPath
});
}

_beforeClose() {
return this._callComponentsMethod("_beforeClose");
}
Expand All @@ -120,28 +127,17 @@ module.exports = class BrowserCore {
return this.page.content().then((content) => {
this._originalHtml = content;
return this._addJsScripts().then(() => {
return this._callComponentsMethod("_afterOpen").then(() => {
this._loaded = true;
});
});
});
}

_addScript(key, scriptPath) {
return this.page.evaluate((k) => {
return Boolean(window[k]);
}, key).then((exists) => {
if (exists) return Promise.resolve();
return this.page.addScriptTag({
path: path.join(injectionScriptsPath, scriptPath)
this._loaded = true;
return this._callComponentsMethod("_afterOpen");
});
});
}

_addJsScripts() {
const scripts = Object.keys(injectionScripts);
const promises = scripts.map((s) => {
return this._addScript(s, injectionScripts[s]);
const promises = injectionScripts.map((s) => {
return this.page.addScriptTag({ // Not using wrapper as this is before loaded is true
path: path.join(injectionScriptsPath, s)
});
});
return Promise.all(promises);
}
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "wendigo",
"version": "1.4.1",
"version": "1.4.2",
"description": "A proper monster for front-end automated testing",
"engines": {
"node": ">=8.0.0"
Expand Down
2 changes: 1 addition & 1 deletion tests/browser/open.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ describe("Open", function() {
it("Open Fails", async() => {
await utils.assertThrowsAsync(async() => {
await browser.open("not-a-page");
}, `FatalError: Failed to open not-a-page. Protocol error (Page.navigate): Cannot navigate to invalid URL`);
}, `FatalError: Failed to open "not-a-page". Protocol error (Page.navigate): Cannot navigate to invalid URL`);
});

it("Before Open Fails", async() => {
Expand Down
7 changes: 7 additions & 0 deletions tests/browser/plugins.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ describe("Plugins", () => {
this.browser = browser;
this.beforeOpenCalled = false;
this.beforeCloseCalled = false;
this.afterOpenCalled = false;
}

myMethod() {
Expand All @@ -25,6 +26,10 @@ describe("Plugins", () => {
_beforeClose() {
this.beforeCloseCalled = true;
}

_afterOpen() {
this.afterOpenCalled = true;
}
}

class AssertionPluginTest {
Expand Down Expand Up @@ -78,8 +83,10 @@ describe("Plugins", () => {
assert.ok(browser.pluginTest);
assert.strictEqual(browser.pluginTest.beforeOpenCalled, false);
assert.strictEqual(browser.pluginTest.beforeCloseCalled, false);
assert.strictEqual(browser.pluginTest.afterOpenCalled, false);
await browser.open(configUrls.index);
assert.strictEqual(browser.pluginTest.beforeOpenCalled, true);
assert.strictEqual(browser.pluginTest.afterOpenCalled, true);
assert.strictEqual(browser.pluginTest.beforeCloseCalled, false);
await browser.close();
assert.strictEqual(browser.pluginTest.beforeCloseCalled, true);
Expand Down
2 changes: 1 addition & 1 deletion tests/browser/wait_for_request.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ describe("Wait For Request", function() {
await browser.wait(10);
await browser.assert.request.url(/api/).exactly(1);
await browser.assert.request.url(/api/).responseBody("test").exactly(0);
await browser.waitForResponse("http://localhost:3456/api");
await browser.waitForResponse("http://localhost:3456/api", 1000);
await browser.assert.request.url(/api/).responseBody("test");
});

Expand Down

0 comments on commit cb0535a

Please sign in to comment.