控制台视图。角量角器茉莉试验的测井输出

时间:2021-08-02 01:23:29

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

如何查看控制台。对数输出在angularjs量角器茉莉花测试?到目前为止,浏览器本身关闭得太快了。

more info - I am working with the angularjs tutorial, step 8. I am trying to change the e2e test to protractor. The protractor config file I'm using is based on %appdata%\npm\node_modules\protractor\referenceConf.js. In spec js files referenced by the config file, I have instances of console.log. However, during execution of the protractor e2e test, the web site opens in chrome, I see things happen in the browser, then the browser closes before I can examine any console.log output. I think I need to keep chrome open somehow. How?

更多信息——我正在使用angularjs教程,第8步。我想把e2e测试改成量角器。我使用的量角器配置文件是基于%appdata%\npm\node_modules\ pro拖拉机\referenceConf.js。在配置文件引用的spec js文件中,我有console.log的实例。然而,在pro拖拉机e2e测试执行期间,web站点在chrome中打开,我看到浏览器中发生了一些事情,然后浏览器在我检查任何控制台之前关闭。日志输出。我想我需要保持chrome的开放。如何?

7 个解决方案

#1


34  

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

使用browser.manage(). log(). get(浏览器)

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

#2


5  

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.

在量角器调试器控制台,您可以通过输入c或cont来跨站。

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

更多信息: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

现在,为了获得控制台内容,您可以检查如何在pro拖拉机FAQ中执行:https://github.com/angular/protractor/blob/masters/docs/faq.md #how-can-i-get-hold-of- browser- console -console

Something like:

喜欢的东西:

browser.manage().logs().get('browser').then(function(browserLog) {
  console.log('log: ' + require('util').inspect(browserLog));
})

#3


5  

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.

一个普遍的误解是安慰。日志将记录浏览器中的内容。这是不正确的。在运行测试时,您应该在终端中也看到console.log()值。浏览器控制台与此完全不同。

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.

希望它可以帮助。

#4


5  

This can now be achieved without writing any special code and via plugins:

现在不需要编写任何特殊代码,也可以通过插件实现:

#5


2  

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

请参阅我对pro拖拉机常见问题解答的建议更新

#6


2  

A simple option is to use:

一个简单的选择是:

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

browser.pause();量角器/ 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.

将该方法调用作为spec主体中需要查看浏览器控制台的第一项。

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.

继续测试,输入c在你的终端继续-将会有提示和指示等待你的进入。

#7


1  

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

您还可以使用$log而不是控制台。记录并使用这样的解决方案将一些钩子放入日志消息中:https://gist.github.com/lrvick/6938531

#1


34  

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

使用browser.manage(). log(). get(浏览器)

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

#2


5  

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.

在量角器调试器控制台,您可以通过输入c或cont来跨站。

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

更多信息: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

现在,为了获得控制台内容,您可以检查如何在pro拖拉机FAQ中执行:https://github.com/angular/protractor/blob/masters/docs/faq.md #how-can-i-get-hold-of- browser- console -console

Something like:

喜欢的东西:

browser.manage().logs().get('browser').then(function(browserLog) {
  console.log('log: ' + require('util').inspect(browserLog));
})

#3


5  

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.

一个普遍的误解是安慰。日志将记录浏览器中的内容。这是不正确的。在运行测试时,您应该在终端中也看到console.log()值。浏览器控制台与此完全不同。

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.

希望它可以帮助。

#4


5  

This can now be achieved without writing any special code and via plugins:

现在不需要编写任何特殊代码,也可以通过插件实现:

#5


2  

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

请参阅我对pro拖拉机常见问题解答的建议更新

#6


2  

A simple option is to use:

一个简单的选择是:

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

browser.pause();量角器/ 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.

将该方法调用作为spec主体中需要查看浏览器控制台的第一项。

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.

继续测试,输入c在你的终端继续-将会有提示和指示等待你的进入。

#7


1  

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

您还可以使用$log而不是控制台。记录并使用这样的解决方案将一些钩子放入日志消息中:https://gist.github.com/lrvick/6938531