读取 cypress 表中的值并给我最新的值

Ing*_*ito 0 html-table cypress

我想在 Cypress 中检查名称文档列的所有行中的内容是否相同。比方说

名称 文件 签发日期
戈尔 2023年6月21日
戈尔 2023年5月30日

该表没有固定的行数。

在此输入图像描述

附加问题:如何检查最新的(发布日期列)是否位于顶部?

Lol*_*ola 5

第一列的所有值都为“GOL”

要仅提取第 1 列元素,请使用cy.get('tr td:nth-child(1)')

要映射到内部文本,这是一个有用的配方从元素列表中获取文本

const getTexts = (els) => Cypress._.map(els, 'innerText')
cy.get('tr td:nth-child(1)')
  .then(getTexts)
  .should('deep.equal', ['GOL', 'GOL', 'GOL'])
Run Code Online (Sandbox Code Playgroud)

如果行是动态的,请使用.every()

cy.get('tr td:nth-child(1)')
  .then(getTexts)
  .should(vals => {
    const allFirstColHasGol = vals.every(val => val === 'GOL')
    assert(allFirstColHasGol, 'All first column has value "GOL"'))
  })
Run Code Online (Sandbox Code Playgroud)

第一次日期是最晚的日期

我推荐dayjs库来处理日期

const dayjs = require('dayjs')
const customParseFormat = require('dayjs/plugin/customParseFormat')
dayjs.extend(customParseFormat)


const getTexts = (els) => Cypress._.map(els, 'innerText')
const toDate = (els) => Cypress._.map(els, el => dayjs(el, 'MM-DD-YYYY').$d) 

cy.get('tr td:nth-child(4)')
  .then(getTexts)
  .then(toDate)
  .should(dates => {
    const firstDateIsLateset = dates.slice(1).every(date => dates[0] > date)
    assert(firstDateIsLateset, 'First date is the largest'))
  })
Run Code Online (Sandbox Code Playgroud)

TDD具有以下内容,并更改列值以使其通过/失败

<table>
  <tbody>
    <tr>
      <td><a>GOL</a></td>
      <td><span>06-02-2023</span></td>
    </tr>
    <tr>
      <td><a>GOL</a></td>
      <td><span>06-01-2023</span></td>
    </tr>
  </tbody>
</table>
Run Code Online (Sandbox Code Playgroud)