Home > Enterprise >  Why does Playwright's generated Java code have invalid syntax?
Why does Playwright's generated Java code have invalid syntax?

Time:10-27

When I use the Playwright's codegen feature it traces my clickpath into a Java file. But the created file has the wrong syntax, so I can't compile it.

I start the codegen with:

mvn exec:java -e -Dexec.mainClass=com.microsoft.playwright.CLI -Dexec.args="codegen wikipedia.org"

And the inspector provides this code:

public class Example {
  public static void main(String[] args) {
    try (Playwright playwright = Playwright.create()) {
      Browser browser = playwright.chromium().launch(new BrowserType.LaunchOptions()
        .setHeadless(false));

      BrowserContext context = browser.newContext();
      page.navigate("https://en.wikipedia.org/wiki/Main_Page");
      page.getByPlaceholder("Search Wikipedia").click();
      page.getByPlaceholder("Search Wikipedia").fill("stackoverflow");
      page.getByRole("button", new Page.GetByRoleOptions().setName("Go")).click();
      assertThat(page).hasURL("https://en.wikipedia.org/wiki/Stack_Overflow");
    }
  }
}

enter image description here

But there is already the first error. The method getByRole requires an AriaRole as its first parameter, not a String. So it's easy to fix, but I think it's not the idea of the product to generate code and let the developer fix it.

In some YouTube tutorials the inspector generates only fill and click functions with powerful selectors inside.

Is there a way to change the generated output to a specifc "code-style"? Or is there another reason why other people get nice working code and I don't?

My dependency:

<dependency>
    <groupId>com.microsoft.playwright</groupId>
    <artifactId>playwright</artifactId>
    <version>1.27.0</version>
</dependency>

CodePudding user response:

<dependency>
<groupId>com.microsoft.playwright</groupId>
<artifactId>playwright</artifactId>
<version>1.17.1</version>

i used this dependency it worked with me

CodePudding user response:

Sorry if I am wrong. But you should get something like this from an inspector which compiles fine

        
package org.example;
import com.microsoft.playwright.*;
import com.microsoft.playwright.options.*;
import static com.microsoft.playwright.assertions.PlaywrightAssertions.assertThat;
import java.util.*;
public class Example {
    public static void main(String[] args) {
        try (Playwright playwright = Playwright.create()) {
            Browser browser = playwright.chromium().launch(new BrowserType.LaunchOptions()
                    .setHeadless(false));
            BrowserContext context = browser.newContext();
            // Open new page
            Page page = context.newPage();
            // Go to https://www.wikipedia.org/
            page.navigate("https://www.wikipedia.org/");
            // Click input[name="search"]
            page.locator("input[name=\"search\"]").click();
            // Fill input[name="search"]
            page.locator("input[name=\"search\"]").fill("stackoverflow");
            // Click button:has-text("Search")
            page.locator("button:has-text(\"Search\")").click();
            assertThat(page).hasURL("https://en.wikipedia.org/wiki/Stack_Overflow");
        }
    }
}
  • Related