简体   繁体   English

你如何在柏树中获得元素的数量作为数字?

[英]How do you get the number of elements as an number in cypress?

Let's say I have three spans in a div.假设我在一个 div 中有三个跨度。

<div>
  <span></span>
  <span></span>
  <span></span>
</div>

And let's say I have some janky code that counts the number of those spans and outputs the value somewhere else假设我有一些 janky 代码来计算这些跨度的数量并在其他地方输出值

<p>3</p>

In cypress I want to get the length of those spans and get the value that I've produced in my janky code and compare the two.在柏树中,我想获得这些跨度的长度并获得我在我的 janky 代码中产生的值并比较两者。

I've tried cy.get('div span').its('length') , but that yields the spans as an object...我试过cy.get('div span').its('length') ,但这会产生跨度作为一个对象......

You can do something like this.你可以做这样的事情。 its('length') will return a number. its('length')将返回一个数字。 <p>3</p> on invoking text gives a string, adding a + in front will convert it into a number +count . <p>3</p>在调用文本时给出一个字符串,在前面添加一个 + 会将其转换为一个数字+count

cy.get('div span')
  .its('length')
  .then((len) => {
    cy.get('p')
      .invoke('text')
      .then((count) => {
        expect(len).to.equal(+count)
      })
  })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM