我有一個很難試圖調整使用node.js中對異步我在使用selenium-webdriver和頁面對象模式時遇到了一個問題。在進行自動化測試時,我覺得有些東西必須是同步的,否則因爲在插入數據之前單擊按鈕,測試將失敗。我遇到類似這樣的問題。我想添加一名員工,然後搜索員工,但在添加員工之前搜索員工正在執行。頁對象模式異步使用Node.js的硒
var employee = new Employee('grimlek', 'Charles', 'Sexton', 'TitleTitle',
'Upper Management', 'Company Admin', 'Contractor', '-7', 'Remote',
'05212016', '3369407787', '3368791234', '[email protected]',
'charles.sexton', 'Skype', 'abcdefgh');
driver.get('https://website.com/login')
.then(function() {
//This behaves as intended
loginPage.login('company.admin', 'password') })
.then(function() {
//Add employee
employeePage.addEmployee(employee) })
.then(function() {
//Search for employee after employee is added
employeePage.searchEmployee(employee)});
EmployeePage對象
var EmployeePage = function (driver) {
this.addEmployee = function (employee) {
driver.findElement(webdriver.By.css('button[class=\'btn btn-default\']')).then(function (element) {
//
//Search employee function is done before the line below this
//
element.click();
}).then(function() {
setTimeout(function() {
driver.findElement(webdriver.By.id('employee_username')).then(function (element) {
element.sendKeys(employee.username);
});
driver.findElement(webdriver.By.id('employee_first_name')).then(function (element) {
element.sendKeys(employee.firstName);
});
driver.findElement(webdriver.By.id('employee_last_name')).then(function (element) {
element.sendKeys(employee.lastName);
});
driver.findElement(webdriver.By.id('employee_title_id')).then(function (element) {
element.sendKeys(employee.title);
});
driver.findElement(webdriver.By.id('employee_role')).then(function (element) {
element.sendKeys(employee.role);
});
}, 5000);
});
//
//
//Search employee should occur when the thread leaves the function
//
};
this.searchEmployee = function (employee) {
driver.findElement(webdriver.By.css('input[class=\'form-control ng-pristine ng-valid\']')).then(function(element) {
element.sendKeys(employee.firstName + ' ' + employee.lastName);
});
};
};
module.exports = EmployeePage;
我知道searchEmployee和addEmployee函數都沒有返回一個promise,我試圖用.then函數來鏈接它們。我確實相信這是我的問題,但我需要如何完成它的幫助,而不是我如何操縱它。我應該使用回調?我已經在這個問題上工作了四個小時,現在我已經嘗試了Google搜索並對各種主題進行了研究。如果我沒有提供足夠的代碼,請告訴我,我將提供一個簡化的可運行示例。
是promise在node.js中正常嗎?我開發的每個解決方案最終都會在測試結束時一個接一個地鏈接在一起。 – Grim
這是否[Nodejs /問:鏈接承諾順序](http://stackoverflow.com/questions/27322994/nodejs-q-chaining-promises-sequentially)幫助? – MikeJRamsey56
也看看這個[幻燈片集]。(OPT沒有提及黃瓜。)(http://pt.slideshare.net/alan_parkinson/cross-browser-testing-java-script-47642180) – MikeJRamsey56