Skip to content

Commit

Permalink
addscript method
Browse files Browse the repository at this point in the history
  • Loading branch information
angrykoala committed Oct 13, 2018
1 parent a72be43 commit 905244b
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 22 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
1.5.0 / ####-##-##
==================

* AddScript method

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

Expand Down
5 changes: 5 additions & 0 deletions 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
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"
]
}
};
30 changes: 13 additions & 17 deletions lib/browser_core.js
Original file line number Diff line number Diff line change
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 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 905244b

Please sign in to comment.