/** * Copyright (c) Microsoft Corporation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import type { TestServer } from 'tests/config/testserver'; import type { Recorder } from './inspectorTest'; import { test, expect } from './inspectorTest'; import type { Page } from '@playwright/test'; test.describe('cli codegen', () => { test.skip(({ mode }) => mode !== 'default'); test.skip(({ trace, codegenMode }) => trace === 'on' && codegenMode === 'trace-events'); test('should click locator.first', async ({ openRecorder }) => { const { page, recorder } = await openRecorder(); await recorder.setContentAndWait(` `); const locator = await recorder.hoverOverElement('button'); expect(locator).toBe(`getByRole('button', { name: 'Submit' }).first()`); const [message, sources] = await Promise.all([ page.waitForEvent('console', msg => msg.type() !== 'error'), recorder.waitForOutput('JavaScript', 'click'), recorder.trustedClick() ]); expect.soft(sources.get('JavaScript')!.text).toContain(` await page.getByRole('button', { name: 'Submit' }).first().click();`); expect.soft(sources.get('Python')!.text).toContain(` page.get_by_role("button", name="Submit").first.click()`); expect.soft(sources.get('Python Async')!.text).toContain(` await page.get_by_role("button", name="Submit").first.click()`); expect.soft(sources.get('Java')!.text).toContain(` page.getByRole(AriaRole.BUTTON, new Page.GetByRoleOptions().setName("Submit")).first().click();`); expect.soft(sources.get('C#')!.text).toContain(` await page.GetByRole(AriaRole.Button, new() { Name = "Submit" }).First.ClickAsync();`); expect(message.text()).toBe('click1'); }); test('should click locator.nth', async ({ openRecorder }) => { const { page, recorder } = await openRecorder(); await recorder.setContentAndWait(` `); const locator = await recorder.hoverOverElement('button >> nth=1'); expect(locator).toBe(`getByRole('button', { name: 'Submit' }).nth(1)`); const [message, sources] = await Promise.all([ page.waitForEvent('console', msg => msg.type() !== 'error'), recorder.waitForOutput('JavaScript', 'click'), recorder.trustedClick() ]); expect.soft(sources.get('JavaScript')!.text).toContain(` await page.getByRole('button', { name: 'Submit' }).nth(1).click();`); expect.soft(sources.get('Python')!.text).toContain(` page.get_by_role("button", name="Submit").nth(1).click()`); expect.soft(sources.get('Python Async')!.text).toContain(` await page.get_by_role("button", name="Submit").nth(1).click()`); expect.soft(sources.get('Java')!.text).toContain(` page.getByRole(AriaRole.BUTTON, new Page.GetByRoleOptions().setName("Submit")).nth(1).click();`); expect.soft(sources.get('C#')!.text).toContain(` await page.GetByRole(AriaRole.Button, new() { Name = "Submit" }).Nth(1).ClickAsync();`); expect(message.text()).toBe('click2'); }); test('should generate frame locators (1)', async ({ openRecorder, server }) => { const { page, recorder } = await openRecorder(); const { frameHello1 } = await createFrameHierarchy(page, recorder, server); const [sources] = await Promise.all([ recorder.waitForOutput('JavaScript', 'Hello1'), frameHello1.click('text=Hello1'), ]); expect.soft(sources.get('JavaScript')!.text).toContain(` await page.locator('#frame1').contentFrame().getByText('Hello1').click();`); expect.soft(sources.get('Java')!.text).toContain(` page.locator("#frame1").contentFrame().getByText("Hello1").click();`); expect.soft(sources.get('Python')!.text).toContain(` page.locator("#frame1").content_frame.get_by_text("Hello1").click()`); expect.soft(sources.get('Python Async')!.text).toContain(` await page.locator("#frame1").content_frame.get_by_text("Hello1").click()`); expect.soft(sources.get('C#')!.text).toContain(` await page.Locator("#frame1").ContentFrame.GetByText("Hello1").ClickAsync();`); }); test('should generate frame locators (2)', async ({ openRecorder, server }) => { const { page, recorder } = await openRecorder(); const { frameHello2 } = await createFrameHierarchy(page, recorder, server); const [sources] = await Promise.all([ recorder.waitForOutput('JavaScript', 'Hello2'), frameHello2.click('text=Hello2'), ]); expect.soft(sources.get('JavaScript')!.text).toContain(` await page.locator('#frame1').contentFrame().locator('iframe').contentFrame().getByText('Hello2').click();`); expect.soft(sources.get('Java')!.text).toContain(` page.locator("#frame1").contentFrame().locator("iframe").contentFrame().getByText("Hello2").click();`); expect.soft(sources.get('Python')!.text).toContain(` page.locator("#frame1").content_frame.locator("iframe").content_frame.get_by_text("Hello2").click()`); expect.soft(sources.get('Python Async')!.text).toContain(` await page.locator("#frame1").content_frame.locator("iframe").content_frame.get_by_text("Hello2").click()`); expect.soft(sources.get('C#')!.text).toContain(` await page.Locator("#frame1").ContentFrame.Locator("iframe").ContentFrame.GetByText("Hello2").ClickAsync();`); }); test('should generate frame locators (3)', async ({ openRecorder, server }) => { const { page, recorder } = await openRecorder(); const { frameAnonymous } = await createFrameHierarchy(page, recorder, server); const [sources] = await Promise.all([ recorder.waitForOutput('JavaScript', 'HelloNameAnonymous'), frameAnonymous.click('text=HelloNameAnonymous'), ]); expect.soft(sources.get('JavaScript')!.text).toContain(` await page.locator('#frame1').contentFrame().locator('iframe').contentFrame().locator('iframe').nth(2).contentFrame().getByText('HelloNameAnonymous').click();`); expect.soft(sources.get('Java')!.text).toContain(` page.locator("#frame1").contentFrame().locator("iframe").contentFrame().locator("iframe").nth(2).contentFrame().getByText("HelloNameAnonymous").click();`); expect.soft(sources.get('Python')!.text).toContain(` page.locator("#frame1").content_frame.locator("iframe").content_frame.locator("iframe").nth(2).content_frame.get_by_text("HelloNameAnonymous").click()`); expect.soft(sources.get('Python Async')!.text).toContain(` await page.locator("#frame1").content_frame.locator("iframe").content_frame.locator("iframe").nth(2).content_frame.get_by_text("HelloNameAnonymous").click()`); expect.soft(sources.get('C#')!.text).toContain(` await page.Locator("#frame1").ContentFrame.Locator("iframe").ContentFrame.Locator("iframe").Nth(2).ContentFrame.GetByText("HelloNameAnonymous").ClickAsync();`); }); test('should generate frame locators (4)', async ({ openRecorder, server }) => { const { page, recorder } = await openRecorder(); /* iframe div Hello1 iframe div Hello2 iframe[name=one] div HelloNameOne iframe dev HelloAnonymous */ await recorder.setContentAndWait(` `, server.EMPTY_PAGE, 1); const [sources] = await Promise.all([ recorder.waitForOutput('JavaScript', 'Click me'), page.locator('[title="hello world"]').contentFrame().getByRole('button', { name: 'Click me' }).click(), ]); expect.soft(sources.get('JavaScript')!.text).toContain( `await page.locator('iframe[title="hello world"]').contentFrame().getByRole('button', { name: 'Click me' }).click();` ); expect.soft(sources.get('Java')!.text).toContain( `page.locator(\"iframe[title=\\\"hello world\\\"]\").contentFrame().getByRole(AriaRole.BUTTON, new FrameLocator.GetByRoleOptions().setName(\"Click me\")).click();` ); expect.soft(sources.get('Python')!.text).toContain( `page.locator(\"iframe[title=\\\"hello world\\\"]\").content_frame.get_by_role(\"button\", name=\"Click me\").click()` ); expect.soft(sources.get('Python Async')!.text).toContain( `await page.locator("iframe[title=\\\"hello world\\\"]").content_frame.get_by_role("button", name="Click me").click()` ); expect.soft(sources.get('C#')!.text).toContain( `await page.Locator("iframe[title=\\\"hello world\\\"]").ContentFrame.GetByRole(AriaRole.Button, new() { Name = "Click me" }).ClickAsync();` ); }); test('should generate frame locators with name attribute', async ({ openRecorder, server }) => { const { page, recorder } = await openRecorder(); await recorder.setContentAndWait(` `, server.EMPTY_PAGE, 1); const [sources] = await Promise.all([ recorder.waitForOutput('JavaScript', 'Click me'), page.locator('[name="hello world"]').contentFrame().getByRole('button', { name: 'Click me' }).click(), ]); expect.soft(sources.get('JavaScript')!.text).toContain( `await page.locator('iframe[name="hello world"]').contentFrame().getByRole('button', { name: 'Click me' }).click();` ); expect.soft(sources.get('Java')!.text).toContain( `page.locator(\"iframe[name=\\\"hello world\\\"]\").contentFrame().getByRole(AriaRole.BUTTON, new FrameLocator.GetByRoleOptions().setName(\"Click me\")).click();` ); expect.soft(sources.get('Python')!.text).toContain( `page.locator(\"iframe[name=\\\"hello world\\\"]\").content_frame.get_by_role(\"button\", name=\"Click me\").click()` ); expect.soft(sources.get('Python Async')!.text).toContain( `await page.locator("iframe[name=\\\"hello world\\\"]").content_frame.get_by_role("button", name="Click me").click()` ); expect.soft(sources.get('C#')!.text).toContain( `await page.Locator("iframe[name=\\\"hello world\\\"]").ContentFrame.GetByRole(AriaRole.Button, new() { Name = "Click me" }).ClickAsync();` ); }); test('should generate frame locators with id attribute', async ({ openRecorder, server }) => { const { page, recorder } = await openRecorder(); await recorder.setContentAndWait(` `, server.EMPTY_PAGE, 1); const [sources] = await Promise.all([ recorder.waitForOutput('JavaScript', 'Click me'), page.locator('[id="hello-world"]').contentFrame().getByRole('button', { name: 'Click me' }).click(), ]); expect.soft(sources.get('JavaScript')!.text).toContain( `await page.locator('#hello-world').contentFrame().getByRole('button', { name: 'Click me' }).click();` ); expect.soft(sources.get('Java')!.text).toContain( `page.locator(\"#hello-world\").contentFrame().getByRole(AriaRole.BUTTON, new FrameLocator.GetByRoleOptions().setName(\"Click me\")).click();` ); expect.soft(sources.get('Python')!.text).toContain( `page.locator(\"#hello-world\").content_frame.get_by_role(\"button\", name=\"Click me\").click()` ); expect.soft(sources.get('Python Async')!.text).toContain( `await page.locator("#hello-world").content_frame.get_by_role("button", name="Click me").click()` ); expect.soft(sources.get('C#')!.text).toContain( `await page.Locator("#hello-world").ContentFrame.GetByRole(AriaRole.Button, new() { Name = "Click me" }).ClickAsync();` ); }); test('should generate frame locators with testId', async ({ openRecorder, server }) => { const { page, recorder } = await openRecorder(); await recorder.setContentAndWait(` `, server.EMPTY_PAGE, 1); const [sources] = await Promise.all([ recorder.waitForOutput('JavaScript', 'my-testid'), page.locator('iframe[data-testid="my-testid"]').contentFrame().getByRole('button', { name: 'Click me' }).click(), ]); expect.soft(sources.get('JavaScript')!.text).toContain( `await page.locator('[data-testid="my-testid"]').contentFrame().getByRole('button', { name: 'Click me' }).click();` ); expect.soft(sources.get('Java')!.text).toContain( `page.locator(\"[data-testid=\\\"my-testid\\\"]\").contentFrame().getByRole(AriaRole.BUTTON, new FrameLocator.GetByRoleOptions().setName(\"Click me\")).click();` ); expect.soft(sources.get('Python')!.text).toContain( `page.locator(\"[data-testid=\\\"my-testid\\\"]\").content_frame.get_by_role(\"button\", name=\"Click me\").click()` ); expect.soft(sources.get('Python Async')!.text).toContain( `await page.locator("[data-testid=\\\"my-testid\\\"]").content_frame.get_by_role("button", name="Click me").click()` ); expect.soft(sources.get('C#')!.text).toContain( `await page.Locator("[data-testid=\\\"my-testid\\\"]").ContentFrame.GetByRole(AriaRole.Button, new() { Name = "Click me" }).ClickAsync();` ); }); test('should generate role locators undef frame locators', async ({ openRecorder, server }) => { const { page, recorder } = await openRecorder(); await recorder.setContentAndWait(`'>"> `, server.EMPTY_PAGE, 6); const frameHello1 = page.mainFrame().childFrames()[0]; const frameHello2 = frameHello1.childFrames()[0]; const frameTwo = page.frame({ name: 'two' })!; await frameTwo.setContent(`
HelloNameTwo
`); const frameAnonymous = frameHello2.childFrames().find(f => !f.name())!; await frameAnonymous.setContent(`
HelloNameAnonymous
`); return { frameHello1, frameHello2, frameTwo, frameAnonymous, }; }