如何使用Playwright从下拉列表中断言预选值的值?打字
我使用下面的代码来断言下拉列表的预选值。
const dropdownValue = await this.page.$eval<string, HTMLSelectElement>("#recommendations_recommendation", ele => ele.value);
expect(dropdownValue).toBe("Approve");
我在控制台中得到以下错误:-
Error: expect(received).toBe(expected) // Object.is equality
Expected: "Approve"
Received: ""
Web元素定位器是正确的,即CSS路径。不知道为什么它会给我带来错误。有人能帮我在这里得到下拉列表的值吗?
您可以这样做:
const locator = page.locator('#recommendations_recommendation');
await expect(locator).toHaveValue('Approve');
您可以尝试断言内部文本:
const locator = page.locator('#recommendations_recommendation');
await expect(locator).toHaveText('Approve');