【问题标题】:testcafe how to make the selection of the conditional elementtestcafe 如何进行条件元素的选择
【发布时间】:2018-02-09 14:15:00
【问题描述】:

我在选择有条件地出现在页面上的元素时遇到问题。 我试过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);
});    

【问题讨论】:

    标签: testing automation automated-tests e2e-testing testcafe


    【解决方案1】:

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

    const el = Selector('#el');
    
    if(await el.exists)
        await t.click(el);
    

    为了使您的测试正确,您需要修复您的断言。根据TestCafe Assertions APIeql 断言应按以下方式使用:

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

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

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

     

    【讨论】:

    • 感谢您帮助 AlexanderMos :)
    猜你喜欢
    • 1970-01-01
    • 2011-03-28
    • 1970-01-01
    • 1970-01-01
    • 2023-02-21
    • 1970-01-01
    • 2021-09-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多