[Protractor] Use protractor to catch errors in the console

For any reason, there is an error in your code, maybe something like undefined error. Protractor still has all the test cases passing, but our application has some problem.

Or let's say in production code, we want check whether we have cleaned all of our console.log() code in the console. 

 

To do that we can add an afertEach() block:

  afterEach(function () {
    browser.manage().logs().get('browser').then(function (browserLog) {
      expect(browserLog.length).toEqual(0);
      if (browserLog.length) console.error('log: ' + JSON.stringify(browserLog));
    });
  });

 

----------------

 

Code:

var IndexPage = require('./IndexPage');

describe('hello-protractor', function () {

  var page = new IndexPage();

  beforeEach(function() {
      page.get();
  });

  afterEach(function () {
    browser.manage().logs().get('browser').then(function (browserLog) {
      expect(browserLog.length).toEqual(0);
      if (browserLog.length) console.error('log: ' + JSON.stringify(browserLog));
    });
  });

  describe('index', function () {
    it('should display the correct title', function () {
      expect(page.getTitle()).toBe('hello protractor');
    });

    it('should display the message when button clicked', function () {
      page.clickButton();

      expect(page.getMessageText()).toBe('button 1 clicked');
    });
  });
});

 

posted @ 2016-02-20 23:37  Zhentiw  阅读(324)  评论(0编辑  收藏  举报