【问题标题】:How can I track DOB that is given in text format in a Wikipedia page and assert that one DOB is greater than other one using cypress? learning cypress如何跟踪 Wikipedia 页面中以文本格式给出的 DOB 并断言一个 DOB 大于另一个使用 cypress 的 DOB?学习柏树
【发布时间】:2025-12-02 10:10:01
【问题描述】:

这就是我正在做的事情:

  • 访问此页面:https://en.wikipedia.org/wiki/The_Beatles
  • 点击 Paul McCartney *页面的第一个链接
  • 跟踪 Paul McCartney 的出生日期
  • 返回甲壳虫乐队页面
  • 点击约翰列侬*页面的第一个链接
  • 跟踪约翰列侬的出生日期
  • 断言约翰列侬在保罗麦卡特尼之前出生
describe('iCONNECT Test Suite', function () {
  it('Test Case 1', function () {
    cy.visit('https://en.wikipedia.org/wiki/The_Beatles')
    cy.get(':nth-child(8) > [href="/wiki/Paul_McCartney"]').click()
    cy.get('.mw-parser-output > :nth-child(6)').contains('18 June 1942')
    cy.go(-1)
    cy.wait(7000)
    cy.get(':nth-child(8) > [href="/wiki/John_Lennon"]').click()
    cy.get('.mw-parser-output > :nth-child(6)').contains('9 October 1940')
    expect(1940).to.be.lessThan(1942)
  })
})

【问题讨论】:

    标签: javascript automation cypress


    【解决方案1】:

    你可以这样做:

    describe('iCONNECT Test Suite', function () {
      it('Test Case 1', function () {
        cy.visit('https://en.wikipedia.org/wiki/The_Beatles')
        cy.get('.infobox-data')
          .find('li > [href="/wiki/Paul_McCartney"]', {timeout: 7000})
          .click()
        cy.get('.infobox-data', {timeout: 6000})
          .eq(0)
          .should('be.visible')
          .invoke('text')
          .should('include', '18 June 1942') //Assert the birth day for Paul McCartney
        cy.get('.infobox-data')
          .eq(0)
          .find('span')
          .invoke('text')
          .then((text) => text.slice(2, 6))
          .as('paulBirthYear') //Get Birth year for Paul McCartney
        cy.go(-1)
        cy.get('.infobox-data')
          .find('li > [href="/wiki/John_Lennon"]', {timeout: 7000})
          .click()
        cy.get('.infobox-data', {timeout: 6000})
          .eq(0)
          .should('be.visible')
          .invoke('text')
          .should('include', '9 October 1940') //Assert the birth day for John Lennon
        cy.get('.infobox-data')
          .eq(0)
          .find('span')
          .invoke('text')
          .then((text) => text.slice(1, 5))
          .as('johnBirthYear') //Get Birth year for John Lennon
        cy.get('@paulBirthYear').then((paulBirthYear) => {
          cy.get('@johnBirthYear').then((johnBirthYear) => {
            expect(+johnBirthYear).to.be.lessThan(+paulBirthYear) //Assert John Lennon is older than Paul McCartney
          })
        })
      })
    })
    

    测试运行器执行:

    【讨论】:

    • 你能帮我理解你调用了哪些文本并使用 .slice 方法来获取 john 的第 2 到第 4 个元素和 paul 的第 3 到第 5 个元素吗?我的问题可能看起来很幼稚。我只是想学习:)
    • 对于 Paul McCartney,cy.get('.infobox-data').eq(0).find('span').invoke('text') 给出 (1942-06-18) 1942-06-18 (age 79),切片后给出 1942。同样,对于约翰列侬,我们得到(1940-10-09)1940-10-09,切片之后,它给你1940。然后我们通过在+johnBirthYear+paulBirthYear 等字符串前面添加+ 将这些字符串转换为数字,最后使用expect 进行断言。
    • 谢谢,阿拉潘。一个简单的基本问题:cy.get('@paulBirthYear').then((paulBirthYear) => { cy.get('@johnBirthYear').then((johnBirthYear) => { expect(+johnBirthYear).to.be .lessThan(+paulBirthYear) //断言约翰列侬比保罗麦卡特尼年长 }) }) .为什么在这里使用嵌套函数?
    • 为了访问和比较两个出生年份的值。
    • 嗨,我正在尝试这样做:cy.get('.infobox-data') .eq(0) .find('span') .invoke('text') .then(function(text){ text.slice(2, 6) }).as('paulBirthYear').then((paulBirthYear)=> cy.log(paulBirthYear)) 但它正在抓取整个文本:(1942-06-18)1942-06-18(79 岁)。这与:.then((text) => text.slice(2, 6)) 相同,对吗?我的意思是然后按照我写的方式运行?