Skip to content

Commit

Permalink
feat(openTab): Make passing in a URL optional
Browse files Browse the repository at this point in the history
- Defaults to about:blank
  • Loading branch information
vinsonchuong committed Mar 8, 2023
1 parent c65976e commit e861271
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
16 changes: 9 additions & 7 deletions actions/open-tab/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# `openTab(browser, url, options)`
Opens a url in a new tab and waits for it to fully load
# `openTab(browser, url?, options?)`
Opens new tab, optionally navigating to a URL and waiting for it to fully load

## Example
```js
Expand All @@ -22,7 +22,7 @@ run()

## Parameters
* `browser` ([Browser](../../interface#browser-object))
* `url` (string)
* `url` (optional string)
* `options = {}` (object)
* `timeout` (number): Number of milliseconds to wait for the page to load
before throwing an error
Expand All @@ -31,7 +31,9 @@ run()
* `tab` ([Promise<Tab>](../../interface#tab-object))

## Details
`openTab` waits until the page is fully loaded and has not made a network
request for `500ms`. Note that WebSocket connections are ignored. By default,
`openTab` will wait at most 5 seconds and then throw an error. Set a custom
timeout by passing in the `timeout` option.
When not given any arguments, `openTab` defaults to `about:blank`.

When given a URL, `openTab` waits until the page is fully loaded and has not
made a network request for `500ms`. Note that WebSocket connections are ignored.
By default, `openTab` will wait at most 5 seconds and then throw an error. Set a
custom timeout by passing in the `timeout` option.
4 changes: 3 additions & 1 deletion actions/open-tab/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ export default async function ({puppeteer: {browser}}, url, options) {
const page = await browser.newPage()
const tab = makeTab(browser, page)

await (options ? navigate(tab, url, options) : navigate(tab, url))
if (url) {
await (options ? navigate(tab, url, options) : navigate(tab, url))
}

return tab
}
9 changes: 9 additions & 0 deletions actions/open-tab/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,15 @@ test('allowing the navigation timeout to be set', async (t) => {
}
})

test('opening empty tabs', async (t) => {
const {browser} = t.context

const tab = await openTab(browser)
t.is((await browser.puppeteer.browser.pages()).length, 1)

t.is(tab.puppeteer.page.url(), 'about:blank')
})

test('collecting console messages', async (t) => {
const {browser, directory} = t.context

Expand Down

0 comments on commit e861271

Please sign in to comment.