angular - Playwright - 如果我有多个同名下拉菜单,如何使用定位器访问 Angular 2 路绑定(bind)模型值?

标签 angular playwright

我有多个同名的下拉菜单,我正在尝试访问当前选定的值,这些值是使用 Angular 进行双向绑定(bind)的。我正在尝试比较当前显示的值是否全部设置为如图所示完成:Validate dropdowns are set to Complete

浏览器当前看到的html元素是:

<select _ngcontent-pis-c119="" name="dispo" class="dash-select ng-pristine ng-valid ng-touched"><option _ngcontent-pis-c119="" value="Active" class="ng-star-inserted"> Active </option><option _ngcontent-pis-c119="" value="On Hold" class="ng-star-inserted"> On Hold </option><option _ngcontent-pis-c119="" value="Completed" class="ng-star-inserted"> Completed </option><option _ngcontent-pis-c119="" value="Canceled" class="ng-star-inserted"> Canceled </option><!----></select>

我当前的代码能够获取下拉列表元素的集合,但我不确定如何访问每个下拉列表的 ngModel 值。我的代码当前是:

// Get the collection of Disposition elements
    const dispositionsCompleted = page.locator('select[name="dispo"]');
    // Check that all Dispositions are set to Complete
    const count = await dispositionsCompleted.count();

    for (let i = 0; i < count; i++) {   
        console.log(await dispositionsCompleted.nth(i).locator('option').inputValue());
        
    }

最佳答案

如果您的列表不是动态的,因此您始终具有相同数量的选择元素,并且您不想为选择器设置不同的 id,则可以使用 :nth-match选择器进入列表并逐行检查值。

示例:

// wait for the selector
await page.waitForSelector(':nth-match("select", 0)');

// Check the value
await expect(page.locator('option[selected="selected"]')).toHaveText("Value");

更好的解决方案:

await page.waitForSelector("select");

// get all the select
const dropdowns = await page.$$("select");
console.log(dropdowns.length);
console.log(await dropdowns[0].innerText());
console.log(await dropdowns[1].innerText());

// the expect() comes here

剧作家:第n场比赛伪选择器

https://playwright.dev/docs/selectors#pick-n-th-match-from-the-query-result

关于angular - Playwright - 如果我有多个同名下拉菜单,如何使用定位器访问 Angular 2 路绑定(bind)模型值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73477425/

相关文章:

c# - 使用 Microsoft Playwright 在 Azure Function Linux 消费计划中创建本地 PDF 文件

Angular4 @Input() 与 Component.inputs 数组

angular - 在 AngularJS 2 中,当使用路由配置调用子组件时,管理父组件和子组件之间的输入/输出属性的方法是什么

Javascript 循环、异步函数和 headless 浏览器

node.js - 剧作家无法拦截所有请求

ssl - 使用 playwright 代码生成忽略 SSL 错误

css - 从 MS Playwright 中的定位器页面对象获取 CSS 字符串值

angular - 找不到管道 ' ' - Angular 升级后出错

angular - 测试 Angular snackbar

angular - 检查是否在路由更改时调用了 ngOnDestroy