【问题标题】:In cypress testing how to use value of a selector partially?在柏树测试中如何部分使用选择器的值?
【发布时间】:2022-11-12 13:17:30
【问题描述】:

所以我有这些选择器:

cy.get(':nth-child(1) > abc-51 > xyz')
cy.get(':nth-child(2) > abc-51 > xyz')
cy.get(':nth-child(3) > abc-51 > xyz')

我如何只使用cy.get(':nth-child(1)')cy.get(':nth-child(2)') 而不是整个事情,因为问题是abc-51 不断变化,就像它会变成abc-43 等等,所以测试不断失败。

【问题讨论】:

    标签: javascript cypress


    【解决方案1】:

    Adding ^ in your selector will match on selectors that begin with that value. 在您的示例中,以下内容应该有效:

    cy.get(':nth-child(1) > [whatever-attribute^="abc-"] > xyz');
    

    【讨论】:

      【解决方案2】:

      选择器的每个部分都是不同的元素级别,它们之间有> 意味着“父子”关系。

      但是,如果您省略 > 并且只在选择器之间使用空格,它应该仍然可以工作,因为您现在有“父-孙”(在您的情况下),或者通常是“父-后代”关系。

      因此,只需将其用作您的部分选择器:

      cy.get(':nth-child(1) xyz')
      

      这是另一个供参考的问题:
      What is the difference direct descendent (>) vs. descendant in jQuery selectors?


      请注意,您还可以使用 Cypress .find() 进行后代搜索:

      cy.get(':nth-child(1)).find('xyz')
      

      【讨论】:

        猜你喜欢
        • 2018-08-09
        • 1970-01-01
        • 2020-07-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-02-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多