Skip to content

Commit

Permalink
feat(testing): expose waitFor option
Browse files Browse the repository at this point in the history
  • Loading branch information
manucorporat committed Jun 20, 2019
1 parent 0b4d814 commit 62839d8
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
6 changes: 3 additions & 3 deletions src/testing/puppeteer/puppeteer-declarations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as d from '../../declarations';
import * as puppeteer from 'puppeteer';


export interface NewE2EPageOptions {
export interface NewE2EPageOptions extends puppeteer.NavigationOptions {
url?: string;
html?: string;
}
Expand Down Expand Up @@ -80,15 +80,15 @@ export interface E2EPage extends PuppeteerPage {
* a localhost address. A shortcut to `page.goto(url)` is to set the `url` option
* when creating a new page, such as `const page = await newE2EPage({ url })`.
*/
goTo(url: string, options?: Partial<puppeteer.NavigationOptions>): Promise<puppeteer.Response | null>;
goTo(url: string, options?: puppeteer.NavigationOptions): Promise<puppeteer.Response | null>;

/**
* Instead of testing a url directly, html content can be mocked using
* `page.setContent(html)`. A shortcut to `page.setContent(html)` is to set
* the `html` option when creating a new page, such as
* `const page = await newE2EPage({ html })`.
*/
setContent(html: string): Promise<void>;
setContent(html: string, options?: puppeteer.NavigationOptions): Promise<void>;

/**
* Used to test if an event was, or was not dispatched. This method
Expand Down
20 changes: 14 additions & 6 deletions src/testing/puppeteer/puppeteer-page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,13 @@ export async function newE2EPage(opts: pd.NewE2EPageOptions = {}): Promise<pd.E2
page.on('requestfailed', requestFailed);

if (typeof opts.html === 'string') {
const errMsg = await e2eSetContent(page, opts.html);
const errMsg = await e2eSetContent(page, opts.html, { waitUntil: opts.waitUntil });
if (errMsg) {
throw errMsg;
}

} else if (typeof opts.url === 'string') {
const errMsg = await e2eGoTo(page, opts.url);
const errMsg = await e2eGoTo(page, opts.url, { waitUntil: opts.waitUntil });
if (errMsg) {
throw errMsg;
}
Expand Down Expand Up @@ -93,7 +93,7 @@ function failedPage() {
return page;
}

async function e2eGoTo(page: pd.E2EPageInternal, url: string) {
async function e2eGoTo(page: pd.E2EPageInternal, url: string, options: puppeteer.NavigationOptions) {
try {
if (page.isClosed()) {
return 'e2eGoTo unavailable: page already closed';
Expand All @@ -120,7 +120,10 @@ async function e2eGoTo(page: pd.E2EPageInternal, url: string) {

const fullUrl = browserUrl + url.substring(1);

const rsp = await page._e2eGoto(fullUrl, { waitUntil: 'networkidle0' });
if (!options.waitUntil) {
options.waitUntil = DEFAULT_WAIT_FOR;
}
const rsp = await page._e2eGoto(fullUrl, options);

if (!rsp.ok()) {
await closePage(page);
Expand All @@ -140,7 +143,7 @@ async function e2eGoTo(page: pd.E2EPageInternal, url: string) {
}


async function e2eSetContent(page: pd.E2EPageInternal, html: string) {
async function e2eSetContent(page: pd.E2EPageInternal, html: string, options: puppeteer.NavigationOptions = {}) {
try {
if (page.isClosed()) {
return 'e2eSetContent unavailable: page already closed';
Expand Down Expand Up @@ -180,7 +183,10 @@ async function e2eSetContent(page: pd.E2EPageInternal, html: string) {
}
});

const rsp = await page._e2eGoto(url, { waitUntil: 'networkidle0' });
if (!options.waitUntil) {
options.waitUntil = DEFAULT_WAIT_FOR;
}
const rsp = await page._e2eGoto(url, options);

if (!rsp.ok()) {
await closePage(page);
Expand Down Expand Up @@ -310,3 +316,5 @@ function pageError(e: any) {
function requestFailed(req: puppeteer.Request) {
console.error('requestfailed', req.url());
}

const DEFAULT_WAIT_FOR = 'load';

0 comments on commit 62839d8

Please sign in to comment.