我在 playwright 中使用页面对象模型开发自动化测试。因此,我正在构建一个类来保存我的定位器并公开定位器,但不一定是使用的选择器。定位器是否可以共享其选择器?
exports.MyWebPageModel = class MyWebPageModel {
constructor(page) {
this.myMultiSelect = page.locator('#select-group select');
this.submitButton = page.locator('#submit-btn');
}
}
test('validate multi-select submission', ({page}) -> {
const myPage = new MyWebPageModel(page);
const selectChoices = ['choice1', 'choice2', 'choice4'];
await myPage.myMultiSelect.selectOptions(selectedChoices);
Promise.all([
page.waitForNavigation(),
myPage.submitButton.click()
]);
/* do tests on new page, click it's back button to return to previous page */
const allSelectedValues = await page.$eval(myPage.myMultiSelect.???, e => Array.from(e.selectedOptions).map(option => option.value)); // get the selected options from select element
expect(allSelectedValues).toEqual(selectedChoices); // verify the selected options matches selectChoices.
});
最佳答案
这是一种方式,但不确定它是否适用于所有可能的定位器!
// Get a selector from a playwright locator
import { Locator } from "@playwright/test";
export function extractSelector(locator: Locator) {
const selector = locator.toString();
const parts = selector.split("@");
if (parts.length !== 2) { throw Error("extractSelector: susupect that this is not a locator"); }
if (parts[0] !== "Locator") { throw Error("extractSelector: did not find locator"); }
return parts[1];
}
关于javascript - 是否可以从剧作家的定位器对象中获取选择器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72044959/