From eda021a9adea9d6b63a5131609aa8865471b06f8 Mon Sep 17 00:00:00 2001 From: gidztech Date: Thu, 17 Jan 2019 23:40:16 +0000 Subject: [PATCH] #3 Replace waitForResource internals with native API, add WIP test for Extensions --- docs/classes/extensions.html | 58 ++++++++++++++++++------------------ docs/classes/hackydate.html | 6 ++-- docs/globals.html | 6 ++-- docs/index.html | 6 ++-- src/Extensions.spec.ts | 47 +++++++++++++++++++++++++++++ src/Extensions.ts | 33 ++++---------------- src/Utils.spec.ts | 22 ++++++-------- src/Utils.ts | 4 +-- 8 files changed, 101 insertions(+), 81 deletions(-) create mode 100644 src/Extensions.spec.ts diff --git a/docs/classes/extensions.html b/docs/classes/extensions.html index 4d42079..c5647d8 100644 --- a/docs/classes/extensions.html +++ b/docs/classes/extensions.html @@ -1080,7 +1080,7 @@

constructor

  • Parameters

    @@ -1105,7 +1105,7 @@

    Private defaultTimeout

    defaultTimeout: number @@ -1115,7 +1115,7 @@

    Private puppeteerPage

    puppeteerPage: Page
    @@ -1125,7 +1125,7 @@

    Private resourceRequestsresourceRequests: Request[] = [] @@ -1142,7 +1142,7 @@

    evaluate

  • @@ -1191,7 +1191,7 @@

    fastForwardTime

  • @@ -1222,7 +1222,7 @@

    getPropertyValue

  • @@ -1262,7 +1262,7 @@

    getText

  • @@ -1296,7 +1296,7 @@

    getValue

  • @@ -1330,7 +1330,7 @@

    isElementFocused

  • @@ -1364,7 +1364,7 @@

    resetResourceRequests

  • @@ -1386,7 +1386,7 @@

    turnOffAnimations

  • @@ -1408,7 +1408,7 @@

    waitFor

  • @@ -1439,7 +1439,7 @@

    waitForDocumentTitle

  • @@ -1470,7 +1470,7 @@

    waitForElementCount

  • @@ -1507,7 +1507,7 @@

    waitForFunction

  • @@ -1567,7 +1567,7 @@

    waitForLoadedWebFontCountToBe

  • @@ -1604,7 +1604,7 @@

    waitForNthSelectorAttribute

  • @@ -1647,7 +1647,7 @@

    waitForNthSelectorAttributeValue

  • @@ -1690,13 +1690,13 @@

    Returns Promise

    waitForResource

      -
    • waitForResource(resource: string, timeout?: number): Promise<Object>
    • +
    • waitForResource(resource: string, timeout?: number): Promise<Response>
    -

    Returns Promise<Object>

    +

    Returns Promise<Response>

  • @@ -1733,7 +1733,7 @@

    waitForSelectorAttribute

  • @@ -1770,7 +1770,7 @@

    waitForSelectorAttributeValue

  • @@ -1813,7 +1813,7 @@

    waitForUrl

  • @@ -1844,7 +1844,7 @@

    waitUntilExistsAndVisible

  • @@ -1880,7 +1880,7 @@

    waitUntilSelectorHasVisibleContent

  • @@ -1911,7 +1911,7 @@

    waitWhileExistsAndVisible

  • @@ -1947,7 +1947,7 @@

    waitWhileSelectorHasVisibleContent

  • diff --git a/docs/classes/hackydate.html b/docs/classes/hackydate.html index 037b44c..1351eb8 100644 --- a/docs/classes/hackydate.html +++ b/docs/classes/hackydate.html @@ -984,7 +984,7 @@

    constructor

  • Parameters

    @@ -1006,7 +1006,7 @@

    date

    date: Date
    @@ -1023,7 +1023,7 @@

    now

  • Returns number

    diff --git a/docs/globals.html b/docs/globals.html index 26b22fd..795f453 100644 --- a/docs/globals.html +++ b/docs/globals.html @@ -981,7 +981,7 @@

    Const DEFAULT_TIMEOUT_M
    DEFAULT_TIMEOUT_MS: number = 5000
    @@ -998,7 +998,7 @@

    Const
    @@ -1033,7 +1033,7 @@

    serializeFunctionWithArgs

  • diff --git a/docs/index.html b/docs/index.html index d253334..102a3ff 100644 --- a/docs/index.html +++ b/docs/index.html @@ -982,7 +982,7 @@

    Const DEFAULT_TIMEOUT_M
    DEFAULT_TIMEOUT_MS: number = 5000
    @@ -999,7 +999,7 @@

    Const
    @@ -1034,7 +1034,7 @@

    serializeFunctionWithArgs

  • diff --git a/src/Extensions.spec.ts b/src/Extensions.spec.ts new file mode 100644 index 0000000..a090b26 --- /dev/null +++ b/src/Extensions.spec.ts @@ -0,0 +1,47 @@ +import * as puppeteer from 'puppeteer'; +import Extensions from './Extensions'; + +jest.setTimeout(10000); + +describe('Extensions', () => { + describe('waitForResource', () => { + let browser; + + beforeAll(async () => { + browser = await puppeteer.launch(); + }); + + it('should respond with true if resource response is found', async () => { + const page = await browser.newPage(); + + const extensions = new Extensions(page, 10000); + const response = extensions.waitForResource('main'); + + await page.goto('https://www.gideonpyzer.com'); + + await response; + expect(response).toBeTruthy(); + }); + + it('should timeout when waiting for resource that will not be requested for', async () => { + const page = await browser.newPage(); + + const extensions = new Extensions(page, 10000); + const response = extensions.waitForResource('bla', 500); + + await page.goto('https://www.gideonpyzer.com'); + + try { + await response; + } catch (e) { + expect(e.message).toBe( + 'Timeout exceeded while waiting for event' + ); + } + }); + + afterAll(async () => { + browser.close(); + }); + }); +}); diff --git a/src/Extensions.ts b/src/Extensions.ts index d227640..1c70e02 100644 --- a/src/Extensions.ts +++ b/src/Extensions.ts @@ -27,33 +27,12 @@ export default class Extensions { resource: string, timeout: number = this.defaultTimeout ) { - return new Promise((resolve, reject) => { - const resourceRequestHasResponded = (): boolean => { - const resourceRequest = this.resourceRequests.find( - r => r.url().indexOf(resource) !== -1 - ); - if (resourceRequest) { - return isSuccessfulResponse(resourceRequest); - } - - return false; - }; - - if (resourceRequestHasResponded()) { - resolve(); - } else { - pollFor({ - checkFn: async () => { - return resourceRequestHasResponded(); - }, - interval: 100, - timeout, - timeoutMsg: 'Timeout waiting for resource match.' - }) - .then(resolve) - .catch(reject); - } - }); + return this.puppeteerPage.waitForResponse( + response => + response.url().indexOf(resource) > -1 && + isSuccessfulResponse(response), + { timeout } + ); } /** diff --git a/src/Utils.spec.ts b/src/Utils.spec.ts index e608aac..609df67 100644 --- a/src/Utils.spec.ts +++ b/src/Utils.spec.ts @@ -1,27 +1,23 @@ -import { Request } from 'puppeteer'; +import { Response } from 'puppeteer'; import { isSuccessfulResponse, pollFor } from './Utils'; describe('Utils', () => { describe('isSuccessfulResponse', () => { const mockRequestWithStatus = (status: number) => { - return jest.fn(() => ({ - response: jest.fn(() => { - return { - status: () => status - }; - }) + return jest.fn(() => ({ + status: () => status })); }; it('correctly determines successful response', () => { - let MockRequest = mockRequestWithStatus(200); - expect(isSuccessfulResponse(new MockRequest())).toBe(true); - MockRequest = mockRequestWithStatus(304); - expect(isSuccessfulResponse(new MockRequest())).toBe(true); + let MockResponse = mockRequestWithStatus(200); + expect(isSuccessfulResponse(new MockResponse())).toBe(true); + MockResponse = mockRequestWithStatus(304); + expect(isSuccessfulResponse(new MockResponse())).toBe(true); }); it('correctly determines unsuccessful response', () => { - const MockRequest = mockRequestWithStatus(500); - expect(isSuccessfulResponse(new MockRequest())).toBe(false); + const MockResponse = mockRequestWithStatus(500); + expect(isSuccessfulResponse(new MockResponse())).toBe(false); }); }); diff --git a/src/Utils.ts b/src/Utils.ts index e9f2416..bc119c4 100644 --- a/src/Utils.ts +++ b/src/Utils.ts @@ -3,9 +3,7 @@ import { Request, Response } from 'puppeteer'; /** * @hidden */ -const isSuccessfulResponse = (request: Request): boolean => { - const response: Response | null = request.response(); - +const isSuccessfulResponse = (response: Response): boolean => { if (response) { return response.status() === 200 || response.status() === 304; }