我一直使用“填充”功能在 Playwright 的文本框内输入内容,但这会导致自动化脚本运行速度过快——比人手还快。我希望输入速度能和人手一样快。有没有其他方法可以替代“填充”功能来填充文本框?
我正在使用 Sync API 和 Python 进行 Playwright 测试,并且我有一个 Python 变量,即我的定位器,已经找到。我需要知道这个定位器是什么类型的 HTML 元素,因为我需要对某些类型的元素进行一些额外的编码。
假设我在代码中这样做了:
my_locator = self.page.get_by_role('some_valid_role')
现在我想知道 my_locator 是 DIV 元素还是 Span 元素?我该怎么做?
我的代码中有以下元素
<sp-textfield id="library-name-textfield" data-testid="library-name-textfield" quiet="" placeholder="Name" dir="ltr" type="text" focusable="">
#shadow-root
<input class="input" type="text" aria-describedby="sp-help-text" aria-label="Yourname" placeholder="Your name">
</sp-textfield>
我尝试执行以下操作:
await this.inputBrandLibNameTxtBox().waitFor({ state: "visible", timeout: 20000 });
await this.inputBrandLibNameTxtBox().fill(brandName);
在哪里:
inputBrandLibNameTxtBox = () => this.page.getByTestId("library-name-textfield").locator("input");
我收到错误:
TimeoutError: locator.fill: Timeout 20000ms exceeded.
Call log:
- waiting for getByTestId('library-name-textfield').locator('input')
- locator resolved to <input type="text" class="input" aria-label="Your brand name" placeholder="Your brand name" aria-describedby="sp-help-text-2f4953d1"/>
- fill("new-brand--1733244070919")
- attempting fill action
- waiting for element to be visible, enabled and editable
我该如何修复此问题?
我想将项目定义保存在剧作家配置文件中,例如:
projects: [
{
name: 'chrome',
use: { browserName: 'chromium' },
},
{
name: 'firefox',
use: { browserName: 'firefox' },
}
....
]
但是,不要使用命令行选择项目
--项目=chrome
我想在我的 .env 文件中执行此操作。可以吗?
尝试覆盖 process.env.project 值,但没有用。我已经从 .env 文件中读取测试中其他变量的值了……
在 PlaywrightTestConfig 中,我们在使用部分设置了一个自定义值,即
export default defineConfig {
use: {
myKey: 'myvalue'
}
}
此值可能会在测试中被覆盖,我想让快照名称包含来自 myKey 的值,而无需编写测试的人添加快照名称,即默认选择快照名称,就像在项目配置中的快照路径模板中设置一样。因此,example.test.ts
使用 test called 调用的测试文件my test
将 expect(page).toHaveScreenshot()
生成一个快照文件名,类似于{testFileName}/{testFileDir}-{myKey}-{projectName}-{platform}.png
( example.test.ts-snapshots/my-test-myvalue-chromium-darwin.png
)
注意:我不想为每种组合都设立一个单独的项目,因为这样最终会有很多组合
在Playwright 组件测试中,有没有办法将标志传递给运行的浏览器实例?例如,对于 Chrome:--use-fake-ui-for-media-stream
、--use-file-for-fake-audio-capture
和类似的。
我正在尝试编写一个剧作家测试,该测试在提交表单后执行某些操作。提交表单后,会发生两件事:
- “抽屉”关闭
- 弹出“通知”
问题是,这两件事的顺序可能会因为随机性而改变。那么我是否这样做:
await expectDrawerToClose();
await expectNotification();
或者:
await expectNotification();
await expectDrawerToClose();
...无论哪种方式,有时都会出错。Playwright 有什么办法可以做到:
await expectTwoThingsToHappenInAnyOrder(
expectDrawerToClose(),
expectNotification()
);
我知道我可以将定位器与 链接在一起or
,但这不是同一件事:我不想说“我会看到这两件事之一发生”......我想说“我会看到两者都会发生;我只是不关心顺序。”