【问题标题】:having multiple describe in spec leads to weird behaviour在规范中有多个描述会导致奇怪的行为
【发布时间】:2019-05-14 23:27:17
【问题描述】:

我在一个规范文件中有 2 个描述块。 首先,describe 访问 xyz.com,其次,describe 访问 abc.com

我只需要在一个规范中描述这 2 个。我看到的有线行为是它顺利运行测试,但是在从 2nd describe 访问 abc.com 后,它又开始运行 1st describe。无限循环的测试

var signedOutArtifactID = null;

describe('WEB APP E2E tests', function() {
  var token = null;

  before(function() {
    cy.visit('/');

    // Login
    cy.get('#username')
      .type(auth.geneticist.username);

    cy.get('#password')
      .type(auth.geneticist.password);

    cy.get('button')
      .contains('Login')
      .click()
      .should(function() {
        token = localStorage.getItem('token');
        expect(token).not.to.be.null;
      });
  });

  beforeEach(function() {
    localStorage.setItem('token', token);

    cy.contains('Logout')
      .should('exist');

    expect(localStorage.getItem('token'));
  });

  it('should land on home page', function() {
    cy.url()
      .should('include', '/home');
  });


  it('should save and generate and end up on signout page', function() {
    cy.contains('Save and Generate Report')
      .click();

    cy.url()
      .should('include', '/sign-out');
  });

  it('should signout and send successfully', function() {
    cy.url()
      .should(function(currentURL) {
        signedOutArtifactID = currentURL.match(/2-([0-9]+)/)[0];
        expect(signedOutArtifactID).not.to.be.null;
    });

    // Make sure interpretation was updated
    cy.get('.card-body pre')
      .should('contain', 'test interpretation added by cypress');

    cy.contains('Sign Out and Send')
      .click();

    cy.contains('Yes, sign out and send')
      .click();

  });


});


describe('2nd WEB APP E2E tests', function() {

  before(function () {
    cy.visit({
      url:`https://webappurl.com/search?scope=All&query=${signedOutArtifactID}`,
      failOnStatusCode: false
    })
  })  

  it('Review Completed step in clarity', async () => {

    cy.get('#username').type(auth.clarity_creds.username)
    cy.get('#password').type(auth.clarity_creds.password)
    cy.get('#sign-in').click()

    cy.get('.result-name').click()
    cy.get('.view-work-link').contains('QWERTYU-IDS').click()

    cy.get('.download-file-link ')
      .should(($downloads) => {
        expect($downloads).to.have.length(2)
      })

  });

});

【问题讨论】:

  • 见:How To Create MCVE。没有代码很难帮助你。
  • @alfasin 不确定代码 sn-p 是否有帮助,但我已经更新了问题
  • 为什么同一个文件的根目录有两个不同的描述?如果它们不相关,请将它们放在单独的文件中。如果它们相关的,则将它们都嵌套在一个附加的描述中,并添加一个描述您将在此文件中测试的内容的字符串。 WDYT?
  • @alfasin 我尝试将它们放在单独的文件中,也尝试在一个文件中包含 2 个描述,但它不起作用(面临同样的问题)。我需要在should signout and send successfully 测试中生成的signedOutArtifactID。两种描述都针对不同的网络应用程序
  • 如果你把它们放在不同的文件中 - 为什么它们都被调用了?

标签: mocha.js cypress


【解决方案1】:

describe 定义了一个测试套件。每个文件只能有一个顶级测试套件,每个测试只能有一个域。

我只需将您的 describes 更改为 contexts 并将两个 contexts 包装在一个 describe 中,如下所示:

var signedOutArtifactID = null;

describe('e2e tests', function() {

  context('WEB APP E2E tests', function() {
    var token = null;

    before(function() {
      cy.visit('/');

      // Login
      cy.get('#username')
        .type(auth.geneticist.username);

      cy.get('#password')
        .type(auth.geneticist.password);

      cy.get('button')
        .contains('Login')
        .click()
        .should(function() {
          token = localStorage.getItem('token');
          expect(token).not.to.be.null;
        });
    });

    beforeEach(function() {
      localStorage.setItem('token', token);

      cy.contains('Logout')
        .should('exist');

      expect(localStorage.getItem('token'));
    });

    it('should land on home page', function() {
      cy.url()
        .should('include', '/home');
    });


    it('should save and generate and end up on signout page', function() {
      cy.contains('Save and Generate Report')
        .click();

      cy.url()
        .should('include', '/sign-out');
    });

    it('should signout and send successfully', function() {
      cy.url()
        .should(function(currentURL) {
          signedOutArtifactID = currentURL.match(/2-([0-9]+)/)[0];
          expect(signedOutArtifactID).not.to.be.null;
      });

      // Make sure interpretation was updated
      cy.get('.card-body pre')
        .should('contain', 'test interpretation added by cypress');

      cy.contains('Sign Out and Send')
        .click();

      cy.contains('Yes, sign out and send')
        .click();

    });


  });


  context('2nd WEB APP E2E tests', function() {

    before(function () {
      cy.visit({
        url:`https://webappurl.com/search?scope=All&query=${signedOutArtifactID}`,
        failOnStatusCode: false
      })
    })  

    it('Review Completed step in clarity', async () => {

      cy.get('#username').type(auth.clarity_creds.username)
      cy.get('#password').type(auth.clarity_creds.password)
      cy.get('#sign-in').click()

      cy.get('.result-name').click()
      cy.get('.view-work-link').contains('QWERTYU-IDS').click()

      cy.get('.download-file-link ')
        .should(($downloads) => {
          expect($downloads).to.have.length(2)
        })

    });

  });

})

【讨论】:

  • context 只是describe 的同义词或别名,在最近的 Jasmine 版本中已被删除。
【解决方案2】:

每个suite(规范文件)应该有一个describe 块。因此,当我需要在一个规范文件中包装多个相关测试时,我使用context。此外,以下是 cypress 文档所说的:

测试接口,借用Mocha,提供describe(), 上下文(),它()和指定()。

context() 与 describe() 相同,并且指定() 与 it(),所以选择最适合你的术语

但是,我认为测试结构和describecontextit 层次结构有点偏离轨道。所以,这就是我编写测试的方式:

describe('User Authentication Using Custom Auth Token', () => {
    beforeEach(() => {
        cy.on('uncaught:exception', err => {
          console.log('cypress has detected uncaught exception', err);
          return false;
        });
      });
    context('when not authenticated', () => {
        it('Redirects to /login and Stays on /Login', () => {
        cy.visit('/');
        cy.location('pathname').should('equal', '/login');
        // more on your logic  
        });
    context('when authenticated', () => {
      it('Successful login using Custom Auth Token', ()=>{
        cy.visit('/')
        cy.login();
        // more on your logic  
      });
    });
});

【讨论】:

  • 每个文件可以有多个描述。 describecontext 是同义词,可以互换使用。
猜你喜欢
  • 1970-01-01
  • 2017-07-21
  • 1970-01-01
  • 2018-05-29
  • 2018-09-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多