是否可以从剧作家中的定位器对象获取选择器?

Mac*_*tyn 6 javascript automation ui-automation playwright

我正在使用页面对象模型来开发剧作家的自动化测试。因此,我正在构建一个类来保存我的定位器并公开定位器,但不一定是使用的选择器。定位器是否有办法共享其选择器?

exports.MyWebPageModel = class MyWebPageModel {
  constructor(page) {
    this.myMultiSelect = page.locator('#select-group select');
    this.submitButton = page.locator('#submit-btn');
  }
}
Run Code Online (Sandbox Code Playgroud)
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.
});
Run Code Online (Sandbox Code Playgroud)

Mac*_*tyn 5

通过执行以下操作:

const myLoc = page.locator('#myElement');
Object.getOwnPropertyNames(myLoc).forEach(prop => { 
  console.log(prop + " = " + row_locator[prop]); 
});
Run Code Online (Sandbox Code Playgroud)

我发现有一个_selector,它返回用于 Locator 对象的内部选择器。

所以答案是使用定位器的“私有”属性_selector。这是生成定位器错误时使用的值。

所以,给定一个表:

<table id="colors">
  <thead>
  <trow>
    <th>Key</th>
    <th>Color</th>
  </trow>
  </thead>
  <tbody>
  <trow>
    <td>R</td><td>Red</td>
  </trow>
  <trow>
    <td>B</td><td>Blue</td>
  </trow>
  <trow>
    <td>G</td><td>Green</td>
  </trow>
  </tbody>
</table>
Run Code Online (Sandbox Code Playgroud)

并且,也许您可​​以按如下方式构建剧作家对象:

exports.ColorTable = class ColorTable {
  constructor(page) {
    this.page = page;
    this.table = this.page.locator("#colors");
    this.tableData = this.table.locator('tbody tr');
  }
}
Run Code Online (Sandbox Code Playgroud)

结果如下

const myT = new ColorTable(page);
console.log(myT.tableData._selector);
Run Code Online (Sandbox Code Playgroud)

可能看起来像: Locator@#colors >> tbody tr