Skip to content
This repository has been archived by the owner on Sep 27, 2023. It is now read-only.

Commit

Permalink
feat: adds clickAndWait helper function
Browse files Browse the repository at this point in the history
  • Loading branch information
moltar authored and Roman committed Apr 14, 2020
1 parent eda9982 commit 179d25b
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 1 deletion.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ const await browser = launch({

```

## Helper Functions

### clickAndWait(page: Page, selector: string, clickOptions?: ClickOptions, navigationOptions?: NavigationOptions)

Clicks and waits for network request to complete and resolves.

## Environment Variables

The following environment variables are available:
Expand Down
28 changes: 28 additions & 0 deletions src/helpers.test.ts
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()
}
})
})
})
21 changes: 21 additions & 0 deletions src/helpers.ts
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
}
6 changes: 5 additions & 1 deletion src/index.test.ts
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()
})
})
1 change: 1 addition & 0 deletions src/index.ts
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'

0 comments on commit 179d25b

Please sign in to comment.