简体   繁体   English

使用赛普拉斯时,为什么需要显式清除sessionStorage?

[英]Why do I need to explicitly clear sessionStorage when using Cypress?

I am using Cypress to test an Angular application that pages through a list of doctors. 我正在使用赛普拉斯(Cypress)测试可在医生列表中进行分页的Angular应用程序。 The controller stores the current page in $sessionStorage.pagination. 控制器将当前页面存储在$ sessionStorage.pagination中。 If I don't explicitly clear the session storage between tests, tests that follow my "should go to next page" test fail. 如果我没有明确清除测试之间的会话存储,则遵循“应转到下一页”测试的测试将失败。 If I do explicitly clear the session storage between tests, everything works as expected. 如果我确实明确清除测试之间的会话存储,那么一切都会按预期进行。 This contradicts what I read here ( https://github.com/cypress-io/cypress/issues/686 ) which seems to say that the session storage is automatically cleared between tests. 这与我在这里阅读的内容( https://github.com/cypress-io/cypress/issues/686 )矛盾,这似乎表明会话存储是在测试之间自动清除的。

Is this a bug in Cypress? 这是赛普拉斯中的错误吗? Or, am I misunderstanding what the Cypress developers mean by clearing session storage between tests? 还是我误解了赛普拉斯开发人员在测试之间清除会话存储的含义?

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')
    });
});

This open gitlab issue seems to match your issue and confirms it's a requested feature. 这个公开的gitlab问题似乎与您的问题相符,并确认它是必需的功能。

Reading through though, this epic is mentioned , which states that Cypress does clear sessionStorage . 尽管sessionStorage 此史诗 ,但它指出赛普拉斯确实清除了sessionStorage So I'm a bit lost as well :) 所以我也有点迷路:)

Have you tried with context? 您是否尝试过上下文?

context('test 1', function () { cy.setCookie('test') })
context('test 2', function () { cy.getCookie('test').should('not.exist' })

does it exist? 是否存在?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用赛普拉斯时如何在Angular中模拟或存根函数? - how do I mock or stub a function in Angular when using Cypress? 当我想明确清除它时,无法读取未定义的属性“viewContainerRef” - Cannot read property 'viewContainerRef' of undefined when I want to clear it explicitly Angular2:在进行双向绑定时,是否需要显式使用EventEmitter? - Angular2: When doing two-way binding, do I need to explicitly use an EventEmitter? 在将Bootstrap与Webpack结合使用时,为什么需要jQuery的特殊“ hack” - Why do I need a special “hack” for jQuery when using Bootstrap with Webpack 为什么我已经有了 expectOne() 还需要 verify()? - Why do I need verify() when I already have expectOne()? 为什么这个 function 每次都明确设置为更新 textarea 值一次? - Why does this function updates textarea value only once when it is explicitly set to do this each time? 离开页面时使用 sessionStorage 保留 select 值中的值 Angular - Using sessionStorage to keep values in select values when leaving the page Angular 为什么我需要标记检查? angular - Why do I need mark for check ? angular 为什么我需要订阅我的 Observable? - Why do I need to subscribe to my Observable? 如何在其他组件中使用 sessionStorage.clear()? - how to use sessionStorage.clear() in other component?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM