Files
Tim NeutkensandGitHub e860cec656 test: migrate webdriver callers to next.browser (#93941)
### What?

Migrate remaining direct `next-webdriver` test callers that have a
`NextInstance` to `next.browser()`, and expose the shared `Playwright`
browser type from `e2e-utils`.

### Why?

`NextInstance.browser` should be the supported browser-opening interface
for test fixtures, with `next-webdriver` kept as the private
implementation detail.

### How?

Updated affected development, e2e, and production tests to call
`next.browser()` directly, passing `baseUrl` where tests intentionally
target a manually spawned or proxied server. Shared helpers now receive
browser callbacks from the test context, and browser types import
`Playwright` from `e2e-utils` instead of deriving from `next.browser` or
importing from private paths.

<!-- NEXT_JS_LLM_PR -->
2026-05-22 14:01:58 +02:00

87 lines
2.6 KiB
TypeScript

import { FileRef, nextTestSetup } from 'e2e-utils'
import { renderViaHTTP } from 'next-test-utils'
import cheerio from 'cheerio'
import path from 'path'
async function matchLogs(browser, includes: string) {
let found = false
const browserLogs = await browser.log()
browserLogs.forEach((log) => {
if (log.message.includes(includes)) {
found = true
}
})
return found
}
const appDir = path.join(__dirname, 'app')
describe('New Link Behavior', () => {
const { next } = nextTestSetup({
files: {
pages: new FileRef(path.join(appDir, 'pages')),
'next.config.js': new FileRef(path.join(appDir, 'next.config.js')),
},
dependencies: {},
})
it('should render link with <a>', async () => {
const html = await renderViaHTTP(next.url, '/')
const $ = cheerio.load(html)
const $a = $('a')
expect($a.text()).toBe('About')
expect($a.attr('href')).toBe('/about')
})
it('should navigate to /about', async () => {
const browser = await next.browser(`/`)
await browser.elementByCss('a').click().waitForElementByCss('#about-page')
const text = await browser.elementByCss('h1').text()
expect(text).toBe('About Page')
})
it('should handle onclick', async () => {
const browser = await next.browser(`/onclick`)
await browser.elementByCss('a').click().waitForElementByCss('h1')
const text = await browser.elementByCss('h1').text()
expect(text).toBe('Home Page')
expect(await matchLogs(browser, 'link to home clicked')).toBe(true)
})
it('should handle preventdefault', async () => {
const browser = await next.browser(`/onclick-prevent-default`)
await browser.elementByCss('a').click()
const text = await browser.elementByCss('h1').text()
expect(text).toBe('Onclick prevent default')
expect(await matchLogs(browser, 'link to home clicked but prevented')).toBe(
true
)
})
it('should render link with id', async () => {
const html = await renderViaHTTP(next.url, '/id-pass-through')
const $ = cheerio.load(html)
const $a = $('a')
expect($a.attr('id')).toBe('home-link')
})
it('should render link with classname', async () => {
const html = await renderViaHTTP(next.url, '/classname-pass-through')
const $ = cheerio.load(html)
const $a = $('a')
expect($a.attr('class')).toBe('home-link')
})
it('should render link with multiple children', async () => {
const html = await renderViaHTTP(next.url, '/multiple-children')
const $ = cheerio.load(html)
const $a = $('a')
expect($a.text()).toBe('About Additional Children')
expect($a.find('strong').text()).toBe('Additional Children')
})
})