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:
parent
3f46ba0680
commit
6494bb30a0
|
|
@ -65,7 +65,7 @@ Examples:
|
||||||
commandWithOpenOptions('codegen [url]', 'open page and generate code for user actions',
|
commandWithOpenOptions('codegen [url]', 'open page and generate code for user actions',
|
||||||
[
|
[
|
||||||
['-o, --output <file name>', 'saves the generated script to a file'],
|
['-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'],
|
['--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'],
|
['--test-id-attribute <attributeName>', 'use the specified attribute to generate data test ID selectors'],
|
||||||
]).action(function(url, options) {
|
]).action(function(url, options) {
|
||||||
|
|
|
||||||
|
|
@ -433,7 +433,8 @@ class ContextRecorder extends EventEmitter {
|
||||||
|
|
||||||
setOutput(codegenId: string, outputFile?: string) {
|
setOutput(codegenId: string, outputFile?: string) {
|
||||||
const languages = new Set([
|
const languages = new Set([
|
||||||
new JavaLanguageGenerator(),
|
new JavaLanguageGenerator('junit'),
|
||||||
|
new JavaLanguageGenerator('library'),
|
||||||
new JavaScriptLanguageGenerator(/* isPlaywrightTest */false),
|
new JavaScriptLanguageGenerator(/* isPlaywrightTest */false),
|
||||||
new JavaScriptLanguageGenerator(/* isPlaywrightTest */true),
|
new JavaScriptLanguageGenerator(/* isPlaywrightTest */true),
|
||||||
new PythonLanguageGenerator(/* isAsync */false, /* isPytest */true),
|
new PythonLanguageGenerator(/* isAsync */false, /* isPytest */true),
|
||||||
|
|
|
||||||
|
|
@ -26,16 +26,36 @@ import { JavaScriptFormatter } from './javascript';
|
||||||
import { escapeWithQuotes } from '../../utils/isomorphic/stringUtils';
|
import { escapeWithQuotes } from '../../utils/isomorphic/stringUtils';
|
||||||
import { asLocator } from '../../utils/isomorphic/locatorGenerators';
|
import { asLocator } from '../../utils/isomorphic/locatorGenerators';
|
||||||
|
|
||||||
|
type JavaLanguageMode = 'library' | 'junit';
|
||||||
|
|
||||||
export class JavaLanguageGenerator implements LanguageGenerator {
|
export class JavaLanguageGenerator implements LanguageGenerator {
|
||||||
id = 'java';
|
id: string;
|
||||||
groupName = 'Java';
|
groupName = 'Java';
|
||||||
name = 'Library';
|
name: string;
|
||||||
highlighter = 'java' as Language;
|
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 {
|
generateAction(actionInContext: ActionInContext): string {
|
||||||
const action = actionInContext.action;
|
const action = actionInContext.action;
|
||||||
const pageAlias = actionInContext.frame.pageAlias;
|
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') {
|
if (action.name === 'openPage') {
|
||||||
formatter.add(`Page ${pageAlias} = context.newPage();`);
|
formatter.add(`Page ${pageAlias} = context.newPage();`);
|
||||||
|
|
@ -141,6 +161,21 @@ export class JavaLanguageGenerator implements LanguageGenerator {
|
||||||
|
|
||||||
generateHeader(options: LanguageGeneratorOptions): string {
|
generateHeader(options: LanguageGeneratorOptions): string {
|
||||||
const formatter = new JavaScriptFormatter();
|
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(`
|
formatter.add(`
|
||||||
import com.microsoft.playwright.*;
|
import com.microsoft.playwright.*;
|
||||||
import com.microsoft.playwright.options.*;
|
import com.microsoft.playwright.options.*;
|
||||||
|
|
@ -157,6 +192,10 @@ export class JavaLanguageGenerator implements LanguageGenerator {
|
||||||
|
|
||||||
generateFooter(saveStorage: string | undefined): string {
|
generateFooter(saveStorage: string | undefined): string {
|
||||||
const storageStateLine = saveStorage ? `\n context.storageState(new BrowserContext.StorageStateOptions().setPath(${quote(saveStorage)}));\n` : '';
|
const storageStateLine = saveStorage ? `\n context.storageState(new BrowserContext.StorageStateOptions().setPath(${quote(saveStorage)}));\n` : '';
|
||||||
|
if (this._mode === 'junit') {
|
||||||
|
return `${storageStateLine} }
|
||||||
|
}`;
|
||||||
|
}
|
||||||
return `${storageStateLine} }
|
return `${storageStateLine} }
|
||||||
}
|
}
|
||||||
}`;
|
}`;
|
||||||
|
|
|
||||||
|
|
@ -103,3 +103,33 @@ test('should work with --save-har', async ({ runCLI }, testInfo) => {
|
||||||
const json = JSON.parse(fs.readFileSync(harFileName, 'utf-8'));
|
const json = JSON.parse(fs.readFileSync(harFileName, 'utf-8'));
|
||||||
expect(json.log.creator.name).toBe('Playwright');
|
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);
|
||||||
|
});
|
||||||
|
|
|
||||||
|
|
@ -35,6 +35,7 @@ const codegenLang2Id: Map<string, string> = new Map([
|
||||||
['JSON', 'jsonl'],
|
['JSON', 'jsonl'],
|
||||||
['JavaScript', 'javascript'],
|
['JavaScript', 'javascript'],
|
||||||
['Java', 'java'],
|
['Java', 'java'],
|
||||||
|
['Java JUnit', 'java-junit'],
|
||||||
['Python', 'python'],
|
['Python', 'python'],
|
||||||
['Python Async', 'python-async'],
|
['Python Async', 'python-async'],
|
||||||
['Pytest', 'python-pytest'],
|
['Pytest', 'python-pytest'],
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue