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): support whitespace in test tags (#36557)

Adam Gastineau360c653b64f3
diff --git a/packages/html-reporter/src/filter.ts b/packages/html-reporter/src/filter.ts
index 04034c8ceb..7e54c98d4d 100644
--- a/packages/html-reporter/src/filter.ts
+++ b/packages/html-reporter/src/filter.ts
@@ -210,12 +210,16 @@ function cacheSearchValues(test: TestCaseSummary & { [searchValuesSymbol]?: Sear
return searchValues;
}
-export function filterWithToken(tokens: string[], token: string, append: boolean): string {
- if (append) {
- if (!tokens.includes(token))
- return '#?q=' + [...tokens, token].join(' ').trim();
- return '#?q=' + tokens.filter(t => t !== token).join(' ').trim();
- }
+// Extract quoted groups of search params, or tokens separated by whitespace
+const SEARCH_PARAM_GROUP_REGEX = /("[^"]*"|"[^"]*$|\S+)/g;
+
+export function filterWithQuery(existingQuery: string, token: string, append: boolean): string {
+ const tokens = [...existingQuery.matchAll(SEARCH_PARAM_GROUP_REGEX)].map(m => {
+ const rawValue = m[0];
+ return rawValue.startsWith('"') && rawValue.endsWith('"') && rawValue.length > 1 ? rawValue.slice(1, rawValue.length - 1) : rawValue;
+ });
+ if (append)
+ return '#?q=' + joinTokens(!tokens.includes(token) ? [...tokens, token] : tokens.filter(t => t !== token));
// if metaKey or ctrlKey is not pressed, replace existing token with new token
let prefix: 's:' | 'p:' | '@';
@@ -228,5 +232,9 @@ export function filterWithToken(tokens: string[], token: string, append: boolean
const newTokens = tokens.filter(t => !t.startsWith(prefix));
newTokens.push(token);
- return '#?q=' + newTokens.join(' ').trim();
+ return '#?q=' + joinTokens(newTokens);
+}
+
+function joinTokens(tokens: string[]): string {
+ return tokens.map(token => /\s/.test(token) ? `"${token}"` : token).join(' ').trim();
}
diff --git a/packages/html-reporter/src/headerView.tsx b/packages/html-reporter/src/headerView.tsx
index 0b80590212..c0804a6681 100644
--- a/packages/html-reporter/src/headerView.tsx
+++ b/packages/html-reporter/src/headerView.tsx
@@ -22,7 +22,7 @@ import './headerView.css';
import * as icons from './icons';
import { Link, navigate, SearchParamsContext } from './links';
import { statusIcon } from './statusIcon';
-import { filterWithToken } from './filter';
+import { filterWithQuery } from './filter';
import { linkifyText } from '@web/renderUtils';
export const HeaderView: React.FC<{
@@ -83,21 +83,20 @@ const StatsNavView: React.FC<{
}> = ({ stats }) => {
const searchParams = React.useContext(SearchParamsContext);
const q = searchParams.get('q')?.toString() || '';
- const tokens = q.split(' ');
return <nav>
<Link className='subnav-item' href='#?'>
All <span className='d-inline counter'>{stats.total - stats.skipped}</span>
</Link>
- <Link className='subnav-item' click={filterWithToken(tokens, 's:passed', false)} ctrlClick={filterWithToken(tokens, 's:passed', true)}>
+ <Link className='subnav-item' click={filterWithQuery(q, 's:passed', false)} ctrlClick={filterWithQuery(q, 's:passed', true)}>
Passed <span className='d-inline counter'>{stats.expected}</span>
</Link>
- <Link className='subnav-item' click={filterWithToken(tokens, 's:failed', false)} ctrlClick={filterWithToken(tokens, 's:failed', true)}>
+ <Link className='subnav-item' click={filterWithQuery(q, 's:failed', false)} ctrlClick={filterWithQuery(q, 's:failed', true)}>
{!!stats.unexpected && statusIcon('unexpected')} Failed <span className='d-inline counter'>{stats.unexpected}</span>
</Link>
- <Link className='subnav-item' click={filterWithToken(tokens, 's:flaky', false)} ctrlClick={filterWithToken(tokens, 's:flaky', true)}>
+ <Link className='subnav-item' click={filterWithQuery(q, 's:flaky', false)} ctrlClick={filterWithQuery(q, 's:flaky', true)}>
{!!stats.flaky && statusIcon('flaky')} Flaky <span className='d-inline counter'>{stats.flaky}</span>
</Link>
- <Link className='subnav-item' click={filterWithToken(tokens, 's:skipped', false)} ctrlClick={filterWithToken(tokens, 's:skipped', true)}>
+ <Link className='subnav-item' click={filterWithQuery(q, 's:skipped', false)} ctrlClick={filterWithQuery(q, 's:skipped', true)}>
Skipped <span className='d-inline counter'>{stats.skipped}</span>
</Link>
</nav>;
diff --git a/packages/html-reporter/src/testFileView.tsx b/packages/html-reporter/src/testFileView.tsx
index 2086272bb8..ed26b1fc5f 100644
--- a/packages/html-reporter/src/testFileView.tsx
+++ b/packages/html-reporter/src/testFileView.tsx
@@ -18,7 +18,7 @@ import type { TestCaseSummary, TestFileSummary } from './types';
import * as React from 'react';
import { hashStringToInt, msToString } from './utils';
import { Chip } from './chip';
-import { filterWithToken } from './filter';
+import { filterWithQuery } from './filter';
import { Link, LinkBadge, navigate, ProjectLink, SearchParamsContext, testResultHref, TraceLink } from './links';
import { statusIcon } from './statusIcon';
import './testFileView.css';
@@ -93,8 +93,7 @@ const LabelsClickView: React.FC<React.PropsWithChildren<{
const onClickHandle = (e: React.MouseEvent, label: string) => {
e.preventDefault();
const q = searchParams.get('q')?.toString() || '';
- const tokens = q.split(' ');
- navigate(filterWithToken(tokens, label, e.metaKey || e.ctrlKey));
+ navigate(filterWithQuery(q, label, e.metaKey || e.ctrlKey));
};
return labels.length > 0 ? (
diff --git a/packages/playwright/src/common/test.ts b/packages/playwright/src/common/test.ts
index 7ef91d27c5..dce6cbcd83 100644
--- a/packages/playwright/src/common/test.ts
+++ b/packages/playwright/src/common/test.ts
@@ -289,7 +289,12 @@ export class TestCase extends Base implements reporterTypes.TestCase {
}
get tags(): string[] {
- return this._grepTitle().match(/@[\S]+/g) || [];
+ const titleTags = this._grepBaseTitlePath().join(' ').match(/@[\S]+/g) || [];
+
+ return [
+ ...titleTags,
+ ...this._tags,
+ ];
}
_serialize(): any {
@@ -354,10 +359,15 @@ export class TestCase extends Base implements reporterTypes.TestCase {
return result;
}
- _grepTitle() {
+ _grepBaseTitlePath(): string[] {
const path: string[] = [];
this.parent._collectGrepTitlePath(path);
path.push(this.title);
+ return path;
+ }
+
+ _grepTitleWithTags(): string {
+ const path = this._grepBaseTitlePath();
path.push(...this._tags);
return path.join(' ');
}
diff --git a/packages/playwright/src/runner/loadUtils.ts b/packages/playwright/src/runner/loadUtils.ts
index 200f668e5d..f4963c478f 100644
--- a/packages/playwright/src/runner/loadUtils.ts
+++ b/packages/playwright/src/runner/loadUtils.ts
@@ -225,7 +225,7 @@ function createProjectSuite(project: FullProjectInternal, fileSuites: Suite[]):
const grepMatcher = createTitleMatcher(project.project.grep);
const grepInvertMatcher = project.project.grepInvert ? createTitleMatcher(project.project.grepInvert) : null;
filterTestsRemoveEmptySuites(projectSuite, (test: TestCase) => {
- const grepTitle = test._grepTitle();
+ const grepTitle = test._grepTitleWithTags();
if (grepInvertMatcher?.(grepTitle))
return false;
return grepMatcher(grepTitle);
@@ -244,7 +244,7 @@ function filterProjectSuite(projectSuite: Suite, options: { cliFileFilters: Test
if (options.testIdMatcher)
filterByTestIds(result, options.testIdMatcher);
filterTestsRemoveEmptySuites(result, (test: TestCase) => {
- if (options.cliTitleMatcher && !options.cliTitleMatcher(test._grepTitle()))
+ if (options.cliTitleMatcher && !options.cliTitleMatcher(test._grepTitleWithTags()))
return false;
if (options.additionalFileMatcher && !options.additionalFileMatcher(test.location.file))
return false;
diff --git a/tests/playwright-test/reporter-html.spec.ts b/tests/playwright-test/reporter-html.spec.ts
index d8f1df6afa..0cef505d20 100644
--- a/tests/playwright-test/reporter-html.spec.ts
+++ b/tests/playwright-test/reporter-html.spec.ts
@@ -1928,6 +1928,49 @@ for (const useIntermediateMergeReport of [true, false] as const) {
}
});
+ test('tags with whitespace', async ({ runInlineTest, showReport, page }) => {
+ const result = await runInlineTest({
+ 'a.test.js': `
+ const { expect, test } = require('@playwright/test');
+ const tags = ['@smoke-p1 with other text', '@issue[123] issue[456]', '@issue#123 issue#456', '@$$$ ???', '@tl/dr didn\\'t read'];
+
+ test.describe('Error Pages', () => {
+ tags.forEach(tag => {
+ test(tag.replace('@', '') + ' passes', { tag: [tag] }, async ({}) => {
+ expect(1).toBe(1);
+ });
+ });
+ });
+ `,
+ }, { reporter: 'dot,html' }, { PLAYWRIGHT_HTML_OPEN: 'never' });
+
+ expect(result.exitCode).toBe(0);
+ expect(result.passed).toBe(5);
+
+ await showReport();
+ const tags = ['smoke-p1 with other text', 'issue[123] issue[456]', 'issue#123 issue#456', '$$$ ???', 'tl/dr didn\'t read'];
+ const searchInput = page.locator('.subnav-search-input');
+
+ for (const tag of tags) {
+ const tagButton = page.locator('.label').getByText(tag, { exact: true });
+ await expect(tagButton).toBeVisible();
+
+ await tagButton.click();
+ await expect(page.locator('.test-file-test')).toHaveCount(1);
+ await expect(page.locator('.chip')).toHaveCount(1);
+ await expect(page.locator('.chip', { hasText: 'a.test.js' })).toHaveCount(1);
+ await expect(page.locator('.test-file-test .test-file-title')).toHaveText(`Error Pages › ${tag} passes`);
+
+ const testTitle = page.locator('.test-file-test .test-file-title', { hasText: `${tag} passes` });
+ await testTitle.click();
+ await expect(page.locator('.header-title', { hasText: `${tag} passes` })).toBeVisible();
+ await expect(page.locator('.label', { hasText: tag })).toBeVisible();
+
+ await page.goBack();
+ await searchInput.clear();
+ }
+ });
+
test('click label should change URL', async ({ runInlineTest, showReport, page }) => {
const result = await runInlineTest({
'a.test.js': `
diff --git a/tests/playwright-test/test-tag.spec.ts b/tests/playwright-test/test-tag.spec.ts
index 9487e31ea3..049ad49f3c 100644
--- a/tests/playwright-test/test-tag.spec.ts
+++ b/tests/playwright-test/test-tag.spec.ts
@@ -47,6 +47,8 @@ test('should have correct tags', async ({ runInlineTest }) => {
});
test('foo-bar-tags', { tag: ['@foo', '@bar'] }, () => {
});
+ test('foo-bar-tags with @inline', { tag: ['@foo', '@bar', '@this is a long tag', '@another', '@long one again'] }, () => {
+ });
test.skip('skip-foo-tag', { tag: '@foo' }, () => {
});
test.fixme('fixme-bar-tag', { tag: '@bar' }, () => {
@@ -77,6 +79,7 @@ test('should have correct tags', async ({ runInlineTest }) => {
`title=no-tags, tags=`,
`title=foo-tag @inline, tags=@inline,@foo`,
`title=foo-bar-tags, tags=@foo,@bar`,
+ `title=foo-bar-tags with @inline, tags=@inline,@foo,@bar,@this is a long tag,@another,@long one again`,
`title=skip-foo-tag, tags=@foo`,
`title=fixme-bar-tag, tags=@bar`,
`title=fail-foo-bar-tags, tags=@foo,@bar`,