Skip to content

Commit

Permalink
Merge pull request #382 from angrykoala/dev
Browse files Browse the repository at this point in the history
Wendigo 2.1.3
  • Loading branch information
angrykoala authored Jun 21, 2019
2 parents 0d0f0d0 + 41ab96a commit e133048
Show file tree
Hide file tree
Showing 21 changed files with 299 additions and 124 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
2.1.3/ 2018-06-21
=================

* Internal Puppeteer wrapper
* Puppeteer updated to 1.18.0

2.1.2 / 2018-06-06
==================

Expand Down
4 changes: 2 additions & 2 deletions lib/browser/browser.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import BrowserTap from './mixins/browser_tap';
import { Page } from 'puppeteer';
import PuppeteerPage from './puppeteer_wrapper/puppeteer_page';
import { FinalBrowserSettings } from '../types';

// Modules
Expand All @@ -18,7 +18,7 @@ export default class Browser extends BrowserTap {
public readonly webworkers: BrowserWebworker;
public readonly dialog: BrowserDialog;

constructor(page: Page, settings: FinalBrowserSettings, components: Array<string> = []) {
constructor(page: PuppeteerPage, settings: FinalBrowserSettings, components: Array<string> = []) {
components = components.concat(["cookies", "localStorage", "requests", "console", "webworkers", "dialog"]);
super(page, settings, components);
this.cookies = new BrowserCookies(this);
Expand Down
56 changes: 31 additions & 25 deletions lib/browser/browser_core.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,27 @@
import path from 'path';
import querystring from 'querystring';
import { ConsoleMessage, Page, Response, Viewport, Frame } from 'puppeteer';
import { ConsoleMessage, Page, Response, Frame } from 'puppeteer';

import { stringifyLogText } from '../utils/puppeteer_utils';
import WendigoConfig from '../../config';
import DomElement from '../models/dom_element';
import { FatalError, InjectScriptError } from '../errors';
import { FinalBrowserSettings, OpenSettings } from '../types';
import PuppeteerPage from './puppeteer_wrapper/puppeteer_page';
import { ViewportOptions } from './puppeteer_wrapper/puppeteer_types';

const injectionScriptsPath = WendigoConfig.injectionScripts.path;
const injectionScripts = WendigoConfig.injectionScripts.files;

async function pageLog(log: ConsoleMessage): Promise<void> {
const text = await stringifyLogText(log);
let logType = log.type() as string;
if (logType === 'warning') logType = 'warn';
const con = console as any;
if (!(con[logType])) logType = 'log';
con[logType](text);
async function pageLog(log?: ConsoleMessage): Promise<void> {
if (log) {
const text = await stringifyLogText(log);
let logType = log.type() as string;
if (logType === 'warning') logType = 'warn';
const con = console as any;
if (!(con[logType])) logType = 'log';
con[logType](text);
}
}

const defaultOpenOptions: OpenSettings = {
Expand All @@ -29,28 +33,28 @@ const defaultOpenOptions: OpenSettings = {
};

export default abstract class BrowserCore {
public page: Page;
public initialResponse: Response | null;

protected _page: PuppeteerPage;
protected originalHtml?: string;
protected settings: FinalBrowserSettings;

private _loaded: boolean;
private disabled: boolean;
private components: Array<string>;

constructor(page: Page, settings: FinalBrowserSettings, components: Array<string> = []) {
this.page = page;
constructor(page: PuppeteerPage, settings: FinalBrowserSettings, components: Array<string> = []) {
this._page = page;
this.settings = settings;
this._loaded = false;
this.initialResponse = null;
this.disabled = false;
this.components = components;
if (this.settings.log) {
this.page.on("console", pageLog);
this._page.on("console", pageLog);
}

this.page.on('load', async (): Promise<void> => {
this._page.on('load', async (): Promise<void> => {
if (this._loaded) {
try {
await this._afterPageLoad();
Expand All @@ -61,6 +65,9 @@ export default abstract class BrowserCore {
});
}

public get page(): Page {
return this._page.page;
}
public get loaded(): boolean {
return this._loaded && !this.disabled;
}
Expand All @@ -79,7 +86,7 @@ export default abstract class BrowserCore {
}
try {
await this._beforeOpen(options);
const response = await this.page.goto(url);
const response = await this._page.goto(url);
this.initialResponse = response;
return this._afterPageLoad();
} catch (err) {
Expand All @@ -106,7 +113,7 @@ export default abstract class BrowserCore {
this.originalHtml = undefined;
try {
await p;
await this.page.browser().close();
await this._page.browser().close();
} catch (err) {
return Promise.reject(new FatalError("close", `Failed to close browser. ${err.message}`));
}
Expand All @@ -115,20 +122,19 @@ export default abstract class BrowserCore {
public async evaluate(cb: (...args: Array<any>) => any, ...args: Array<any>): Promise<any> {
this._failIfNotLoaded("evaluate");
args = this._setupEvaluateArguments(args);
const rawResult = await this.page.evaluateHandle(cb, ...args);
const rawResult = await this._page.evaluateHandle(cb, ...args);
const resultAsElement = rawResult.asElement();
if (resultAsElement) {
return new DomElement(resultAsElement);
} else return rawResult.jsonValue();
}

public setViewport(config = {}): Promise<void> {
const finalConfig = Object.assign({}, this.page.viewport(), config) as Viewport;
return this.page.setViewport(finalConfig);
public setViewport(config: ViewportOptions = {}): Promise<void> {
return this._page.setViewport(config);
}

public frames(): Array<Frame> {
return this.page.frames();
return this._page.frames();
}

public async mockDate(date: Date, options = { freeze: true }): Promise<void> {
Expand All @@ -146,7 +152,7 @@ export default abstract class BrowserCore {
public async addScript(scriptPath: string): Promise<void> {
this._failIfNotLoaded("addScript");
try {
await this.page.addScriptTag({
await this._page.addScriptTag({
path: scriptPath
});
} catch (err) {
Expand All @@ -168,19 +174,19 @@ export default abstract class BrowserCore {

protected async _beforeOpen(options: OpenSettings): Promise<void> {
if (this.settings.userAgent) {
await this.page.setUserAgent(this.settings.userAgent);
await this._page.setUserAgent(this.settings.userAgent);
}

if (this.settings.bypassCSP) {
await this.page.setBypassCSP(true);
await this._page.setBypassCSP(true);
}
await this.setViewport(options.viewport);
await this._callComponentsMethod("_beforeOpen", options);
}

protected async _afterPageLoad(): Promise<void> {
try {
const content = await this.page.content();
const content = await this._page.content();
this.originalHtml = content;
await this._addJsScripts();
} catch (err) {
Expand All @@ -192,7 +198,7 @@ export default abstract class BrowserCore {

private async _addJsScripts(): Promise<void> {
const promises = injectionScripts.map((s) => {
return this.page.addScriptTag({ // Not using wrapper as this is before loaded is true
return this._page.addScriptTag({ // Not using wrapper as this is before loaded is true
path: path.join(injectionScriptsPath, s)
});
});
Expand Down
8 changes: 4 additions & 4 deletions lib/browser/mixins/browser_actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export default abstract class BrowserActions extends BrowserQueries {
try {
for (let i = 0; i < count; i++) {
for (const k of keys) {
await this.page.keyboard.press(k);
await this._page.keyboard.press(k);
}
}
} catch (err) {
Expand Down Expand Up @@ -55,7 +55,7 @@ export default abstract class BrowserActions extends BrowserQueries {
if (!element) throw new Error();
cssPath = await this.findCssPath(element);
} else cssPath = selector;
return await this.page.select(cssPath, ...values);
return await this._page.select(cssPath, ...values);
} catch (err) {
throw new QueryError("select", `Element "${selector}" not found.`);
}
Expand Down Expand Up @@ -150,7 +150,7 @@ export default abstract class BrowserActions extends BrowserQueries {

public screenshot(args?: Base64ScreenShotOptions): Promise<string | Buffer> {
this._failIfNotLoaded("screenshot");
return this.page.screenshot(args);
return this._page.screenshot(args);
}

public async screenshotOfElement(selector: WendigoSelector, options?: Base64ScreenShotOptions): Promise<string | Buffer> {
Expand Down Expand Up @@ -180,7 +180,7 @@ export default abstract class BrowserActions extends BrowserQueries {
// const boxFrom = await fromElement.element.boundingBox();
// const boxTo = await toElement.element.boundingBox();
// if (!boxFrom || !boxTo) throw new FatalError("dragAndDrop", "Bounding box not found");
// const mouse = this.page.mouse;
// const mouse = this._page.mouse;
// await mouse.up();
// await mouse.move(boxFrom.x + (boxFrom.width / 2), boxFrom.y + (boxFrom.height / 2));
// await mouse.down();
Expand Down
2 changes: 1 addition & 1 deletion lib/browser/mixins/browser_click.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,6 @@ export default abstract class BrowserClick extends BrowserActions {
}

private _clickCoordinates(x: number, y: number): Promise<void> {
return this.page.mouse.click(x, y);
return this._page.mouse.click(x, y);
}
}
2 changes: 1 addition & 1 deletion lib/browser/mixins/browser_info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export default abstract class BrowserInfo extends BrowserClick {

public title(): Promise<string> {
this._failIfNotLoaded("title");
return this.page.title();
return this._page.title();
}

public html(): string {
Expand Down
10 changes: 5 additions & 5 deletions lib/browser/mixins/browser_navigation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,28 @@ import BrowserEvents from './browser_events';
export default abstract class BrowserNavigation extends BrowserEvents {
public async back(): Promise<void> {
this._failIfNotLoaded("back");
await this.page.goBack();
await this._page.goBack();
await this._afterPageLoad();
}

public async forward(): Promise<void> {
this._failIfNotLoaded("forward");
await this.page.goForward();
await this._page.goForward();
await this._afterPageLoad();
}

public async refresh(): Promise<void> {
this._failIfNotLoaded("refresh");
await this.page.reload();
await this._page.reload();
await this._afterPageLoad();

}

public async waitForPageLoad(): Promise<void> {
await this.page.waitForNavigation({
await this._page.waitForNavigation({
waitUntil: "domcontentloaded"
});
await this.page.waitFor(() => {
await this._page.waitFor(() => {
const w = window as any;
return Boolean(w.WendigoUtils && w.WendigoQuery && w.WendigoPathFinder); // Waits until Wendigo is ready
});
Expand Down
8 changes: 4 additions & 4 deletions lib/browser/mixins/browser_queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ export default abstract class BrowserQueries extends BrowserCore {
if (typeof selector === 'string') {
let elementHandle: ElementHandle | null;
if (isXPathQuery(selector)) {
const results = await this.page.$x(selector);
const results = await this._page.$x(selector);
elementHandle = results[0] || null;
} else elementHandle = await this.page.$(selector);
} else elementHandle = await this._page.$(selector);
result = DomElement.processQueryResult(elementHandle, selector);
} else if (selector instanceof DomElement) result = selector;
else throw new WendigoError("query", "Invalid selector.");
Expand All @@ -34,8 +34,8 @@ export default abstract class BrowserQueries extends BrowserCore {

if (typeof selector === 'string') {
let rawElements: Array<ElementHandle>;
if (isXPathQuery(selector)) rawElements = await this.page.$x(selector);
else rawElements = await this.page.$$(selector);
if (isXPathQuery(selector)) rawElements = await this._page.$x(selector);
else rawElements = await this._page.$$(selector);
result = rawElements.map((e) => {
return DomElement.processQueryResult(e, selector);
}).filter(b => Boolean(b)) as Array<DomElement>;
Expand Down
2 changes: 1 addition & 1 deletion lib/browser/mixins/browser_tap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export default abstract class BrowserTap extends BrowserWait {
}

private async tapCoordinates(x: number, y: number): Promise<number> {
await this.page.touchscreen.tap(x, y);
await this._page.touchscreen.tap(x, y);
return 1;
}
}
4 changes: 2 additions & 2 deletions lib/browser/mixins/browser_wait.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export default abstract class BrowserWait extends BrowserNavigation {
else return e;
});
try {
await this.page.waitFor(selector, {
await this._page.waitFor(selector, {
timeout: timeout,
visible: true
}, ...args);
Expand Down Expand Up @@ -74,7 +74,7 @@ export default abstract class BrowserWait extends BrowserNavigation {
this._failIfNotLoaded("waitForNavigation");
const t1 = new Date().getTime();
try {
await this.page.waitForNavigation({
await this._page.waitForNavigation({
timeout: timeout
});
const t2 = new Date().getTime();
Expand Down
Loading

0 comments on commit e133048

Please sign in to comment.