我有一个很难试图调整使用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