cypress.io - 在所有页面上运行测试,遍历寻呼机

时间:2018-04-09 10:45:01

标签: javascript automated-tests mocha cypress

我想在多个页面上运行相同的测试。它是一个分为多个页面的长形式。页数会有所不同。有时它是7,有时是30.看截图: https://drive.google.com/file/d/1O_xhayyIaRJTqABDR27E4XubrmBTR2IW/view 填好所有页面后,网址会更改。

如何使用cypress.io遍历所有页面?在jQuery / javascript中就是这样。

$(document).ready(function($) {
             if(window.location.href.indexOf("xyz") > -1) {
             fillInForm();
             clickOnNext();
}
});

在cypress.io中,我试过但它只运行一次。

cy.location().its('href').then((val) => {
    if (val.indexOf('xyz')  !== -1) {
        // cypress test, fill in form...
        cy.contains('Next').eq(0).click()  
    }
  })

有什么想法吗?谢谢。

1 个答案:

答案 0 :(得分:0)

我正在使用cypress 3.1.1,这是我在应用程序中测试分页的方法,该应用程序提供具有不同页面数的搜索结果。

it('Pages proper', function() {

    // Perform search
    cy.get('[data-testid=search-icon]').click();
    cy.get('[data-testid=explorer-search-input]').type('the');
    cy.get('[data-testid=explorer-search-input]').type('{enter}');
    cy.contains('Displaying 1 - 10 of 21 results');
    cy.contains('Page 1 of 3');

    // Test Next Page
    cy.get('[data-testid=explorer-page-btns-next]').click();
    cy.contains('Displaying 11 - 20 of 21 results');
    cy.contains('Page 2 of 3');
    cy.get('[data-testid=explorer-page-btns-next]').click();
    cy.contains('Displaying 21 - 21 of 21 results');
    cy.contains('Page 3 of 3');

    // Test Previous Page
    cy.get('[data-testid=explorer-page-btns-previous]').click();
    cy.contains('Displaying 11 - 20 of 21 results');
    cy.contains('Page 2 of 3');

    // Preform new search to vary results/pages returned
    cy.get('[data-testid=search-icon]').click();
    cy.get('[data-testid=explorer-search-input]').type('Rabbit Hole');
    cy.get('[data-testid=explorer-search-input]').type('{enter}');        

    ...

})

除了分页功能外,您还可以为结果中的任何已知项目添加cy.contains('xyz');,以验证数据是否显示。