[Protractor] Use protractor to catch errors in the console

本文将介绍如何通过Protractor测试框架实现自动化检查,确保生产代码中已清除所有console.log调用,避免应用运行时出现不必要的控制台输出。

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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值