fix(codegen): escape slashes correctly in JavaScript (#75)

This commit is contained in:
Max Schmitt
2020-09-25 13:39:12 +02:00
committed by GitHub
parent 64583b0e41
commit e6a7a119ba
2 changed files with 16 additions and 9 deletions
+5 -9
View File
@@ -167,7 +167,7 @@ export class JavaScriptLanguageGenerator implements LanguageGenerator {
const browser = await ${browserName}.launch(${formatObjectOrVoid(launchOptions)});
const context = await browser.newContext(${formatContextOptions(contextOptions, deviceName)});
})();`);
this._output.write(formatter.format() + '\n');
this._output.write(formatter.format() + '\n');
}
writeFooter(): void {
@@ -266,12 +266,8 @@ class JavaScriptFormatter {
}
}
function quote(text: string, char: string = '\'') {
if (char === '\'')
return char + text.replace(/[']/g, '\\\'') + char;
if (char === '"')
return char + text.replace(/["]/g, '\\"') + char;
if (char === '`')
return char + text.replace(/[`]/g, '\\`') + char;
throw new Error('Invalid escape char');
const quoteChar = '\'';
function quote(text: string): string {
return quoteChar + text.replace(/[']/g, '\\\'')
.replace(/\\/g, '\\\\') + quoteChar;
}
+11
View File
@@ -36,6 +36,17 @@ it('should click', async ({ page, recorder }) => {
expect(message.text()).toBe('click');
});
it('should escape slashes correctly for JavaScript', async ({ recorder, page }) => {
await recorder.setContentAndWait(`<button>username (first last) / Repositories</button>`);
const selector = await recorder.focusElement('button');
expect(selector).toBe('text=/.*username \\(first last\\) / Reposi.*/');
await page.click('text=username')
await recorder.waitForOutput('username')
expect(recorder.output()).toContain(`
// Click text=/.*username \\(first last\\) / Reposi.*/
await page.click('text=/.*username \\\\(first last\\\\) / Reposi.*/');`)
});
it('should not target selector preview by text regexp', async ({ page, recorder }) => {
await recorder.setContentAndWait(`<span>dummy</span>`);