首页 > 解决方案 > testcafe 高阶选择器

问题描述

我正在尝试创建一个高阶选择器,我可以通过更改文本参数来重用它。在下面的代码段中,当我尝试执行t.clickusingactualSelector时,它没有按预期工作。当我控制台记录 的值时actualSelector,我看到打印了整个项目值。

似乎我没有以正确的方式使用它。你能帮我解决这个问题吗?

例如

const testItemNameGenericSelector = (itemName) =>
    Selector(
      ".ms-Callout-container .ms-Callout-main  div div"
    )
  .withText(itemName);
...
  const itemNameToSelect = "Test Item-8ab1ec12-e719-4ab6-a0a3-ed538143d6d3";
  const actualSelector = testItemNameGenericSelector(itemNameToSelect)
...
  console.log(`selecting ${await actualSelector().textContent}`)

以下是此测试的完整 testcafe 代码。


fixture`Getting Started`
  .page // declare the fixture
`https://hasans30.github.io/testpage/dropdown.html`; // specify the start page

//then create a test and place your code there
test("My first test", async (t) => {

   const testItemNameGenericSelector = (itemName) =>
    Selector(
      ".ms-Callout-container .ms-Callout-main  div div"
    )
  .withText(itemName);
  const buttonSelector = Selector('.ms-Button-label');
  const selectedValue = Selector('.ms-Dropdown-title');

  const itemNameToSelect = "Test Item-8ab1ec12-e719-4ab6-a0a3-ed538143d6d3";
  const actualSelector = testItemNameGenericSelector(itemNameToSelect);


  await t.click(buttonSelector,{speed:0.51})
  console.log(`selecting ${await actualSelector().textContent}`)

  await t.click(actualSelector,{speed:0.51})
  await t.expect(await selectedValue().textContent).eql(itemNameToSelect);

});

标签: javascripttestingautomated-testsselectortestcafe

解决方案


推荐阅读