首页 > 解决方案 > 为什么在使用 Cypress 时需要明确清除 sessionStorage?

问题描述

我正在使用赛普拉斯测试一个通过医生列表分页的 Angular 应用程序。控制器将当前页面存储在 $sessionStorage.pagination 中。如果我没有明确清除测试之间的会话存储,那么我的“应该转到下一页”测试之后的测试就会失败。如果我明确清除测试之间的会话存储,一切都会按预期工作。这与我在此处阅读的内容(https://github.com/cypress-io/cypress/issues/686)相矛盾,这似乎是说会话存储在测试之间会自动清除。

这是赛普拉斯的错误吗?或者,我是否误解了赛普拉斯开发人员在测试之间清除会话存储的意思?

describe('Simple Search Results Page Tests', function () {

    beforeEach(function(){
      cy.server();

    // routes omitted for brevity 

    // Changing pages breaks tests if I visit page under test like this
    cy.visit('http://localhost:9001/pageUnderTest'); 

    // tests work as expected if I visit page under test like this
    /*
    cy.visit('http://localhost:9001/pageUnderTest', 
              {onBeforeLoad: (win) => { win.sessionStorage.clear()}
    });
    /*
  }); 

  it('should search Best Match', function(){
    cy.get('[data-cy=first-name]').first().should('have.text', 'Michael')
      .get('[data-cy=last-name]').first().should('have.text', 'Emiley')
    });

  it('should go to the next page', function(){
    cy.get('[data-cy="page-number"]').click('right')
  }); 

  // this test is broken if session storage not explicitly re-set.
  it('should search Best Match (again)', function(){
    cy.get('[data-cy=first-name]').first().should('have.text', 'Michael')
      .get('[data-cy=last-name]').first().should('have.text', 'Emiley')
    });
});

标签: angularsession-storagecypress

解决方案


这个开放的 gitlab 问题似乎与您的问题相匹配,并确认它是一个请求的功能。

不过,通读一遍,提到了这部史诗,它表明赛普拉斯确实很清楚sessionStorage。所以我也有点失落:)


推荐阅读