feat(test-runner): support baseURL in toHaveURL (#8743)
This commit is contained in:
parent
b6ec6339d0
commit
e40b805782
|
|
@ -15,6 +15,9 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { Locator, Page } from '../../..';
|
import { Locator, Page } from '../../..';
|
||||||
|
import { PlaywrightTestOptions} from '../../../types/test';
|
||||||
|
import { constructURLBasedOnBaseURL } from '../../utils/utils';
|
||||||
|
import { currentTestInfo } from '../globals';
|
||||||
import type { Expect } from '../types';
|
import type { Expect } from '../types';
|
||||||
import { toBeTruthy } from './toBeTruthy';
|
import { toBeTruthy } from './toBeTruthy';
|
||||||
import { toEqual } from './toEqual';
|
import { toEqual } from './toEqual';
|
||||||
|
|
@ -234,9 +237,14 @@ export function toHaveURL(
|
||||||
expected: string | RegExp,
|
expected: string | RegExp,
|
||||||
options?: { timeout?: number },
|
options?: { timeout?: number },
|
||||||
) {
|
) {
|
||||||
|
const testInfo = currentTestInfo();
|
||||||
|
if (!testInfo)
|
||||||
|
throw new Error(`toHaveURL must be called during the test`);
|
||||||
|
const baseURL = (testInfo.project.use as PlaywrightTestOptions)?.baseURL;
|
||||||
|
|
||||||
return toMatchText.call(this, 'toHaveURL', page, 'Page', async () => {
|
return toMatchText.call(this, 'toHaveURL', page, 'Page', async () => {
|
||||||
return page.url();
|
return page.url();
|
||||||
}, expected, options);
|
}, typeof expected === 'string' ? constructURLBasedOnBaseURL(baseURL, expected) : expected, options);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function toHaveValue(
|
export function toHaveValue(
|
||||||
|
|
|
||||||
|
|
@ -331,18 +331,6 @@ export function canAccessFile(file: string) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const localIpAddresses = [
|
|
||||||
'localhost',
|
|
||||||
'127.0.0.1',
|
|
||||||
'::ffff:127.0.0.1',
|
|
||||||
'::1',
|
|
||||||
'0000:0000:0000:0000:0000:0000:0000:0001', // WebKit (Windows)
|
|
||||||
];
|
|
||||||
|
|
||||||
export function isLocalIpAddress(ipAdress: string): boolean {
|
|
||||||
return localIpAddresses.includes(ipAdress);
|
|
||||||
}
|
|
||||||
|
|
||||||
export function getUserAgent() {
|
export function getUserAgent() {
|
||||||
const packageJson = require('./../../package.json');
|
const packageJson = require('./../../package.json');
|
||||||
return `Playwright/${packageJson.version} (${os.arch()}/${os.platform()}/${os.release()})`;
|
return `Playwright/${packageJson.version} (${os.arch()}/${os.platform()}/${os.release()})`;
|
||||||
|
|
|
||||||
|
|
@ -14,6 +14,7 @@
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
import http from 'http';
|
||||||
import { test, expect, stripAscii } from './playwright-test-fixtures';
|
import { test, expect, stripAscii } from './playwright-test-fixtures';
|
||||||
|
|
||||||
test('should support toHaveCount', async ({ runInlineTest }) => {
|
test('should support toHaveCount', async ({ runInlineTest }) => {
|
||||||
|
|
@ -159,6 +160,44 @@ test('should support toHaveURL', async ({ runInlineTest }) => {
|
||||||
expect(result.exitCode).toBe(1);
|
expect(result.exitCode).toBe(1);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('should support toHaveURL with baseURL', 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;
|
||||||
|
|
||||||
|
test('pass', async ({ page }) => {
|
||||||
|
await page.goto('/foobar');
|
||||||
|
await expect(page).toHaveURL('/foobar');
|
||||||
|
await expect(page).toHaveURL('http://localhost:${port}/foobar');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('fail', async ({ page }) => {
|
||||||
|
await page.goto('/foobar');
|
||||||
|
await expect(page).toHaveURL('/kek', { timeout: 100 });
|
||||||
|
});
|
||||||
|
`,
|
||||||
|
'playwright.config.ts': `
|
||||||
|
module.exports = {
|
||||||
|
use: {
|
||||||
|
baseURL: 'http://localhost:${port}',
|
||||||
|
}
|
||||||
|
};
|
||||||
|
`,
|
||||||
|
}, { workers: 1 });
|
||||||
|
const output = stripAscii(result.output);
|
||||||
|
expect(output).toContain('expect(page).toHaveURL');
|
||||||
|
expect(output).toContain(`Expected string: \"http://localhost:${port}/kek\"`);
|
||||||
|
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 }) => {
|
test('should support respect expect.timeout', async ({ runInlineTest }) => {
|
||||||
const result = await runInlineTest({
|
const result = await runInlineTest({
|
||||||
'playwright.config.js': `module.exports = { expect: { timeout: 1000 } }`,
|
'playwright.config.js': `module.exports = { expect: { timeout: 1000 } }`,
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue