【问题标题】:how to javascript data validation by cypress?如何通过 cypress 进行 javascript 数据验证?
【发布时间】:2021-11-27 13:58:42
【问题描述】:

我有一个列表,其中列出了从 javascript 获取的信息。如何通过 cypress 验证 javascript 数据。我正在尝试自动化一个项目。附上功能截图。 如何通过 cypress 验证 javascript 数据?

【问题讨论】:

    标签: javascript automation automated-tests cypress


    【解决方案1】:

    测试通常应该按照您手动测试页面的方式进行,例如在第一次加载时“北美”是活动页面,因此测试该链接是否处于活动状态。

    然后测试内容是否正确,包括标题和图像,然后转到其他选项卡。

    一个例子,

    cy.visit('https://my-vacation.com/home')
    
    cy.contains('a.js-tab-links', 'North America')
      .should('have.class', 'active')                  // this link is active after loading
    
    // Titles on the page
    cy.contains('Galveston Vacation Rentals')
    cy.contains('Sedona Vacation Rentals')
    //  ... same for remaining titles
    
    // Images on the page
    cy.get('img[src="galveston-island-historic-pleasure-pier.jpg"]')
    // ... same for other images
    
    
    // Other tabs
    cy.contains('a.js-tab-links', 'South America')
      .click()                                      // activate this tab
      .should('have.class', 'active')
    
    // Titles on the page
    cy.contains('Peru Vacation Rentals')
    cy.contains('Argentina Vacation Rentals')
    //  ... same for remaining titles
    
    // Images on the page
    cy.get('img[src="machu-pichu.jpg"]')
    // ... same for other images
    

    您可以将其拆分,以便每个部分都有自己的测试

    before(() => {
      cy.visit('my-vacation.html')  // visit once before all the tests
    })
    
    it('North America', () => {
      cy.contains('a.js-tab-links', 'North America')
        .should('have.class', 'active')                  // this link is active after loading
    
      // Titles on the page
      cy.contains('Galveston Vacation Rentals')
      cy.contains('Sedona Vacation Rentals')
      //  ... same for remaining titles
    
      // Images on the page
      cy.get('img[src="galveston-island-historic-pleasure-pier.jpg"]')
      // ... same for other images
    })
    
    it('South America', () => {
      cy.contains('a.js-tab-links', 'South America')
        .click()                                      // activate this tab
        .should('have.class', 'active')
    
      // Titles on the page
      cy.contains('Peru Vacation Rentals')
      cy.contains('Argentina Vacation Rentals')
      //  ... same for remaining titles
    
      // Images on the page
      cy.get('img[src="machu-pichu.jpg"]')
      // ... same for other images
    })
    
    

    【讨论】:

    • 这个解决方案无效
    【解决方案2】:

    您可以直接使用eq 直接断言任何大陆-

    cy.get('.location-selector ul li a').eq(0).should('have.text', 'North America')
    cy.get('.location-selector ul li a').eq(1).should('have.text', 'South America')
    

    如果你想断言所有的大陆,你还可以使用each 并验证 -

    var continents = [
      'North America',
      'South America',
      'Europe',
      'Asia',
      'Australia/NZ',
      'Middle East/Africa',
    ]
    
    cy.get('.location-selector ul li a').each(($ele, index) => {
      expect($ele.text().trim()).to.equal(continents[index])
    })
    

    【讨论】:

    • 这个解决方案无效
    • 您遇到的错误是什么
    • 您好,我有 7 个标签。在每个选项卡中都有一个列表。我必须验证这个列表。错误截图:prnt.sc/1vtz6ae 功能视频:dropbox.com/s/c8soecl1fqmhmu5/video.wmv?dl=0 谢谢
    • 用新的定位器更新了答案。您现在可以查看。
    • 感谢您的解决方案。它正在工作,但最后一个选项卡执行的错误最小 屏幕截图:prnt.sc/1vu8afj
    猜你喜欢
    • 2014-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-30
    • 1970-01-01
    • 2021-12-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多