This repository has been archived by the owner on Sep 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: adds clickAndWait helper function
- Loading branch information
Showing
5 changed files
with
61 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { Browser } from 'puppeteer-core' | ||
import { clickAndWait } from './helpers' | ||
import { launch } from './puppeteer' | ||
|
||
jest.setTimeout(20 * 1000) | ||
|
||
describe('helpers', () => { | ||
describe(clickAndWait.name, () => { | ||
it('should click and wait', async () => { | ||
expect.assertions(1) | ||
|
||
const browser = await launch() | ||
|
||
try { | ||
const page = await browser.newPage() | ||
await page.goto('http://example.com/', { waitUntil: 'networkidle2' }) | ||
|
||
const res = await clickAndWait(page, 'a') | ||
|
||
expect(res.ok()).toBe(true) | ||
} catch (e) { | ||
expect(e).toBeFalsy() | ||
} finally { | ||
return browser.close() | ||
} | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { Page, ClickOptions, NavigationOptions } from 'puppeteer-core' | ||
|
||
/** | ||
* Clicks a link and waits for network request to complete. | ||
*/ | ||
export async function clickAndWait( | ||
page: Page, | ||
selector: string, | ||
clickOptions: ClickOptions = {}, | ||
navigationOptions: NavigationOptions = { | ||
waitUntil: 'networkidle2', | ||
}, | ||
) { | ||
const wait = page.waitForNavigation(navigationOptions) | ||
|
||
const click = page.click(selector, clickOptions) | ||
|
||
const [response] = await Promise.all([wait, click]) | ||
|
||
return response | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,12 @@ | ||
import { launch } from './index' | ||
import { launch, clickAndWait } from './index' | ||
import { launch as puppeteerLaunch } from './puppeteer' | ||
|
||
describe('index', () => { | ||
it('should export our custom launch function', () => { | ||
expect(launch).toEqual(puppeteerLaunch) | ||
}) | ||
|
||
it(`should export ${clickAndWait.name} helper`, () => { | ||
expect(clickAndWait).toBeTruthy() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from 'puppeteer-core' | ||
export { launch } from './puppeteer' | ||
export * from './helpers' |