63 行
2.2 KiB
TypeScript
63 行
2.2 KiB
TypeScript
import { expect, type Locator } from "@playwright/test";
|
|
|
|
export async function selectOptionByText(locator: Locator, pattern: RegExp) {
|
|
await expect
|
|
.poll(async () => {
|
|
return locator.locator("option").evaluateAll((options, rawPattern) => {
|
|
const regexp = new RegExp(rawPattern as string);
|
|
return options
|
|
.map((option) => ({
|
|
value: (option as HTMLOptionElement).value,
|
|
label: (option as HTMLOptionElement).label,
|
|
disabled: (option as HTMLOptionElement).disabled,
|
|
}))
|
|
.filter((option) => option.value && !option.disabled && regexp.test(option.label))
|
|
.map((option) => option.value)[0] ?? "";
|
|
}, pattern.source);
|
|
})
|
|
.not.toBe("");
|
|
|
|
const optionValue = await locator.locator("option").evaluateAll((options, rawPattern) => {
|
|
const regexp = new RegExp(rawPattern as string);
|
|
return options
|
|
.map((option) => ({
|
|
value: (option as HTMLOptionElement).value,
|
|
label: (option as HTMLOptionElement).label,
|
|
disabled: (option as HTMLOptionElement).disabled,
|
|
}))
|
|
.filter((option) => option.value && !option.disabled && regexp.test(option.label))
|
|
.map((option) => option.value)[0] ?? "";
|
|
}, pattern.source);
|
|
|
|
await locator.selectOption(optionValue);
|
|
}
|
|
|
|
export async function selectFirstBusinessOption(locator: Locator) {
|
|
await expect
|
|
.poll(async () => {
|
|
const values = await locator.locator("option").evaluateAll((options) =>
|
|
options
|
|
.map((option) => ({
|
|
value: (option as HTMLOptionElement).value,
|
|
disabled: (option as HTMLOptionElement).disabled,
|
|
}))
|
|
.filter((option) => option.value && !option.disabled)
|
|
.map((option) => option.value),
|
|
);
|
|
return values[0] ?? "";
|
|
})
|
|
.not.toBe("");
|
|
|
|
const values = await locator.locator("option").evaluateAll((options) =>
|
|
options
|
|
.map((option) => ({
|
|
value: (option as HTMLOptionElement).value,
|
|
disabled: (option as HTMLOptionElement).disabled,
|
|
}))
|
|
.filter((option) => option.value && !option.disabled)
|
|
.map((option) => option.value),
|
|
);
|
|
|
|
await locator.selectOption(values[0]);
|
|
}
|