量角器的日志

时间:2016-08-05 08:46:46

标签: selenium protractor automated-tests nodes

我是量角器的新手,想为我的测试用例创建日志。我用if和else写日志。我想知道是否有更好的方法为量角器测试用例编写日志?

我的代码:

var colors = require('colors/safe');

var mapFeedBackpage=require('./mapFeedBack-page.js')
describe("Map feedback Automation",function()
{

var mapFeedBack= new mapFeedBackpage();

    it("Check if the Url works ",function() //spec1
    {
        console.log("Checking the url :"+browser.params.url+'\n')
        browser.get(browser.params.url);
    browser.getCurrentUrl().then(function(value){
  if(/report/.test(value) === false) {
    fail("Result: URL doesnt works-FAIL \n");
  }
  else
  {
      console.log(colors.green("PASS :")+browser.params.url+ "is reachable \n");
  }

});

    }); 


    it("test browser should reach report road option",function() //spec2s
    {
        console.log("Checking if road report option is available \n");
        mapFeedBack.REPORT_ROAD.click();
        expect(browser.getCurrentUrl()).toContain("report_road");
        browser.getCurrentUrl().then(function(value){

        if(/report_road/.test(value) === false) {
        fail("Result: URL doesnt works-FAIL");
  }
  else
  {
      console.log(colors.green("PASS")+" Road report option is available");
  }

});

    });

1 个答案:

答案 0 :(得分:2)

是的,您可以使用https://www.npmjs.com/package/log4js这基本上是nodejs应用程序的log4j模块。由于量角器是nodejs程序,它肯定会支持这一点。实现这个很容易 -

 var log4js = require('log4js');
 var logger = log4js.getLogger();
 logger.debug("Some debug messages");

或者你可以写一个自定义记录器:

 var logger = exports;
logger.debugLevel = 'warn';
logger.log = function(level, message) {
var levels = ['error', 'warn', 'info'];
if (levels.indexOf(level) >= levels.indexOf(logger.debugLevel) ) {
  if (typeof message !== 'string') {
    message = JSON.stringify(message);
  };
  console.log(level+': '+message);
}
 }

然后在脚本中使用它:

var logger = require('./logger');
logger.debugLevel = 'warn';
logger.log('info', 'Everything started properly.');
logger.log('warn', 'Running out of memory...');
logger.log('error', { error: 'flagrant'});