2021-07-29 01:44:44 +03:00
|
|
|
/**
|
|
|
|
* Copyright (c) Microsoft Corporation.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2021-09-21 22:41:24 +03:00
|
|
|
import path from 'path';
|
2022-02-01 04:14:59 +03:00
|
|
|
import { test, expect, stripAnsi } from './playwright-test-fixtures';
|
2021-07-29 01:44:44 +03:00
|
|
|
|
2021-07-29 17:33:19 +03:00
|
|
|
test('should support toHaveCount', async ({ runInlineTest }) => {
|
2021-07-29 01:44:44 +03:00
|
|
|
const result = await runInlineTest({
|
|
|
|
'a.test.ts': `
|
|
|
|
const { test } = pwt;
|
|
|
|
|
|
|
|
test('pass', async ({ page }) => {
|
2021-09-14 03:06:19 +03:00
|
|
|
await page.setContent('<select><option>One</option></select>');
|
2021-07-29 01:44:44 +03:00
|
|
|
const locator = page.locator('option');
|
2021-09-14 03:06:19 +03:00
|
|
|
let done = false;
|
|
|
|
const promise = expect(locator).toHaveCount(2).then(() => { done = true; });
|
|
|
|
await page.waitForTimeout(1000);
|
|
|
|
expect(done).toBe(false);
|
|
|
|
await page.setContent('<select><option>One</option><option>Two</option></select>');
|
|
|
|
await promise;
|
|
|
|
expect(done).toBe(true);
|
2021-07-29 01:44:44 +03:00
|
|
|
});
|
2021-10-22 00:46:54 +03:00
|
|
|
|
|
|
|
test('pass zero', async ({ page }) => {
|
|
|
|
await page.setContent('<div></div>');
|
|
|
|
const locator = page.locator('span');
|
|
|
|
await expect(locator).toHaveCount(0);
|
|
|
|
await expect(locator).not.toHaveCount(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('eventually pass zero', async ({ page }) => {
|
|
|
|
await page.setContent('<div></div>');
|
|
|
|
const locator = page.locator('span');
|
|
|
|
setTimeout(() => page.evaluate(() => div.textContent = '').catch(() => {}), 200);
|
|
|
|
await expect(locator).toHaveCount(0);
|
|
|
|
await expect(locator).not.toHaveCount(1);
|
|
|
|
});
|
|
|
|
|
2021-10-29 02:04:21 +03:00
|
|
|
test('eventually pass non-zero', async ({ page }) => {
|
|
|
|
await page.setContent('<ul></ul>');
|
|
|
|
setTimeout(async () => {
|
|
|
|
await page.setContent("<ul><li>one</li><li>two</li></ul>");
|
|
|
|
}, 500);
|
|
|
|
const locator = page.locator('li');
|
|
|
|
await expect(locator).toHaveCount(2);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('eventually pass not non-zero', async ({ page }) => {
|
|
|
|
await page.setContent('<ul><li>one</li><li>two</li></ul>');
|
|
|
|
setTimeout(async () => {
|
|
|
|
await page.setContent("<ul></ul>");
|
|
|
|
}, 500);
|
|
|
|
const locator = page.locator('li');
|
|
|
|
await expect(locator).not.toHaveCount(2);
|
|
|
|
});
|
|
|
|
|
2021-10-22 00:46:54 +03:00
|
|
|
test('fail zero', async ({ page }) => {
|
|
|
|
await page.setContent('<div><span></span></div>');
|
|
|
|
const locator = page.locator('span');
|
|
|
|
await expect(locator).toHaveCount(0, { timeout: 500 });
|
|
|
|
});
|
|
|
|
|
|
|
|
test('fail zero 2', async ({ page }) => {
|
|
|
|
await page.setContent('<div><span></span></div>');
|
|
|
|
const locator = page.locator('span');
|
|
|
|
await expect(locator).not.toHaveCount(1, { timeout: 500 });
|
|
|
|
});
|
2021-07-29 01:44:44 +03:00
|
|
|
`,
|
|
|
|
}, { workers: 1 });
|
2022-02-01 04:14:59 +03:00
|
|
|
const output = stripAnsi(result.output);
|
2021-10-29 02:04:21 +03:00
|
|
|
expect(result.passed).toBe(5);
|
2021-10-22 00:46:54 +03:00
|
|
|
expect(result.failed).toBe(2);
|
|
|
|
expect(result.exitCode).toBe(1);
|
|
|
|
expect(output).toContain('Expected: 0');
|
|
|
|
expect(output).toContain('Received: 1');
|
2021-11-18 04:28:30 +03:00
|
|
|
expect(output).toContain('expect.toHaveCount with timeout 500ms');
|
2021-07-29 01:44:44 +03:00
|
|
|
});
|
|
|
|
|
2021-08-07 02:58:42 +03:00
|
|
|
test('should support toHaveJSProperty', async ({ runInlineTest }) => {
|
2021-07-29 01:44:44 +03:00
|
|
|
const result = await runInlineTest({
|
|
|
|
'a.test.ts': `
|
|
|
|
const { test } = pwt;
|
|
|
|
|
|
|
|
test('pass', async ({ page }) => {
|
|
|
|
await page.setContent('<div></div>');
|
|
|
|
await page.$eval('div', e => e.foo = { a: 1, b: 'string', c: new Date(1627503992000) });
|
|
|
|
const locator = page.locator('div');
|
2021-08-07 02:58:42 +03:00
|
|
|
await expect(locator).toHaveJSProperty('foo', { a: 1, b: 'string', c: new Date(1627503992000) });
|
2021-07-29 01:44:44 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
test('fail', async ({ page }) => {
|
|
|
|
await page.setContent('<div></div>');
|
|
|
|
await page.$eval('div', e => e.foo = { a: 1, b: 'string', c: new Date(1627503992000) });
|
|
|
|
const locator = page.locator('div');
|
2021-08-07 02:58:42 +03:00
|
|
|
await expect(locator).toHaveJSProperty('foo', { a: 1, b: 'string', c: new Date(1627503992001) }, { timeout: 1000 });
|
2021-07-29 01:44:44 +03:00
|
|
|
});
|
|
|
|
`,
|
|
|
|
}, { workers: 1 });
|
2022-02-01 04:14:59 +03:00
|
|
|
const output = stripAnsi(result.output);
|
2021-07-29 01:44:44 +03:00
|
|
|
expect(output).toContain('- "c"');
|
|
|
|
expect(result.passed).toBe(1);
|
|
|
|
expect(result.failed).toBe(1);
|
|
|
|
expect(result.exitCode).toBe(1);
|
|
|
|
});
|
|
|
|
|
2021-10-29 04:45:59 +03:00
|
|
|
|
|
|
|
test('should support toHaveJSProperty with builtin types', async ({ runInlineTest }) => {
|
|
|
|
const result = await runInlineTest({
|
|
|
|
'a.test.ts': `
|
|
|
|
const { test } = pwt;
|
|
|
|
|
|
|
|
test('pass string', async ({ page }) => {
|
|
|
|
await page.setContent('<div></div>');
|
|
|
|
await page.$eval('div', e => e.foo = 'string');
|
|
|
|
const locator = page.locator('div');
|
|
|
|
await expect(locator).toHaveJSProperty('foo', 'string');
|
|
|
|
});
|
|
|
|
|
|
|
|
test('fail string', async ({ page }) => {
|
|
|
|
await page.setContent('<div></div>');
|
|
|
|
await page.$eval('div', e => e.foo = 'string');
|
|
|
|
const locator = page.locator('div');
|
2022-05-18 00:44:12 +03:00
|
|
|
await expect(locator).toHaveJSProperty('foo', 'error', {timeout: 200});
|
2021-10-29 04:45:59 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
test('pass number', async ({ page }) => {
|
|
|
|
await page.setContent('<div></div>');
|
|
|
|
await page.$eval('div', e => e.foo = 2021);
|
|
|
|
const locator = page.locator('div');
|
|
|
|
await expect(locator).toHaveJSProperty('foo', 2021);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('fail number', async ({ page }) => {
|
|
|
|
await page.setContent('<div></div>');
|
|
|
|
await page.$eval('div', e => e.foo = 2021);
|
|
|
|
const locator = page.locator('div');
|
2022-05-18 00:44:12 +03:00
|
|
|
await expect(locator).toHaveJSProperty('foo', 1, {timeout: 200});
|
2021-10-29 04:45:59 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
test('pass boolean', async ({ page }) => {
|
|
|
|
await page.setContent('<div></div>');
|
|
|
|
await page.$eval('div', e => e.foo = true);
|
|
|
|
const locator = page.locator('div');
|
|
|
|
await expect(locator).toHaveJSProperty('foo', true);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('fail boolean', async ({ page }) => {
|
|
|
|
await page.setContent('<div></div>');
|
|
|
|
await page.$eval('div', e => e.foo = false);
|
|
|
|
const locator = page.locator('div');
|
2022-05-18 00:44:12 +03:00
|
|
|
await expect(locator).toHaveJSProperty('foo', true, {timeout: 200});
|
|
|
|
});
|
|
|
|
|
|
|
|
test('pass boolean 2', async ({ page }) => {
|
|
|
|
await page.setContent('<div></div>');
|
|
|
|
await page.$eval('div', e => e.foo = false);
|
|
|
|
const locator = page.locator('div');
|
|
|
|
await expect(locator).toHaveJSProperty('foo', false);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('fail boolean 2', async ({ page }) => {
|
|
|
|
await page.setContent('<div></div>');
|
|
|
|
await page.$eval('div', e => e.foo = false);
|
|
|
|
const locator = page.locator('div');
|
|
|
|
await expect(locator).toHaveJSProperty('foo', true, {timeout: 200});
|
|
|
|
});
|
|
|
|
|
|
|
|
test('pass undefined', async ({ page }) => {
|
|
|
|
await page.setContent('<div></div>');
|
|
|
|
const locator = page.locator('div');
|
|
|
|
await expect(locator).toHaveJSProperty('foo', undefined);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('pass null', async ({ page }) => {
|
|
|
|
await page.setContent('<div></div>');
|
|
|
|
await page.$eval('div', e => e.foo = null);
|
|
|
|
const locator = page.locator('div');
|
|
|
|
await expect(locator).toHaveJSProperty('foo', null);
|
2021-10-29 04:45:59 +03:00
|
|
|
});
|
|
|
|
`,
|
|
|
|
}, { workers: 1 });
|
2022-02-01 04:14:59 +03:00
|
|
|
const output = stripAnsi(result.output);
|
2022-05-18 00:44:12 +03:00
|
|
|
expect(result.passed).toBe(6);
|
|
|
|
expect(result.failed).toBe(4);
|
2021-10-29 04:45:59 +03:00
|
|
|
expect(result.exitCode).toBe(1);
|
|
|
|
expect(output).toContain('Expected: "error"');
|
|
|
|
expect(output).toContain('Received: "string"');
|
|
|
|
expect(output).toContain('Expected: 1');
|
|
|
|
expect(output).toContain('Received: 2021');
|
|
|
|
expect(output).toContain('Expected: true');
|
|
|
|
expect(output).toContain('Received: false');
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2021-07-29 01:44:44 +03:00
|
|
|
test('should support toHaveClass', async ({ runInlineTest }) => {
|
|
|
|
const result = await runInlineTest({
|
|
|
|
'a.test.ts': `
|
|
|
|
const { test } = pwt;
|
|
|
|
|
|
|
|
test('pass', async ({ page }) => {
|
|
|
|
await page.setContent('<div class="foo bar baz"></div>');
|
|
|
|
const locator = page.locator('div');
|
2021-07-29 17:33:19 +03:00
|
|
|
await expect(locator).toHaveClass('foo bar baz');
|
2021-07-29 01:44:44 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
test('fail', async ({ page }) => {
|
|
|
|
await page.setContent('<div class="bar baz"></div>');
|
|
|
|
const locator = page.locator('div');
|
2021-07-29 17:33:19 +03:00
|
|
|
await expect(locator).toHaveClass('foo bar baz', { timeout: 1000 });
|
2021-07-29 01:44:44 +03:00
|
|
|
});
|
|
|
|
`,
|
|
|
|
}, { workers: 1 });
|
2022-02-01 04:14:59 +03:00
|
|
|
const output = stripAnsi(result.output);
|
2021-07-29 01:44:44 +03:00
|
|
|
expect(output).toContain('expect(locator).toHaveClass');
|
2021-07-29 17:33:19 +03:00
|
|
|
expect(output).toContain('Expected string: \"foo bar baz\"');
|
|
|
|
expect(result.passed).toBe(1);
|
|
|
|
expect(result.failed).toBe(1);
|
|
|
|
expect(result.exitCode).toBe(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should support toHaveClass w/ array', async ({ runInlineTest }) => {
|
|
|
|
const result = await runInlineTest({
|
|
|
|
'a.test.ts': `
|
|
|
|
const { test } = pwt;
|
|
|
|
|
|
|
|
test('pass', async ({ page }) => {
|
|
|
|
await page.setContent('<div class="foo"></div><div class="bar"></div><div class="baz"></div>');
|
|
|
|
const locator = page.locator('div');
|
2021-09-03 01:48:04 +03:00
|
|
|
await expect(locator).toHaveClass(['foo', 'bar', /[a-z]az/]);
|
2021-07-29 17:33:19 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
test('fail', async ({ page }) => {
|
|
|
|
await page.setContent('<div class="foo"></div><div class="bar"></div><div class="bar"></div>');
|
|
|
|
const locator = page.locator('div');
|
2021-09-03 01:48:04 +03:00
|
|
|
await expect(locator).toHaveClass(['foo', 'bar', /[a-z]az/], { timeout: 1000 });
|
2021-07-29 17:33:19 +03:00
|
|
|
});
|
|
|
|
`,
|
|
|
|
}, { workers: 1 });
|
2022-02-01 04:14:59 +03:00
|
|
|
const output = stripAnsi(result.output);
|
2021-07-29 17:33:19 +03:00
|
|
|
expect(output).toContain('expect(received).toHaveClass(expected)');
|
2021-09-03 01:48:04 +03:00
|
|
|
expect(output).toContain('- /[a-z]az/,');
|
2021-07-29 17:33:19 +03:00
|
|
|
expect(result.passed).toBe(1);
|
|
|
|
expect(result.failed).toBe(1);
|
|
|
|
expect(result.exitCode).toBe(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should support toHaveTitle', async ({ runInlineTest }) => {
|
|
|
|
const result = await runInlineTest({
|
|
|
|
'a.test.ts': `
|
|
|
|
const { test } = pwt;
|
|
|
|
|
|
|
|
test('pass', async ({ page }) => {
|
2021-09-15 05:24:29 +03:00
|
|
|
await page.setContent('<title> Hello world</title>');
|
|
|
|
await expect(page).toHaveTitle('Hello world');
|
2021-07-29 17:33:19 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
test('fail', async ({ page }) => {
|
|
|
|
await page.setContent('<title>Bye</title>');
|
|
|
|
await expect(page).toHaveTitle('Hello', { timeout: 100 });
|
|
|
|
});
|
|
|
|
`,
|
|
|
|
}, { workers: 1 });
|
2022-02-01 04:14:59 +03:00
|
|
|
const output = stripAnsi(result.output);
|
2021-07-29 17:33:19 +03:00
|
|
|
expect(output).toContain('expect(page).toHaveTitle');
|
|
|
|
expect(output).toContain('Expected string: \"Hello\"');
|
|
|
|
expect(result.passed).toBe(1);
|
|
|
|
expect(result.failed).toBe(1);
|
|
|
|
expect(result.exitCode).toBe(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should support toHaveURL', async ({ runInlineTest }) => {
|
|
|
|
const result = await runInlineTest({
|
|
|
|
'a.test.ts': `
|
|
|
|
const { test } = pwt;
|
|
|
|
|
|
|
|
test('pass', async ({ page }) => {
|
|
|
|
await page.goto('data:text/html,<div>A</div>');
|
|
|
|
await expect(page).toHaveURL('data:text/html,<div>A</div>');
|
|
|
|
});
|
|
|
|
|
|
|
|
test('fail', async ({ page }) => {
|
|
|
|
await page.goto('data:text/html,<div>B</div>');
|
|
|
|
await expect(page).toHaveURL('wrong', { timeout: 100 });
|
|
|
|
});
|
|
|
|
`,
|
|
|
|
}, { workers: 1 });
|
2022-02-01 04:14:59 +03:00
|
|
|
const output = stripAnsi(result.output);
|
2021-07-29 17:33:19 +03:00
|
|
|
expect(output).toContain('expect(page).toHaveURL');
|
|
|
|
expect(output).toContain('Expected string: \"wrong\"');
|
2021-07-29 01:44:44 +03:00
|
|
|
expect(result.passed).toBe(1);
|
|
|
|
expect(result.failed).toBe(1);
|
|
|
|
expect(result.exitCode).toBe(1);
|
|
|
|
});
|
2021-07-30 07:03:50 +03:00
|
|
|
|
2021-09-21 22:41:24 +03:00
|
|
|
test('should support toHaveURL with baseURL from webServer', async ({ runInlineTest }, testInfo) => {
|
2021-09-07 19:34:02 +03:00
|
|
|
const port = testInfo.workerIndex + 10500;
|
|
|
|
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 = {
|
2021-09-21 22:41:24 +03:00
|
|
|
webServer: {
|
|
|
|
command: 'node ${JSON.stringify(path.join(__dirname, 'assets', 'simple-server.js'))} ${port}',
|
|
|
|
port: ${port},
|
|
|
|
},
|
2021-09-07 19:34:02 +03:00
|
|
|
};
|
|
|
|
`,
|
|
|
|
}, { workers: 1 });
|
2022-02-01 04:14:59 +03:00
|
|
|
const output = stripAnsi(result.output);
|
2021-09-07 19:34:02 +03:00
|
|
|
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);
|
|
|
|
});
|
|
|
|
|
2021-11-18 04:28:30 +03:00
|
|
|
test('should respect expect.timeout', async ({ runInlineTest }) => {
|
2021-07-30 07:03:50 +03:00
|
|
|
const result = await runInlineTest({
|
2021-08-08 08:08:56 +03:00
|
|
|
'playwright.config.js': `module.exports = { expect: { timeout: 1000 } }`,
|
2021-07-30 07:03:50 +03:00
|
|
|
'a.test.ts': `
|
|
|
|
const { test } = pwt;
|
|
|
|
|
|
|
|
test('timeout', async ({ page }) => {
|
|
|
|
await page.goto('data:text/html,<div>A</div>');
|
|
|
|
await Promise.all([
|
|
|
|
expect(page).toHaveURL('data:text/html,<div>B</div>'),
|
|
|
|
new Promise(f => setTimeout(f, 2000)).then(() => expect(true).toBe(false))
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
`,
|
|
|
|
}, { workers: 1 });
|
2022-02-01 04:14:59 +03:00
|
|
|
const output = stripAnsi(result.output);
|
2021-07-30 07:03:50 +03:00
|
|
|
expect(output).toContain('expect(received).toHaveURL(expected)');
|
2021-11-18 04:28:30 +03:00
|
|
|
expect(output).toContain('expect.toHaveURL with timeout 1000ms');
|
2021-07-30 07:03:50 +03:00
|
|
|
expect(result.failed).toBe(1);
|
|
|
|
expect(result.exitCode).toBe(1);
|
|
|
|
});
|
2021-09-24 22:54:33 +03:00
|
|
|
|
|
|
|
test('should log scale the time', async ({ runInlineTest }) => {
|
|
|
|
const result = await runInlineTest({
|
|
|
|
'a.test.ts': `
|
|
|
|
const { test } = pwt;
|
|
|
|
|
|
|
|
test('pass', async ({ page }) => {
|
|
|
|
await page.setContent('<div id=div>Wrong</div>');
|
|
|
|
await expect(page.locator('div')).toHaveText('Text', { timeout: 2000 });
|
|
|
|
});
|
|
|
|
`,
|
|
|
|
}, { workers: 1 });
|
2022-02-01 04:14:59 +03:00
|
|
|
const output = stripAnsi(result.output);
|
2021-09-24 22:54:33 +03:00
|
|
|
const tokens = output.split('unexpected value');
|
|
|
|
// Log scale: 0, 100, 250, 500, 1000, 1000, should be less than 8.
|
|
|
|
expect(tokens.length).toBeGreaterThan(1);
|
|
|
|
expect(tokens.length).toBeLessThan(8);
|
|
|
|
expect(result.passed).toBe(0);
|
|
|
|
expect(result.exitCode).toBe(1);
|
|
|
|
});
|