Javascript 量角器异步/等待错误:未处理的承诺拒绝

Javascript 量角器异步/等待错误:未处理的承诺拒绝,javascript,angularjs,async-await,jasmine,protractor,Javascript,Angularjs,Async Await,Jasmine,Protractor,我正在使用量角器Async/Await重构我的框架,以避免在整个代码库中出现草率的browser.sleep() 以下是测试步骤,以代码为例: Opens ChromeBrowser Logins with the credentials Selects a customer Clicks on "Manage Customer" button. 您能帮我解决以下错误吗: Report destination: target\e2e\screenshots\my-report.html [

我正在使用量角器Async/Await重构我的框架,以避免在整个代码库中出现草率的browser.sleep()

以下是测试步骤,以代码为例:

Opens ChromeBrowser
Logins with the credentials
Selects a customer
Clicks on "Manage Customer" button.
您能帮我解决以下错误吗:

Report destination:   target\e2e\screenshots\my-report.html
[12:42:21] I/launcher - Running 1 instances of WebDriver
[12:42:21] I/hosted - Using the selenium server at http://127.0.0.1:4444/wd/hub

(node:18208) UnhandledPromiseRejectionWarning: Error: Error while waiting for Protractor to sync with the page: "both angularJS testability and angular testability are undefined.  This could be either because this is a non-angular page or because you
r test involves client-side navigation, which can interfere with Protractor's bootstrapping.  See http://git.io/v4gXM for details"
    at runWaitForAngularScript.then (C:\Users\Ashish\AppData\Roaming\npm\node_modules\protractor\built\browser.js:463:23)
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:188:7)Error
    at ElementArrayFinder.applyAction_ (C:\Users\Ashish\AppData\Roaming\npm\node_modules\protractor\built\element.js:459:27)
    at ElementArrayFinder.(anonymous function).args [as sendKeys] (C:\Users\Ashish\AppData\Roaming\npm\node_modules\protractor\built\element.js:91:29)
    at ElementFinder.(anonymous function).args [as sendKeys] (C:\Users\Ashish\AppData\Roaming\npm\node_modules\protractor\built\element.js:831:22)
    at customer.cusSelection (C:\Users\Ashish\Documents\Protractor\scripts\CustomerSelection.js:7:50)
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:188:7)
(node:18208) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:18208) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
[12:42:25] I/launcher - 0 instance(s) of WebDriver still running
[12:42:25] I/launcher - chrome #01 passed
var HtmlScreenshotReporter = require('protractor-jasmine2-screenshot-reporter');
var reporter = new HtmlScreenshotReporter({
  dest: 'target/e2e/screenshots',
  filename: 'my-report.html',
  ignoreSkippedSpecs: true,
  reportOnlyFailedSpecs: false,
  captureOnlyFailedSpecs: true  ,
  showSummary: true,
  showQuickLinks: true,
  showConfiguration: true,
  reportTitle: "Protractor Automation Report",
  reportFailedUrl: true,
  inlineImages: true,
});

exports.config = {
seleniumAddress: 'http://127.0.0.1:4444/wd/hub',

suites: {
    Scenario1: './Scenario_01.js', 
},

SELENIUM_PROMISE_MANAGER: false,

capabilities: {
    'shardTestFiles': false,
    'maxInstances': 1,
    'browserName': 'chrome',
    'chromeOptions': {
     'args': ['disable-extensions', 'start-maximized']  //'--headless',
    }
},
allScriptsTimeout: 11000,
getPageTimout: 10000,
restartBrowserBetweenTests: false,
framework: 'jasmine2',
jasmineNodeOpts: {
    onComplete: null,
    isVerbose: true,
    showColors: true, // is True, prints colors to terminal
    includeStackTrace: true,
    defaultTimeoutInterval: 30000, 
    print: function () {}
},
beforeLaunch: function() {
    return new Promise(function(resolve){
    reporter.beforeLaunch(resolve);
    });
},

onPrepare: function () {

    jasmine.getEnv().addReporter(reporter);
    browser.manage().window().maximize();
    browser.manage().timeouts().implicitlyWait(5000);
    jasmine.getEnv().addReporter( new Jasmine2HtmlReporter({savePath: 'target/screenshots'}));
}
};
describe('Scenario_01', function() {

 var common = require('./scripts/CloseBrowsers.js');    
 var Login = require('./scripts/Login.js'); 
 var customer = require('./scripts/CustomerSelection.js');

 it('Login', function() {
         browser.waitForAngularEnabled(false); 
         Login.login('admin','Adminpwd');
     });

     it('CustomerSelection', function() {
         browser.waitForAngularEnabled(true);
         customer.cusSelection();
     });

afterAll(function(){
    common.closeBrowsers();
});
});
var Login = function() {

     this.login = async function(username, passwordKey){
     await browser.get('http://testwebsite.com/showCust');
     await element(by.name('USER')).sendKeys(username);
     await element(by.name('PASSWORD')).sendKeys(passwordKey);
     await element(by.xpath('/html/body/table/tbody/tr[2]/td/table/tbody/tr/td/table/tbody/tr[3]/td/form/input[9]')).click();
     await element(by.name('YES')).click();
     //browser.sleep(10000);

};
}
module.exports = new Login();
var customer = function(){

    this.cusSelection = async function(){

     await element(by.css(['ng-model="selectedChannel.selected"']));
     await element(by.id('customer-auto-complete')).sendKeys('TestCustomer');
     //browser.sleep(500);

     await element.all(by.css('ul[class^="ui-autocomplete"]')).first().click();
    //browser.sleep(1500);
     await element(by.partialButtonText('Manage Customer')).click();
     browser.sleep(10000);      
    };
}
module.exports = new customer();    
场景\u 01.js:

Report destination:   target\e2e\screenshots\my-report.html
[12:42:21] I/launcher - Running 1 instances of WebDriver
[12:42:21] I/hosted - Using the selenium server at http://127.0.0.1:4444/wd/hub

(node:18208) UnhandledPromiseRejectionWarning: Error: Error while waiting for Protractor to sync with the page: "both angularJS testability and angular testability are undefined.  This could be either because this is a non-angular page or because you
r test involves client-side navigation, which can interfere with Protractor's bootstrapping.  See http://git.io/v4gXM for details"
    at runWaitForAngularScript.then (C:\Users\Ashish\AppData\Roaming\npm\node_modules\protractor\built\browser.js:463:23)
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:188:7)Error
    at ElementArrayFinder.applyAction_ (C:\Users\Ashish\AppData\Roaming\npm\node_modules\protractor\built\element.js:459:27)
    at ElementArrayFinder.(anonymous function).args [as sendKeys] (C:\Users\Ashish\AppData\Roaming\npm\node_modules\protractor\built\element.js:91:29)
    at ElementFinder.(anonymous function).args [as sendKeys] (C:\Users\Ashish\AppData\Roaming\npm\node_modules\protractor\built\element.js:831:22)
    at customer.cusSelection (C:\Users\Ashish\Documents\Protractor\scripts\CustomerSelection.js:7:50)
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:188:7)
(node:18208) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:18208) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
[12:42:25] I/launcher - 0 instance(s) of WebDriver still running
[12:42:25] I/launcher - chrome #01 passed
var HtmlScreenshotReporter = require('protractor-jasmine2-screenshot-reporter');
var reporter = new HtmlScreenshotReporter({
  dest: 'target/e2e/screenshots',
  filename: 'my-report.html',
  ignoreSkippedSpecs: true,
  reportOnlyFailedSpecs: false,
  captureOnlyFailedSpecs: true  ,
  showSummary: true,
  showQuickLinks: true,
  showConfiguration: true,
  reportTitle: "Protractor Automation Report",
  reportFailedUrl: true,
  inlineImages: true,
});

exports.config = {
seleniumAddress: 'http://127.0.0.1:4444/wd/hub',

suites: {
    Scenario1: './Scenario_01.js', 
},

SELENIUM_PROMISE_MANAGER: false,

capabilities: {
    'shardTestFiles': false,
    'maxInstances': 1,
    'browserName': 'chrome',
    'chromeOptions': {
     'args': ['disable-extensions', 'start-maximized']  //'--headless',
    }
},
allScriptsTimeout: 11000,
getPageTimout: 10000,
restartBrowserBetweenTests: false,
framework: 'jasmine2',
jasmineNodeOpts: {
    onComplete: null,
    isVerbose: true,
    showColors: true, // is True, prints colors to terminal
    includeStackTrace: true,
    defaultTimeoutInterval: 30000, 
    print: function () {}
},
beforeLaunch: function() {
    return new Promise(function(resolve){
    reporter.beforeLaunch(resolve);
    });
},

onPrepare: function () {

    jasmine.getEnv().addReporter(reporter);
    browser.manage().window().maximize();
    browser.manage().timeouts().implicitlyWait(5000);
    jasmine.getEnv().addReporter( new Jasmine2HtmlReporter({savePath: 'target/screenshots'}));
}
};
describe('Scenario_01', function() {

 var common = require('./scripts/CloseBrowsers.js');    
 var Login = require('./scripts/Login.js'); 
 var customer = require('./scripts/CustomerSelection.js');

 it('Login', function() {
         browser.waitForAngularEnabled(false); 
         Login.login('admin','Adminpwd');
     });

     it('CustomerSelection', function() {
         browser.waitForAngularEnabled(true);
         customer.cusSelection();
     });

afterAll(function(){
    common.closeBrowsers();
});
});
var Login = function() {

     this.login = async function(username, passwordKey){
     await browser.get('http://testwebsite.com/showCust');
     await element(by.name('USER')).sendKeys(username);
     await element(by.name('PASSWORD')).sendKeys(passwordKey);
     await element(by.xpath('/html/body/table/tbody/tr[2]/td/table/tbody/tr/td/table/tbody/tr[3]/td/form/input[9]')).click();
     await element(by.name('YES')).click();
     //browser.sleep(10000);

};
}
module.exports = new Login();
var customer = function(){

    this.cusSelection = async function(){

     await element(by.css(['ng-model="selectedChannel.selected"']));
     await element(by.id('customer-auto-complete')).sendKeys('TestCustomer');
     //browser.sleep(500);

     await element.all(by.css('ul[class^="ui-autocomplete"]')).first().click();
    //browser.sleep(1500);
     await element(by.partialButtonText('Manage Customer')).click();
     browser.sleep(10000);      
    };
}
module.exports = new customer();    
Login.js:

Report destination:   target\e2e\screenshots\my-report.html
[12:42:21] I/launcher - Running 1 instances of WebDriver
[12:42:21] I/hosted - Using the selenium server at http://127.0.0.1:4444/wd/hub

(node:18208) UnhandledPromiseRejectionWarning: Error: Error while waiting for Protractor to sync with the page: "both angularJS testability and angular testability are undefined.  This could be either because this is a non-angular page or because you
r test involves client-side navigation, which can interfere with Protractor's bootstrapping.  See http://git.io/v4gXM for details"
    at runWaitForAngularScript.then (C:\Users\Ashish\AppData\Roaming\npm\node_modules\protractor\built\browser.js:463:23)
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:188:7)Error
    at ElementArrayFinder.applyAction_ (C:\Users\Ashish\AppData\Roaming\npm\node_modules\protractor\built\element.js:459:27)
    at ElementArrayFinder.(anonymous function).args [as sendKeys] (C:\Users\Ashish\AppData\Roaming\npm\node_modules\protractor\built\element.js:91:29)
    at ElementFinder.(anonymous function).args [as sendKeys] (C:\Users\Ashish\AppData\Roaming\npm\node_modules\protractor\built\element.js:831:22)
    at customer.cusSelection (C:\Users\Ashish\Documents\Protractor\scripts\CustomerSelection.js:7:50)
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:188:7)
(node:18208) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:18208) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
[12:42:25] I/launcher - 0 instance(s) of WebDriver still running
[12:42:25] I/launcher - chrome #01 passed
var HtmlScreenshotReporter = require('protractor-jasmine2-screenshot-reporter');
var reporter = new HtmlScreenshotReporter({
  dest: 'target/e2e/screenshots',
  filename: 'my-report.html',
  ignoreSkippedSpecs: true,
  reportOnlyFailedSpecs: false,
  captureOnlyFailedSpecs: true  ,
  showSummary: true,
  showQuickLinks: true,
  showConfiguration: true,
  reportTitle: "Protractor Automation Report",
  reportFailedUrl: true,
  inlineImages: true,
});

exports.config = {
seleniumAddress: 'http://127.0.0.1:4444/wd/hub',

suites: {
    Scenario1: './Scenario_01.js', 
},

SELENIUM_PROMISE_MANAGER: false,

capabilities: {
    'shardTestFiles': false,
    'maxInstances': 1,
    'browserName': 'chrome',
    'chromeOptions': {
     'args': ['disable-extensions', 'start-maximized']  //'--headless',
    }
},
allScriptsTimeout: 11000,
getPageTimout: 10000,
restartBrowserBetweenTests: false,
framework: 'jasmine2',
jasmineNodeOpts: {
    onComplete: null,
    isVerbose: true,
    showColors: true, // is True, prints colors to terminal
    includeStackTrace: true,
    defaultTimeoutInterval: 30000, 
    print: function () {}
},
beforeLaunch: function() {
    return new Promise(function(resolve){
    reporter.beforeLaunch(resolve);
    });
},

onPrepare: function () {

    jasmine.getEnv().addReporter(reporter);
    browser.manage().window().maximize();
    browser.manage().timeouts().implicitlyWait(5000);
    jasmine.getEnv().addReporter( new Jasmine2HtmlReporter({savePath: 'target/screenshots'}));
}
};
describe('Scenario_01', function() {

 var common = require('./scripts/CloseBrowsers.js');    
 var Login = require('./scripts/Login.js'); 
 var customer = require('./scripts/CustomerSelection.js');

 it('Login', function() {
         browser.waitForAngularEnabled(false); 
         Login.login('admin','Adminpwd');
     });

     it('CustomerSelection', function() {
         browser.waitForAngularEnabled(true);
         customer.cusSelection();
     });

afterAll(function(){
    common.closeBrowsers();
});
});
var Login = function() {

     this.login = async function(username, passwordKey){
     await browser.get('http://testwebsite.com/showCust');
     await element(by.name('USER')).sendKeys(username);
     await element(by.name('PASSWORD')).sendKeys(passwordKey);
     await element(by.xpath('/html/body/table/tbody/tr[2]/td/table/tbody/tr/td/table/tbody/tr[3]/td/form/input[9]')).click();
     await element(by.name('YES')).click();
     //browser.sleep(10000);

};
}
module.exports = new Login();
var customer = function(){

    this.cusSelection = async function(){

     await element(by.css(['ng-model="selectedChannel.selected"']));
     await element(by.id('customer-auto-complete')).sendKeys('TestCustomer');
     //browser.sleep(500);

     await element.all(by.css('ul[class^="ui-autocomplete"]')).first().click();
    //browser.sleep(1500);
     await element(by.partialButtonText('Manage Customer')).click();
     browser.sleep(10000);      
    };
}
module.exports = new customer();    
CustomerSelection.js:

Report destination:   target\e2e\screenshots\my-report.html
[12:42:21] I/launcher - Running 1 instances of WebDriver
[12:42:21] I/hosted - Using the selenium server at http://127.0.0.1:4444/wd/hub

(node:18208) UnhandledPromiseRejectionWarning: Error: Error while waiting for Protractor to sync with the page: "both angularJS testability and angular testability are undefined.  This could be either because this is a non-angular page or because you
r test involves client-side navigation, which can interfere with Protractor's bootstrapping.  See http://git.io/v4gXM for details"
    at runWaitForAngularScript.then (C:\Users\Ashish\AppData\Roaming\npm\node_modules\protractor\built\browser.js:463:23)
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:188:7)Error
    at ElementArrayFinder.applyAction_ (C:\Users\Ashish\AppData\Roaming\npm\node_modules\protractor\built\element.js:459:27)
    at ElementArrayFinder.(anonymous function).args [as sendKeys] (C:\Users\Ashish\AppData\Roaming\npm\node_modules\protractor\built\element.js:91:29)
    at ElementFinder.(anonymous function).args [as sendKeys] (C:\Users\Ashish\AppData\Roaming\npm\node_modules\protractor\built\element.js:831:22)
    at customer.cusSelection (C:\Users\Ashish\Documents\Protractor\scripts\CustomerSelection.js:7:50)
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:188:7)
(node:18208) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:18208) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
[12:42:25] I/launcher - 0 instance(s) of WebDriver still running
[12:42:25] I/launcher - chrome #01 passed
var HtmlScreenshotReporter = require('protractor-jasmine2-screenshot-reporter');
var reporter = new HtmlScreenshotReporter({
  dest: 'target/e2e/screenshots',
  filename: 'my-report.html',
  ignoreSkippedSpecs: true,
  reportOnlyFailedSpecs: false,
  captureOnlyFailedSpecs: true  ,
  showSummary: true,
  showQuickLinks: true,
  showConfiguration: true,
  reportTitle: "Protractor Automation Report",
  reportFailedUrl: true,
  inlineImages: true,
});

exports.config = {
seleniumAddress: 'http://127.0.0.1:4444/wd/hub',

suites: {
    Scenario1: './Scenario_01.js', 
},

SELENIUM_PROMISE_MANAGER: false,

capabilities: {
    'shardTestFiles': false,
    'maxInstances': 1,
    'browserName': 'chrome',
    'chromeOptions': {
     'args': ['disable-extensions', 'start-maximized']  //'--headless',
    }
},
allScriptsTimeout: 11000,
getPageTimout: 10000,
restartBrowserBetweenTests: false,
framework: 'jasmine2',
jasmineNodeOpts: {
    onComplete: null,
    isVerbose: true,
    showColors: true, // is True, prints colors to terminal
    includeStackTrace: true,
    defaultTimeoutInterval: 30000, 
    print: function () {}
},
beforeLaunch: function() {
    return new Promise(function(resolve){
    reporter.beforeLaunch(resolve);
    });
},

onPrepare: function () {

    jasmine.getEnv().addReporter(reporter);
    browser.manage().window().maximize();
    browser.manage().timeouts().implicitlyWait(5000);
    jasmine.getEnv().addReporter( new Jasmine2HtmlReporter({savePath: 'target/screenshots'}));
}
};
describe('Scenario_01', function() {

 var common = require('./scripts/CloseBrowsers.js');    
 var Login = require('./scripts/Login.js'); 
 var customer = require('./scripts/CustomerSelection.js');

 it('Login', function() {
         browser.waitForAngularEnabled(false); 
         Login.login('admin','Adminpwd');
     });

     it('CustomerSelection', function() {
         browser.waitForAngularEnabled(true);
         customer.cusSelection();
     });

afterAll(function(){
    common.closeBrowsers();
});
});
var Login = function() {

     this.login = async function(username, passwordKey){
     await browser.get('http://testwebsite.com/showCust');
     await element(by.name('USER')).sendKeys(username);
     await element(by.name('PASSWORD')).sendKeys(passwordKey);
     await element(by.xpath('/html/body/table/tbody/tr[2]/td/table/tbody/tr/td/table/tbody/tr[3]/td/form/input[9]')).click();
     await element(by.name('YES')).click();
     //browser.sleep(10000);

};
}
module.exports = new Login();
var customer = function(){

    this.cusSelection = async function(){

     await element(by.css(['ng-model="selectedChannel.selected"']));
     await element(by.id('customer-auto-complete')).sendKeys('TestCustomer');
     //browser.sleep(500);

     await element.all(by.css('ul[class^="ui-autocomplete"]')).first().click();
    //browser.sleep(1500);
     await element(by.partialButtonText('Manage Customer')).click();
     browser.sleep(10000);      
    };
}
module.exports = new customer();    

错误清楚地表明,
CustomerSelection.js
中的承诺拒绝未被处理。根据堆栈跟踪,
await元素(by.xpath(…)
无法解析承诺

解决此问题的方法是在
场景中捕获
customer.cusSelection()
。\u 01.js:
&在那里处理异常

默认情况下,每个带有
async
关键字的方法的返回类型都是承诺。因此,您应该在
场景中等待
customer.cusSelection()
,或者使用
然后catch
子句进行断言

类似这样的事情-

 it('CustomerSelection', async function() {
     browser.waitForAngularEnabled(true);
     let response = await customer.cusSelection();
 });

 // OR this to catch exceptions.

 it('CustomerSelection', function() {
     browser.waitForAngularEnabled(true);
     customer.cusSelection()
        .then(response => console.log(response))
        .catch(error => console.error(error));
 });
Node.js正在朝着处理
未处理的承诺拒绝
的方向发展,类似于将来处理
未处理的异常
错误

这意味着即将到来的节点版本将在遇到承诺拒绝()时终止进程

更新-CustomerSelection.js的代码片段


谢天谢地,朋友。虽然在尝试单击CustomerSelection.js中的“wait element.all(by.css('ul[class^=“ui autocomplete”]”)。first()。click();”元素时出现了“ElementNotVisibleError:element not visible”错误,但现在似乎正在进行中。在寻找元素之前,不是“等待”等待元素可见吗?也尝试了1500毫秒的睡眠。无需等待/异步设置,一切正常。您能建议如何克服这个问题吗?我不太擅长jasmine和量角器,但您能捕获
wait元素.all(by.css('ul[class^=“ui autocomplete”]”)的结果吗?我猜
element.all(by.css('ul[class^=“ui autocomplete”])
在这里返回一个承诺。尝试计数并打印数组中的元素,我可以看到值为'1',这是预期值,让autocompletelem=wait元素。all(by.css('ul[class^=“ui autocomplete”])。然后(函数(计数器){console.log(“值的数量:“+counter”););.但是,最终出现了错误:“TypeError:如果我尝试单击第一个元素,则无法读取undefined”的属性“first”。“让autocompletelem=await元素。all(by.css('ul[class^=“ui autocomplete”]”)。然后(function(){autocompletelem.first().click();});“任何提示?您不应该同时使用await和then。我将用新代码更新我的答案。试着用一下,让我知道它是否有效。运气不好,伙计。以下面的错误结束。TypeError:elements.first不是at进程中customer.cusSelection(C:\Users\Ashish\Documents\dragrator\scripts\CustomerSelection.js:11:22)的函数。_tickCallback(internal/process/next_tick.js:188:7)