【问题标题】:How to use result of .length in selector Cypress如何在选择器 Cypress 中使用 .length 的结果
【发布时间】:2018-08-21 07:17:03
【问题描述】:

我正在尝试根据先前断言的结果执行操作。我有以下情况,我想确定一个文档的版本数,这在下面的html中表示:

<ul data-testhook-id="accordion-body">
    <li data-testhook-id="accordion-body-item">
            <div>
                    <div data-testhook-id="version-1">
                        <div data-testhook-id="avatar">
                            <div data-id="tooltip">John Doe</div>
                            </div>
                        </div>
                        <span data-testhook-id="content">1. 17-08-2018 at 15:26</span>
                    </div>
                    <div data-testhook-id="version-2">
                        <div data-testhook-id="avatar">
                            <div data-id="tooltip">John Doe</div>
                            </div>
                        </div>
                        <span data-testhook-id="content">2. 20-08-2018 at 13:05</span>
                    </div>
                    <div data-testhook-id="version-3">
                        <div data-testhook-id="avatar">
                            <div data-id="tooltip">Jane Doe</div>
                            </div>
                        </div>
                        <span data-testhook-id="content">3. 20-08-2018 at 13:11</span>
                    </div>
                     <div data-testhook-id="version-4">
                        <div data-testhook-id="avatar">
                            <div data-id="tooltip">No Body</div>
                            </div>
                        </div>
                        <span data-testhook-id="content">4. 21-08-2018 at 13:11</span>
                    </div>
                    <svg width="12" height="12" data-testhook-id="icon-active-version"><path d="path-here"></path></svg>
                    </div>
            </div>
    </li>

每个带有测试 id data-testhook-id="version-xxx" 的元素都是一行,其中包含版本信息以及我想要计算的这些 div 元素。为此,我设置了以下内容:

cy.get('[data-testhook-id="accordion-body-item"] > div > div').its('length').as('versions')

现在如果我添加以下断言,这将毫无问题地通过

cy.get('@versions').should('equal', 4)

但是,如果我尝试使用在 console.log 中找到的 div 数量的结果作为变量,我不再得到“4”而是 [object, Object]。试过这个:

var size = cy.get('[data-testhook-id="asset-versions"] [data-testhook-id="accordion-body-item"] > div > div').its('length')
console.log('foo ' + size)

这导致foo [object Object] 被打印到控制台。

我想要做的是使用选择器中的项目数来选择一个元素,例如:

cy.get('[data-testhook-id="accordion-body-item"] > div > div:nth-child(' + size + ')').find('svg').should('have.attr', 'data-testhook-id', 'icon-active-version')

但由于 var 不是数字,所以我得到了 msg

Error: Syntax error, unrecognized expression: :nth-child
[data-testhook-id="asset-versions"] [data-testhook-id="accordion-body-item"] > div > div:nth-child([object Object])

是否可以将找到的元素数量用作下一个选择器的变量?

【问题讨论】:

    标签: jquery-selectors cypress


    【解决方案1】:

    试试这个:

    cy.get('[data-testhook-id="accordion-body-item"] > div > div').its('length').then((size) => {
       cy.get('[data-testhook-id="accordion-body-item"] > div > div:nth-child(' + size + ')').find('svg').should('have.attr', 'data-testhook-id', 'icon-active-version')
    });
    

    您不能分配或使用任何赛普拉斯命令的返回值。命令被排队并异步运行。真正返回的命令是Chainable&lt;typeOfValueYouWant&gt;,换句话说,它是一种以期望值解析的队列对象。 阅读更多here

    顺便说一句:我认为您应该考虑改变选择元素的方法。阅读更多here

    对于那些使用 Typescript 的人 - 我为 tslint 编写了插件以防止犯此错误:https://github.com/krzysztof-grzybek/tslint-plugin-cypress

    【讨论】:

    • 谢谢,这对我有用。我也会看看你包含的链接。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-29
    • 1970-01-01
    • 1970-01-01
    • 2020-12-04
    • 1970-01-01
    相关资源
    最近更新 更多