JUnit codegen (#29424)

Codegen for JUnit

Fixes https://github.com/microsoft/playwright-java/issues/1039

Following JUnit5 integration
https://github.com/microsoft/playwright-java/issues/1369
This commit is contained in:
Jean-François Greffier 2024-02-20 20:08:53 +01:00 committed by GitHub
parent 3f46ba0680
commit 6494bb30a0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 76 additions and 5 deletions

View File

@ -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) {

View File

@ -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),

View File

@ -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} }
}
}`;

View File

@ -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);
});

View File

@ -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'],