Skip to content

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

feat(codegen): use web-first page assertions to match URL (#12177)

Max Schmitt4115235f4dfe
diff --git a/packages/playwright-core/src/server/supplements/recorder/java.ts b/packages/playwright-core/src/server/supplements/recorder/java.ts
index d70efc7f18..e000d8ca7f 100644
--- a/packages/playwright-core/src/server/supplements/recorder/java.ts
+++ b/packages/playwright-core/src/server/supplements/recorder/java.ts
@@ -89,7 +89,7 @@ export class JavaLanguageGenerator implements LanguageGenerator {
formatter.add(code);
if (signals.assertNavigation)
- formatter.add(`// assert ${pageAlias}.url().equals(${quote(signals.assertNavigation.url)});`);
+ formatter.add(`// assertThat(${pageAlias}).hasURL(${quote(signals.assertNavigation.url)});`);
return formatter.format();
}
@@ -141,6 +141,7 @@ export class JavaLanguageGenerator implements LanguageGenerator {
formatter.add(`
import com.microsoft.playwright.*;
import com.microsoft.playwright.options.*;
+ import static com.microsoft.playwright.assertions.PlaywrightAssertions.assertThat;
import java.util.*;
public class Example {
diff --git a/packages/playwright-core/src/server/supplements/recorder/python.ts b/packages/playwright-core/src/server/supplements/recorder/python.ts
index d0b6bee6eb..33c78f873d 100644
--- a/packages/playwright-core/src/server/supplements/recorder/python.ts
+++ b/packages/playwright-core/src/server/supplements/recorder/python.ts
@@ -98,7 +98,7 @@ export class PythonLanguageGenerator implements LanguageGenerator {
formatter.add(code);
if (signals.assertNavigation)
- formatter.add(` # assert ${pageAlias}.url == ${quote(signals.assertNavigation.url)}`);
+ formatter.add(` # ${this._awaitPrefix}expect(${pageAlias}).to_have_url(${quote(signals.assertNavigation.url)})`);
return formatter.format();
}
@@ -151,7 +151,7 @@ export class PythonLanguageGenerator implements LanguageGenerator {
formatter.add(`
import asyncio
-from playwright.async_api import Playwright, async_playwright
+from playwright.async_api import Playwright, async_playwright, expect
async def run(playwright: Playwright) -> None {
@@ -159,7 +159,7 @@ async def run(playwright: Playwright) -> None {
context = await browser.new_context(${formatContextOptions(options.contextOptions, options.deviceName)})`);
} else {
formatter.add(`
-from playwright.sync_api import Playwright, sync_playwright
+from playwright.sync_api import Playwright, sync_playwright, expect
def run(playwright: Playwright) -> None {
diff --git a/tests/inspector/cli-codegen-1.spec.ts b/tests/inspector/cli-codegen-1.spec.ts
index 418b3a9a24..79b872563f 100644
--- a/tests/inspector/cli-codegen-1.spec.ts
+++ b/tests/inspector/cli-codegen-1.spec.ts
@@ -624,17 +624,17 @@ test.describe('cli codegen', () => {
expect(sources.get('Java').text).toContain(`
// Click text=link
page.locator("text=link").click();
- // assert page.url().equals("about:blank#foo");`);
+ // assertThat(page).hasURL("about:blank#foo");`);
expect(sources.get('Python').text).toContain(`
# Click text=link
page.locator(\"text=link\").click()
- # assert page.url == \"about:blank#foo\"`);
+ # expect(page).to_have_url(\"about:blank#foo\")`);
expect(sources.get('Python Async').text).toContain(`
# Click text=link
await page.locator(\"text=link\").click()
- # assert page.url == \"about:blank#foo\"`);
+ # await expect(page).to_have_url(\"about:blank#foo\")`);
expect(sources.get('C#').text).toContain(`
// Click text=link
diff --git a/tests/inspector/cli-codegen-java.spec.ts b/tests/inspector/cli-codegen-java.spec.ts
index a760234d87..b8170313e8 100644
--- a/tests/inspector/cli-codegen-java.spec.ts
+++ b/tests/inspector/cli-codegen-java.spec.ts
@@ -27,6 +27,7 @@ test('should print the correct imports and context options', async ({ runCLI, ch
const cli = runCLI(['--target=java', emptyHTML]);
const expectedResult = `import com.microsoft.playwright.*;
import com.microsoft.playwright.options.*;
+import static com.microsoft.playwright.assertions.PlaywrightAssertions.assertThat;
import java.util.*;
public class Example {
diff --git a/tests/inspector/cli-codegen-python-async.spec.ts b/tests/inspector/cli-codegen-python-async.spec.ts
index 29eca794ad..db62c6b2b0 100644
--- a/tests/inspector/cli-codegen-python-async.spec.ts
+++ b/tests/inspector/cli-codegen-python-async.spec.ts
@@ -27,7 +27,7 @@ test('should print the correct imports and context options', async ({ browserNam
const cli = runCLI(['--target=python-async', emptyHTML]);
const expectedResult = `import asyncio
-from playwright.async_api import Playwright, async_playwright
+from playwright.async_api import Playwright, async_playwright, expect
async def run(playwright: Playwright) -> None:
@@ -41,7 +41,7 @@ test('should print the correct context options for custom settings', async ({ br
const cli = runCLI(['--color-scheme=light', '--target=python-async', emptyHTML]);
const expectedResult = `import asyncio
-from playwright.async_api import Playwright, async_playwright
+from playwright.async_api import Playwright, async_playwright, expect
async def run(playwright: Playwright) -> None:
@@ -57,7 +57,7 @@ test('should print the correct context options when using a device', async ({ br
const cli = runCLI(['--device=Pixel 2', '--target=python-async', emptyHTML]);
const expectedResult = `import asyncio
-from playwright.async_api import Playwright, async_playwright
+from playwright.async_api import Playwright, async_playwright, expect
async def run(playwright: Playwright) -> None:
@@ -73,7 +73,7 @@ test('should print the correct context options when using a device and additiona
const cli = runCLI(['--color-scheme=light', '--device=iPhone 11', '--target=python-async', emptyHTML]);
const expectedResult = `import asyncio
-from playwright.async_api import Playwright, async_playwright
+from playwright.async_api import Playwright, async_playwright, expect
async def run(playwright: Playwright) -> None:
@@ -87,10 +87,10 @@ test('should save the codegen output to a file if specified', async ({ browserNa
const tmpFile = testInfo.outputPath('script.js');
const cli = runCLI(['--target=python-async', '--output', tmpFile, emptyHTML]);
await cli.exited;
- const content = await fs.readFileSync(tmpFile);
+ const content = fs.readFileSync(tmpFile);
expect(content.toString()).toBe(`import asyncio
-from playwright.async_api import Playwright, async_playwright
+from playwright.async_api import Playwright, async_playwright, expect
async def run(playwright: Playwright) -> None:
@@ -127,7 +127,7 @@ test('should print load/save storage_state', async ({ browserName, channel, runC
const cli = runCLI([`--load-storage=${loadFileName}`, `--save-storage=${saveFileName}`, '--target=python-async', emptyHTML]);
const expectedResult1 = `import asyncio
-from playwright.async_api import Playwright, async_playwright
+from playwright.async_api import Playwright, async_playwright, expect
async def run(playwright: Playwright) -> None:
diff --git a/tests/inspector/cli-codegen-python.spec.ts b/tests/inspector/cli-codegen-python.spec.ts
index fe8ccd67b9..594483a92d 100644
--- a/tests/inspector/cli-codegen-python.spec.ts
+++ b/tests/inspector/cli-codegen-python.spec.ts
@@ -25,7 +25,7 @@ const launchOptions = (channel: string) => {
test('should print the correct imports and context options', async ({ runCLI, channel, browserName }) => {
const cli = runCLI(['--target=python', emptyHTML]);
- const expectedResult = `from playwright.sync_api import Playwright, sync_playwright
+ const expectedResult = `from playwright.sync_api import Playwright, sync_playwright, expect
def run(playwright: Playwright) -> None:
@@ -37,7 +37,7 @@ def run(playwright: Playwright) -> None:
test('should print the correct context options for custom settings', async ({ runCLI, channel, browserName }) => {
const cli = runCLI(['--color-scheme=light', '--target=python', emptyHTML]);
- const expectedResult = `from playwright.sync_api import Playwright, sync_playwright
+ const expectedResult = `from playwright.sync_api import Playwright, sync_playwright, expect
def run(playwright: Playwright) -> None:
@@ -51,7 +51,7 @@ test('should print the correct context options when using a device', async ({ br
test.skip(browserName !== 'chromium');
const cli = runCLI(['--device=Pixel 2', '--target=python', emptyHTML]);
- const expectedResult = `from playwright.sync_api import Playwright, sync_playwright
+ const expectedResult = `from playwright.sync_api import Playwright, sync_playwright, expect
def run(playwright: Playwright) -> None:
@@ -65,7 +65,7 @@ test('should print the correct context options when using a device and additiona
test.skip(browserName !== 'webkit');
const cli = runCLI(['--color-scheme=light', '--device=iPhone 11', '--target=python', emptyHTML]);
- const expectedResult = `from playwright.sync_api import Playwright, sync_playwright
+ const expectedResult = `from playwright.sync_api import Playwright, sync_playwright, expect
def run(playwright: Playwright) -> None:
@@ -80,7 +80,7 @@ test('should save the codegen output to a file if specified', async ({ runCLI, c
const cli = runCLI(['--target=python', '--output', tmpFile, emptyHTML]);
await cli.exited;
const content = fs.readFileSync(tmpFile);
- expect(content.toString()).toBe(`from playwright.sync_api import Playwright, sync_playwright
+ expect(content.toString()).toBe(`from playwright.sync_api import Playwright, sync_playwright, expect
def run(playwright: Playwright) -> None:
@@ -111,7 +111,7 @@ test('should print load/save storage_state', async ({ runCLI, channel, browserNa
const saveFileName = testInfo.outputPath('save.json');
await fs.promises.writeFile(loadFileName, JSON.stringify({ cookies: [], origins: [] }), 'utf8');
const cli = runCLI([`--load-storage=${loadFileName}`, `--save-storage=${saveFileName}`, '--target=python', emptyHTML]);
- const expectedResult1 = `from playwright.sync_api import Playwright, sync_playwright
+ const expectedResult1 = `from playwright.sync_api import Playwright, sync_playwright, expect
def run(playwright: Playwright) -> None: