当我使用剧作家的codegen特性时,它会将我的点击路径跟踪到Java文件中。但是创建的文件语法错误,所以我无法编译它。
我以以下方式开始编码:
mvn exec:java -e -Dexec.mainClass=com.microsoft.playwright.CLI -Dexec.args="codegen wikipedia.org"检查员提供了以下代码:
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");
}
}
}

但已经有了第一个错误。方法getByRole需要一个AriaRole作为它的第一个参数,而不是字符串。所以它很容易修复,但我认为这不是产品生成代码并让开发人员修复它的想法。
在一些YouTube教程中,检查器只生成填充和单击函数,里面有强大的选择器。
是否有办法将生成的输出更改为特定的“代码样式”?或者,还有另一个原因,为什么其他人得到了良好的工作代码,而我却没有?
我的依赖性:
<dependency>
<groupId>com.microsoft.playwright</groupId>
<artifactId>playwright</artifactId>
<version>1.27.0</version>
</dependency>发布于 2022-10-23 22:41:40
<dependency>
<groupId>com.microsoft.playwright</groupId>
<artifactId>playwright</artifactId>
<version>1.17.1</version>我利用了这种依赖,它对我起作用
发布于 2022-10-26 07:43:24
对不起,如果我错了。但是你应该从一个编译良好的检查员那里得到这样的信息。
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");
}
}
}https://stackoverflow.com/questions/74125792
复制相似问题