view console.log output in angular protractor jasmine test

后端 未结 7 1805
忘掉有多难
忘掉有多难 2020-12-24 05:12

How can I view console.log output in an angularjs protractor jasmine test? As of now, the browser closes by itself too quickly.

more info - I am working with the ang

相关标签:
7条回答
  • 2020-12-24 05:46

    A simple option is to use:

    browser.pause(); protractor/api/browser.pause

    for example:

    it('should .. test stuff', function() {
        browser.pause(); //--->the automation will pause here - follow the instructions in your terminal to continue 
        //--->your broken testing magic here...
    });
    

    Place that method call as the first item in the spec body where you need to view the browsers console.

    After the browser pauses you'll have control of the automation. You can then interact with the browser as usual, inspecting elements at different states, checking the browsers console, etc.

    Continue the tests by entering c for continue in your terminal - there will be a prompt with instructions waiting for your entry.

    0 讨论(0)
  • 2020-12-24 05:57

    You may also want to change the logging level to see other types of console outputs.

    See my proposed update to the protractor faq to do this here

    0 讨论(0)
  • 2020-12-24 06:04

    Use browser.manage().logs().get('browser')

    browser.manage().logs().get('browser').then(function(browserLogs) {
       // browserLogs is an array of objects with level and message fields
       browserLogs.forEach(function(log){
          if (log.level.value > 900) { // it's an error log
            console.log('Browser console error!');
            console.log(log.message);
          }
       });
    });
    
    0 讨论(0)
  • 2020-12-24 06:07

    You could always just override console.log in your test :)

    logMessages = [];
    
    console.log = function(message) {
        logMessages.push(message);
    }
    

    You could also use $log instead of console.log and use a solution like this to put some hooks into the log messages: https://gist.github.com/lrvick/6938531

    0 讨论(0)
  • 2020-12-24 06:09

    A general misconception is console.log will log the things in your browser. That is incorrect. When you run your tests, along with the results of tests you should see the console.log() values also in the terminal. Browser console is completely different from this.

    A general example:

    it('get name as John', function(){
            element(by.id('name')).getText().then(function(value){
            console.log(value);
        })
    });
    

    Results in Terminal:

    John
    get name as John - pass
    

    Hope it helps.

    0 讨论(0)
  • 2020-12-24 06:09

    In order to keep the browser's window open you should try running Protractor in debug mode:

    $ <route-to-protractor> debug <route-to-conf-file>
    

    Then in your spec file add this line where you want the execution to stop:

    browser.debugger();
    

    In the Protractor debugger console, you can step over the stops by typing c or cont.

    More info here: https://github.com/angular/protractor/blob/master/docs/debugging.md

    Now, in order to get the console content you can check how to do it in Protractor FAQ: https://github.com/angular/protractor/blob/master/docs/faq.md#how-can-i-get-hold-of-the-browsers-console

    Something like:

    browser.manage().logs().get('browser').then(function(browserLog) {
      console.log('log: ' + require('util').inspect(browserLog));
    })
    
    0 讨论(0)
提交回复
热议问题