diff --git a/CHANGELOG.md b/CHANGELOG.md index c7aaafb8..fd13172b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 ================== diff --git a/README.md b/README.md index 77d0ef3b..9875f2be 100644 --- a/README.md +++ b/README.md @@ -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. @@ -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 } diff --git a/config.js b/config.js index 13ca12d3..eee2cbaf 100644 --- a/config.js +++ b/config.js @@ -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" + ] } }; diff --git a/lib/browser_core.js b/lib/browser_core.js index 1e113408..7b1260f3 100644 --- a/lib/browser_core.js +++ b/lib/browser_core.js @@ -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}`)); }); } @@ -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"); } @@ -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); } diff --git a/package-lock.json b/package-lock.json index 8a608d13..22be0c55 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "wendigo", - "version": "1.4.1", + "version": "1.4.2", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 4d37737a..f4e1a722 100644 --- a/package.json +++ b/package.json @@ -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" diff --git a/tests/browser/open.test.js b/tests/browser/open.test.js index ea7b36c7..5efb53ab 100644 --- a/tests/browser/open.test.js +++ b/tests/browser/open.test.js @@ -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() => { diff --git a/tests/browser/plugins.test.js b/tests/browser/plugins.test.js index a2628767..02ea1721 100644 --- a/tests/browser/plugins.test.js +++ b/tests/browser/plugins.test.js @@ -12,6 +12,7 @@ describe("Plugins", () => { this.browser = browser; this.beforeOpenCalled = false; this.beforeCloseCalled = false; + this.afterOpenCalled = false; } myMethod() { @@ -25,6 +26,10 @@ describe("Plugins", () => { _beforeClose() { this.beforeCloseCalled = true; } + + _afterOpen() { + this.afterOpenCalled = true; + } } class AssertionPluginTest { @@ -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); diff --git a/tests/browser/wait_for_request.test.js b/tests/browser/wait_for_request.test.js index 8fe83eac..5ebce95e 100644 --- a/tests/browser/wait_for_request.test.js +++ b/tests/browser/wait_for_request.test.js @@ -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"); });