feat(test-runner): support baseURL in toHaveURL (#8743)

This commit is contained in:
Max Schmitt 2021-09-07 18:34:02 +02:00 committed by GitHub
parent b6ec6339d0
commit e40b805782
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 48 additions and 13 deletions

View file

@ -15,6 +15,9 @@
*/
import { Locator, Page } from '../../..';
import { PlaywrightTestOptions} from '../../../types/test';
import { constructURLBasedOnBaseURL } from '../../utils/utils';
import { currentTestInfo } from '../globals';
import type { Expect } from '../types';
import { toBeTruthy } from './toBeTruthy';
import { toEqual } from './toEqual';
@ -234,9 +237,14 @@ 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 as PlaywrightTestOptions)?.baseURL;
return toMatchText.call(this, 'toHaveURL', page, 'Page', async () => {
return page.url();
}, expected, options);
}, typeof expected === 'string' ? constructURLBasedOnBaseURL(baseURL, expected) : expected, options);
}
export function toHaveValue(

View file

@ -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() {
const packageJson = require('./../../package.json');
return `Playwright/${packageJson.version} (${os.arch()}/${os.platform()}/${os.release()})`;

View file

@ -14,6 +14,7 @@
* limitations under the License.
*/
import http from 'http';
import { test, expect, stripAscii } from './playwright-test-fixtures';
test('should support toHaveCount', async ({ runInlineTest }) => {
@ -159,6 +160,44 @@ test('should support toHaveURL', async ({ runInlineTest }) => {
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 }) => {
const result = await runInlineTest({
'playwright.config.js': `module.exports = { expect: { timeout: 1000 } }`,