Skip to content

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

fix(test-runner): toHaveURL respect baseURL (#9050)

Max Schmittf9c5279c3a25
diff --git a/src/test/matchers/matchers.ts b/src/test/matchers/matchers.ts
index 206a37be13..44cf82bff1 100644
--- a/src/test/matchers/matchers.ts
+++ b/src/test/matchers/matchers.ts
@@ -16,7 +16,6 @@
import { Locator, Page } from '../../..';
import { constructURLBasedOnBaseURL, isString } from '../../utils/utils';
-import { currentTestInfo } from '../globals';
import type { Expect } from '../types';
import { toBeTruthy } from './toBeTruthy';
import { toEqual } from './toEqual';
@@ -239,10 +238,7 @@ export function toHaveURL(
expected: string | RegExp,
options?: { timeout?: number },
) {
- const testInfo = currentTestInfo();
- if (!testInfo)
- throw new Error(`toHaveURL must be called during the test`);
- const baseURL = testInfo.project.use.baseURL;
+ const baseURL = (page.context() as any)._options.baseURL;
return toMatchText.call(this, 'toHaveURL', page, 'Page', async () => {
return page.url();
diff --git a/tests/playwright-test/playwright.expect.misc.spec.ts b/tests/playwright-test/playwright.expect.misc.spec.ts
index 8362f1dc5b..81c727457c 100644
--- a/tests/playwright-test/playwright.expect.misc.spec.ts
+++ b/tests/playwright-test/playwright.expect.misc.spec.ts
@@ -14,7 +14,7 @@
* limitations under the License.
*/
-import http from 'http';
+import path from 'path';
import { test, expect, stripAscii } from './playwright-test-fixtures';
test('should support toHaveCount', async ({ runInlineTest }) => {
@@ -166,12 +166,8 @@ test('should support toHaveURL', async ({ runInlineTest }) => {
expect(result.exitCode).toBe(1);
});
-test('should support toHaveURL with baseURL', async ({ runInlineTest }, testInfo) => {
+test('should support toHaveURL with baseURL from webServer', async ({ runInlineTest }, testInfo) => {
const port = testInfo.workerIndex + 10500;
- const server = http.createServer((req: http.IncomingMessage, res: http.ServerResponse) => {
- res.end('<html><body>hello</body></html>');
- });
- await new Promise(resolve => server.listen(port, resolve));
const result = await runInlineTest({
'a.test.ts': `
const { test } = pwt;
@@ -189,9 +185,10 @@ test('should support toHaveURL with baseURL', async ({ runInlineTest }, testInfo
`,
'playwright.config.ts': `
module.exports = {
- use: {
- baseURL: 'http://localhost:${port}',
- }
+ webServer: {
+ command: 'node ${JSON.stringify(path.join(__dirname, 'assets', 'simple-server.js'))} ${port}',
+ port: ${port},
+ },
};
`,
}, { workers: 1 });
@@ -201,7 +198,6 @@ test('should support toHaveURL with baseURL', async ({ runInlineTest }, testInfo
expect(result.passed).toBe(1);
expect(result.failed).toBe(1);
expect(result.exitCode).toBe(1);
- await new Promise(resolve => server.close(resolve));
});
test('should support respect expect.timeout', async ({ runInlineTest }) => {