Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.
TypeScript automationchromechromiume2e-testingelectronend-to-end-testingfirefoxjavascriptplaywrighttesttest-automationtestingtesting-toolswebwebkit
JUnit codegen (#29424)
Jean-François Greffier6494bb30a03e
diff --git a/packages/playwright-core/src/cli/program.ts b/packages/playwright-core/src/cli/program.tsindex 5f2767d3f3..c38683a832 100644--- a/packages/playwright-core/src/cli/program.ts+++ b/packages/playwright-core/src/cli/program.ts@@ -65,7 +65,7 @@ Examples:commandWithOpenOptions('codegen [url]', 'open page and generate code for user actions',[['-o, --output <file name>', 'saves the generated script to a file'],- ['--target <language>', `language to generate, one of javascript, playwright-test, python, python-async, python-pytest, csharp, csharp-mstest, csharp-nunit, java`, codegenId()],+ ['--target <language>', `language to generate, one of javascript, playwright-test, python, python-async, python-pytest, csharp, csharp-mstest, csharp-nunit, java, java-junit`, codegenId()],['--save-trace <filename>', 'record a trace for the session and save it to a file'],['--test-id-attribute <attributeName>', 'use the specified attribute to generate data test ID selectors'],]).action(function(url, options) {diff --git a/packages/playwright-core/src/server/recorder.ts b/packages/playwright-core/src/server/recorder.tsindex d288726fcd..997e1244d2 100644--- a/packages/playwright-core/src/server/recorder.ts+++ b/packages/playwright-core/src/server/recorder.ts@@ -433,7 +433,8 @@ class ContextRecorder extends EventEmitter {setOutput(codegenId: string, outputFile?: string) {const languages = new Set([- new JavaLanguageGenerator(),+ new JavaLanguageGenerator('junit'),+ new JavaLanguageGenerator('library'),new JavaScriptLanguageGenerator(/* isPlaywrightTest */false),new JavaScriptLanguageGenerator(/* isPlaywrightTest */true),new PythonLanguageGenerator(/* isAsync */false, /* isPytest */true),diff --git a/packages/playwright-core/src/server/recorder/java.ts b/packages/playwright-core/src/server/recorder/java.tsindex 742b4db93a..d4ebfdeea4 100644--- a/packages/playwright-core/src/server/recorder/java.ts+++ b/packages/playwright-core/src/server/recorder/java.ts@@ -26,16 +26,36 @@ import { JavaScriptFormatter } from './javascript';import { escapeWithQuotes } from '../../utils/isomorphic/stringUtils';import { asLocator } from '../../utils/isomorphic/locatorGenerators';+type JavaLanguageMode = 'library' | 'junit';+export class JavaLanguageGenerator implements LanguageGenerator {- id = 'java';+ id: string;groupName = 'Java';- name = 'Library';+ name: string;highlighter = 'java' as Language;+ _mode: JavaLanguageMode;++ constructor(mode: JavaLanguageMode) {+ if (mode === 'library') {+ this.name = 'Library';+ this.id = 'java';+ } else if (mode === 'junit') {+ this.name = 'JUnit';+ this.id = 'java-junit';+ } else {+ throw new Error(`Unknown Java language mode: ${mode}`);+ }+ this._mode = mode;+ }generateAction(actionInContext: ActionInContext): string {const action = actionInContext.action;const pageAlias = actionInContext.frame.pageAlias;- const formatter = new JavaScriptFormatter(6);+ const offset = this._mode === 'junit' ? 4 : 6;+ const formatter = new JavaScriptFormatter(offset);++ if (this._mode !== 'library' && (action.name === 'openPage' || action.name === 'closePage'))+ return '';if (action.name === 'openPage') {formatter.add(`Page ${pageAlias} = context.newPage();`);@@ -141,6 +161,21 @@ export class JavaLanguageGenerator implements LanguageGenerator {generateHeader(options: LanguageGeneratorOptions): string {const formatter = new JavaScriptFormatter();+ if (this._mode === 'junit') {+ formatter.add(`+ import com.microsoft.playwright.junit.UsePlaywright;+ import com.microsoft.playwright.Page;+ import com.microsoft.playwright.options.*;++ import org.junit.jupiter.api.*;+ import static com.microsoft.playwright.assertions.PlaywrightAssertions.*;++ @UsePlaywright+ public class TestExample {+ @Test+ void test(Page page) {`);+ return formatter.format();+ }formatter.add(`import com.microsoft.playwright.*;import com.microsoft.playwright.options.*;@@ -157,6 +192,10 @@ export class JavaLanguageGenerator implements LanguageGenerator {generateFooter(saveStorage: string | undefined): string {const storageStateLine = saveStorage ? `\n context.storageState(new BrowserContext.StorageStateOptions().setPath(${quote(saveStorage)}));\n` : '';+ if (this._mode === 'junit') {+ return `${storageStateLine} }+}`;+ }return `${storageStateLine} }}}`;diff --git a/tests/library/inspector/cli-codegen-java.spec.ts b/tests/library/inspector/cli-codegen-java.spec.tsindex 738ec37cf9..6b22a1a4cb 100644--- a/tests/library/inspector/cli-codegen-java.spec.ts+++ b/tests/library/inspector/cli-codegen-java.spec.ts@@ -103,3 +103,33 @@ test('should work with --save-har', async ({ runCLI }, testInfo) => {const json = JSON.parse(fs.readFileSync(harFileName, 'utf-8'));expect(json.log.creator.name).toBe('Playwright');});++test('should print the correct imports in junit', async ({ runCLI, channel, browserName }) => {+ const cli = runCLI(['--target=java-junit', emptyHTML]);+ const expectedImportResult = `import com.microsoft.playwright.junit.UsePlaywright;+import com.microsoft.playwright.Page;+import com.microsoft.playwright.options.*;++import org.junit.jupiter.api.*;+import static com.microsoft.playwright.assertions.PlaywrightAssertions.*;`;+ await cli.waitFor(expectedImportResult);+});++test('should print a valid basic program in junit', async ({ runCLI, channel, browserName }) => {+ const cli = runCLI(['--target=java-junit', emptyHTML]);+ const expectedResult = `import com.microsoft.playwright.junit.UsePlaywright;+import com.microsoft.playwright.Page;+import com.microsoft.playwright.options.*;++import org.junit.jupiter.api.*;+import static com.microsoft.playwright.assertions.PlaywrightAssertions.*;++@UsePlaywright+public class TestExample {+ @Test+ void test(Page page) {+ page.navigate("${emptyHTML}");+ }+}`;+ await cli.waitFor(expectedResult);+});diff --git a/tests/library/inspector/inspectorTest.ts b/tests/library/inspector/inspectorTest.tsindex 8485c9f219..7f759f13aa 100644--- a/tests/library/inspector/inspectorTest.ts+++ b/tests/library/inspector/inspectorTest.ts@@ -35,6 +35,7 @@ const codegenLang2Id: Map<string, string> = new Map([['JSON', 'jsonl'],['JavaScript', 'javascript'],['Java', 'java'],+ ['Java JUnit', 'java-junit'],['Python', 'python'],['Python Async', 'python-async'],['Pytest', 'python-pytest'],