Reputation: 2499
When using puppeteer, i used to get new tab by using this lines of code:
const browser = await puppeteer.launch()
const [page] = await browser.pages()
await page.goto('http://example.com')
The main purpose of this is the fewer tabs number, my app is running lighter. But when i using playwright, it seems that the context isn't contain any page yet.
const browser = await playwright.chromium.launch()
const context = await browser.newContext()
const [page] = await context.pages()
await page.goto('http://example.com')
My code is running, but i keep getting this error message:
(node:47248) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'goto' of undefined
Am i the only one who getting this kind of error?
Upvotes: 2
Views: 2830
Reputation: 21617
That's the same behavior you would get in puppeteer if you use createIncognitoBrowserContext
.
const browser = await puppeteer.launch();
const context = await browser.createIncognitoBrowserContext();
const [page] = await context.pages(); //Page is null here
await page.goto('http://example.com');
Both createIncognitoBrowserContext
in puppeteer and newContext
in playwright are created with no pages.
As you mentioned in your answer, you could use the default context or call newPage
in the context you just created.
Upvotes: 1
Reputation: 2499
After trying to get this error gone, i'm getting the code to be like this:
const browser = await playwright.chromium.launch()
const context = await browser.defaultContext()
const [page] = await context.pages()
await page.goto('http://example.com')
I change newContext()
to defaultContext()
.
Upvotes: 0