【发布时间】:2016-03-07 20:36:30
【问题描述】:
我很难适应使用 node.js 的异步。我在使用 selenium-webdriver 和页面对象模式时遇到了问题。我觉得在进行自动化测试时某些东西必须是同步的,否则您的测试将失败,因为您在插入数据之前单击了一个按钮。我有一个类似的问题。我想添加一名员工,然后搜索该员工,但搜索员工是在添加员工之前执行的。
var employee = new Employee('grimlek', 'Charles', 'Sexton', 'TitleTitle',
'Upper Management', 'Company Admin', 'Contractor', '-7', 'Remote',
'05212016', '3369407787', '3368791234', 'charles@example.com',
'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 函数都不会返回承诺,我正在尝试将它们与 .then 函数链接起来。我确实相信这是我的问题,但我需要关于如何完成而不是如何操纵它的帮助。我应该使用回调吗?我已经在这个问题上工作了四个小时,我尝试过谷歌搜索并研究各种主题。如果我没有提供足够的代码,请告诉我,我将提供一个简化的可运行示例。
【问题讨论】:
-
我已经从 node_module 中删除了异步和异步库并再次安装 npm,每次我启动调试模式时都会发生这种情况
标签: javascript node.js asynchronous selenium-webdriver