在规范中进行多次描述会导致行为异常

时间:2019-05-14 23:27:17

标签: mocha cypress

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

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

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

  });

});

2 个答案:

答案 0 :(得分:0)

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

我只需将您的describe更改为context,然后将两个context包裹在一个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)
        })

    });

  });

})

答案 1 :(得分:0)

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

<块引用>

测试接口,借用Mocha,提供describe(), context()、it() 和specify()。

context() 与describe() 相同,而specify() 与 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  
      });
    });
});