简体   繁体   English

如何遍历表并从行中提取值并与数据表进行比较

[英]How do I iterate through a table and extract the values from a row and compare with datatable

I am trying to fill in a form and save the details then the same details get displayed in a table row.我正在尝试填写表格并保存详细信息,然后在表格行中显示相同的详细信息。 I need to iterate through the table row and extract all the values (Value1, Value2, Value3) from the row and then compare with the input values.我需要遍历表行并从行中提取所有值(Value1、Value2、Value3),然后与输入值进行比较。 How do I do this using cypress.我如何使用柏树做到这一点。


<tbody class="jss1692">
    <tr class="jss1588">
        <td class="jss1598 jss1600 jss1593 jss1597 jss1693 jss1694 jss1696 jss1697 sc-jtRfpW bPusKc" style="left: 0px;">
            <div class="sc-kTUwUJ frEzXN">
                <div title="Draft" class="sc-jKJlTe gSOlpt">
                    <div data-testid="StatusLight" class="sc-eNQAEJ heHvgs"></div>
                </div>
            </div>
        </td>
        <td class="jss1598 jss1600 jss1593 jss1597 jss1696 sc-dqBHgY eAlZOZ">
            <div class="sc-elJkPf epzdvI">Value1</div>
        </td>
        <td class="jss1598 jss1600 jss1593 jss1597 jss1696 sc-dqBHgY eAlZOZ">
            <div class="sc-elJkPf epzdvI">Value2</div>
        </td>
        <td class="jss1598 jss1600 jss1593 jss1597 jss1696 sc-dqBHgY eAlZOZ">
            <div class="sc-elJkPf epzdvI">Value3</div>
        </td>
    </tr>
</tbody>

You need to iterate through the td elements and extract the text content.您需要遍历 td 元素并提取文本内容。 Since I use .find('td') it finds all the td elements, and with .each() you can iterate through them one-by-one, selecting the elements you want (in this case I am skipping the element with index 0 as it's just an empty string):因为我使用.find('td')它会找到所有 td 元素,并且使用.each()你可以一一遍历它们,选择你想要的元素(在这种情况下我跳过带有索引的元素0 因为它只是一个空字符串):

let values = []
cy.visit('stackoverflow/table.html')
cy.get('tbody > tr')
  .find('td')
  .each(($el, $index) => {
     cy.wrap($el)
      .invoke('text')
      .then(text => {
          if($index!==0)
            values.push(text.trim())
          })
       })
      .then(() => expect(values).to.deep.eq(["Value1", "Value2", "Value3"]))

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

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