testing - testcafe如何选择条件元素

标签 testing automation automated-tests e2e-testing testcafe

我无法选择有条件地出现在页面上的元素。 我试过 awaiting 但它没有用。

// Gets imported as detailedProductPage 
export default class Page {
  constructor () {
    this.chipItem0 = Selector('[data-test-id="chipItem0"]').child('.tag-name').child('[data-test-id="tagValue"]');
  }
}


test('should accept value and allow for making the selection of multiple     items.', async t => {
  const string0 = 'Professionelle Nassreinigung nicht erlaubt';
  const string1 = 'Handwäsche';
  const string2 = 'Waschen 30°C';

  await t
    .click(detailedProductPage.listContainerFirstChild)

    .typeText(detailedProductPage.symbols, string0)
    .click(detailedProductPage.symbolsResultsItem0)
    .expect(string0).eql(detailedProductPage.chipItem0.innerText)

    .typeText(detailedProductPage.symbols, string1)
    .click(detailedProductPage.symbolsResultsItem0)
    .expect(string1).eql(detailedProductPage.chipItem1.innerText)

    .typeText(detailedProductPage.symbols, string2)
    .click(detailedProductPage.symbolsResultsItem1)
    .expect(string2).eql(detailedProductPage.chipItem2.innerText);
});    

enter image description here

enter image description here

最佳答案

您可以使用 exists 属性来检查该元素是否存在于页面上。有了这个,您可以点击有条件地出现在页面上的元素:

const el = Selector('#el');

if(await el.exists)
    await t.click(el);

为了让你的测试正确,你需要修正你的断言。根据TestCafe Assertions API eql 断言应按以下方式使用:

await t.expect( actual ).eql( expected, message, options );

TestCafe 允许用户将异步 Selector 属性作为 actual 参数传递。这些属性表示测试页面上相关 DOM 元素的状态。在您的例子中,实际值为 detailedProductPage.chipItem0.innerTextexpected 值不能是异步属性,它应该是计算值(如字符串、 bool 值、数字或某些对象等)。 以下代码应该可以正常工作:

await t
    .click(detailedProductPage.listContainerFirstChild)
    .typeText(detailedProductPage.symbols, string0)
    .click(detailedProductPage.symbolsResultsItem0)
    .expect(detailedProductPage.chipItem0.innerText).eql(string0);

关于testing - testcafe如何选择条件元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45984567/

相关文章:

testing - 将测试模块移动到单独的文件时未定义宏

unit-testing - 测试通过,但之后给出断言错误

sql - 自动将 View 另存为带有额外列的表

angularjs - Angular 应用程序的最大圈复杂度

c# - SpecFlow IObjectContainer : How to register and consume multiple objects of the same type?

java - TestNG:在类路径中找不到类,testng.xml

testing - Nancy.Testing 似乎很慢,我应该做些什么来提高性能?

docker - 在 Docker Compose 中自动创建网络

ruby - Watir,通过自定义属性定位元素

c# - 命令行自动化——期待等效