feat: Add Happy DOM location mocking for Playwright tests

- Enhanced setup-browser-globals.js with configurable URL and full location mock
- Created LocationMock helper class for dynamic location mocking in tests
- Integrated LocationMock into ComfyPage fixture with optional setup
- Added example test file demonstrating location mock usage
- Support for mocking location.assign, location.replace, and location.reload methods
This commit is contained in:
snomiao
2025-09-13 09:14:37 +00:00
parent 29d22454f4
commit ead43312f8
4 changed files with 279 additions and 4 deletions

View File

@@ -0,0 +1,96 @@
import { expect } from '@playwright/test'
import { comfyPageFixture as test } from './fixtures/ComfyPage'
import { LocationMock } from './helpers/locationMock'
test.describe('Location Mock Example', () => {
test('should mock location object', async ({ page, comfyPage }) => {
const locationMock = new LocationMock(page)
// Setup location mock before navigating to the page
await locationMock.setupLocationMock({
href: 'http://example.com/test',
pathname: '/test',
search: '?query=value',
hash: '#section'
})
// Navigate to your app
await comfyPage.goto()
// Verify the mock is working
const location = await locationMock.getLocation()
expect(location.pathname).toBe('/test')
expect(location.search).toBe('?query=value')
expect(location.hash).toBe('#section')
// Test navigation
await locationMock.navigateTo('http://example.com/new-page')
const newLocation = await locationMock.getLocation()
expect(newLocation.href).toBe('http://example.com/new-page')
// Test updating specific properties
await locationMock.updateLocation({
pathname: '/updated-path',
search: '?new=param'
})
const updatedLocation = await locationMock.getLocation()
expect(updatedLocation.pathname).toBe('/updated-path')
expect(updatedLocation.search).toBe('?new=param')
})
test('should handle location methods', async ({ page, comfyPage }) => {
const locationMock = new LocationMock(page)
await locationMock.setupLocationMock({
href: 'http://localhost:5173/'
})
await comfyPage.goto()
// Test location.assign
await page.evaluate(() => {
window.location.assign('/new-route')
})
// Check console for mock output
const consoleMessages: string[] = []
page.on('console', (msg) => {
if (msg.text().includes('[Mock]')) {
consoleMessages.push(msg.text())
}
})
await locationMock.navigateTo('/another-route')
await locationMock.replaceTo('/replaced-route')
await locationMock.reload()
// Verify mock methods were called
expect(consoleMessages.some((msg) => msg.includes('location.assign'))).toBeTruthy()
expect(consoleMessages.some((msg) => msg.includes('location.replace'))).toBeTruthy()
expect(consoleMessages.some((msg) => msg.includes('location.reload'))).toBeTruthy()
})
test('should work with Happy DOM globals', async ({ page, comfyPage }) => {
// Set environment variable for Happy DOM URL
process.env.HAPPY_DOM_URL = 'http://custom-domain.com/'
const locationMock = new LocationMock(page)
await locationMock.setupLocationMock()
await comfyPage.goto()
// Verify location is mocked correctly
const location = await page.evaluate(() => ({
href: window.location.href,
origin: window.location.origin,
canAssign: typeof window.location.assign === 'function',
canReplace: typeof window.location.replace === 'function',
canReload: typeof window.location.reload === 'function'
}))
expect(location.canAssign).toBeTruthy()
expect(location.canReplace).toBeTruthy()
expect(location.canReload).toBeTruthy()
})
})