protractor debug mode gives "FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - process out of memory"

protractor debug mode gives "FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - process out of memory"

我正在 运行使用 NodeJS 0.12.4、Protractor 2.1.0 使用脚本自动登录网页,我的系统是 Win 8.1、i7 2.5 GHz、16 GB RAM ,所以我认为我 运行 不太可能内存不足!。 仅供参考,当我注释代码中的所有语句但 browser.get(),并且我在开始时使用 browser.pause() 在交互模式下一个一个地执行它们时,代码有效。 这是 spec.js 文件的代码:

 describe('Web Page Login', function() {
  it('should login', function() {
    browser.get('http://URL_HERE'); // opens page
      
    //browser.pause();
      
    element(by.css('[ng-click="showLogin(true);"]')).click();// clicks the login link to open the login dialog
      
      
    var user = element(by.model('user.login.username'));// input user name
    user.sendKeys('user');
                       
    var pass = element(by.model('user.login.password')); // input password                
    pass.sendKeys('pass');

    element(by.css('[ng-click="login();"]')).click();// clicks the login button
      
    var userName = element(by.className('ng-binding'));// locates the logged in user from the player details element and store it in "userName"
    userName.getText();  //extracts the text that contains the user name
   
    expect(userName).toBe("user"); //compare the string obtained above with the one expected 

  });
});

这是 conf.js 文件的代码:

var HtmlReporter = require('protractor-html-screenshot-reporter');

var reporter=new HtmlReporter({
    baseDirectory: './results', // the location to store the screen shots and results.
    docTitle: 'Login test result',
    docName:  'login-tests-report.html'
});


exports.config = {
  seleniumAddress: 'http://localhost:4444/wd/hub',
  specs: ['login-spec.js'],
  onPrepare: function() {
        jasmine.getEnv().addReporter(reporter);
 }
  };
不幸的是我不能 post 我们网站的 URL 因为它是内部的,如果需要 HTML 我可以 post 一些片段。 所以如果我尝试 运行 它输入

protractor conf.js

没有注释所有行我得到这个错误:

FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - process out of memory

但是,如果我注释 spec.js 中除 browser.get() 之外的所有行,并且我使用 repl 进入调试交互模式,然后逐行键入代码中的每一行我在上面说过,我可以毫无错误地执行所有代码到最后。 我在 2 周前开始使用量角器和 JavaScript,所以我可能会错过一些东西。 感谢您的帮助。

在与我的一位开发人员讨论后,我有点想出了如何使代码工作,而不是触发该错误的原因,我怀疑它可能是来自 Node 库的东西。 改变我执行断言的方式消除了错误。 这是有效的代码:

describe('Web Page Login', function() {
  it('should login', function() {
    browser.get('http://URL_HERE');//opens Web page
      
    element(by.css('[ng-click="showLogin(true);"]')).click();//clicks the login link to open the login dialog     
 
    element(by.model('login.user.login.username')).sendKeys('username');//input user name          
    element(by.model('login.user.login.password')).sendKeys('password');//input password 
    element(by.css('[ng-click="login.login(false, loginForm.$valid);"]')).click(); //clicks the login button
    
    var userName = element(by.className('ng-binding'));// locates the logged in user from the player details element and store it in "userName"
    expect(userName.getText()).toContain('username');//compares the string found with the one expected and asserts if it's true or false
  });
});
如果我能找到根本原因,我会post在这里。

希望这些事实会有所帮助。

  1. 始终保持代码清洁和优化。最好不要声明不必要的变量。

  2. getText return是一个承诺,因此您需要解决它。

      element(by.className('ng-binding')).getText().then(function (text) {
       expect(text).toContain('username');
    });  

是最好的处理方式。

  1. 更好地了解何时使用 getText

getText 将 return 元素的开始和结束标签之间的文本内容。 由于userName是文本输入,是自闭标签,getText将无法抓取内容

所以对于自闭标签总是使用

  getAttribute('value')  

而不是 getText。

  1. 'Process out of memory' 如果为执行分配的内存小于所需内存,通常会发生错误。此答案将指导您修复它。

--max-old-space-size=1024 将内存增加到 1GB --max-old-space-size=2048 将内存增加到 2GB --max-old-space-size=3072 将内存增加到 3GB --max-old-space-size=4096 将内存增加到 4GB --max-old-space-size=5120 将内存增加到 5GB --max-old-space-size=6144 将内存增加到 6GB --max-old-space-size=7168 将内存增加到 7GB --max-old-space-size=8192 将内存增加到 8GB

此外,最好不要使用toContain来检查userName,因为它是一个关键字段。您应该检查它是否包含确切的用户名。所以 toEqual 比 toBe 更合适,因为 toEqual 会检查深度相等性。