我很难适应使用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函数将它们链接起来。我确实认为这是我的问题,但我需要帮助,它应该如何做,而不是我如何能够操纵它。我应该使用回调吗?我已经为这个问题工作了四个小时,我已经尝试谷歌和做各种主题的研究。如果我没有提供足够的代码,请告诉我,我将提供一个简化的运行示例。
发布于 2016-03-04 18:56:01
一个值得称赞的目标是使每个测试独立。如果对应用程序(例如,g,bug修复)进行了更改,则只需要执行受影响的测试。同时,它也使得移动到网格是可以想象的。
但这在实践中是很难实现的。您的测试必须包括满足先决条件所需的所有测试。
黄瓜有包含场景的特性文件,每个场景都是一个测试。场景按照特性文件中列出的顺序执行。因此,组织事物的一种方法是将测试前的所有先决条件场景都包含在一个特性文件中,您可以在Feature语句之前添加标签,以便在执行该标记时运行整个特性文件。也许第一个场景将数据库重置为一个已知状态。
诀窍是在多台机器上并行运行特性。如果将多个客户端指向同一台服务器,请注意这些功能不应创建或更新服务器写入数据库时可能发生冲突的重叠实体。例如。“用户‘汤姆’已经存在是什么意思?”每个特性都需要创建一个唯一的用户名。
发布于 2016-03-07 12:36:30
使用黄瓜的方法是为每个单独的操作划分步骤。
例如:
Given I am on XYZ Form
And I provide all form details在上述情况下,对于步骤And I provide all form details,您将包含步骤定义中的所有字段,并开始在单步定义中填充字段--例如名称、姓氏、地址。
而不是这样,我们应该为每个单独的字段划分步骤,例如:
Given I am on XYZ Form
And I provide name details in XYZ Form
And I provide last name details in XYZ Form
And I provide address details in XYZ Form然后,我们将编写三个步骤的定义,当然,它将按顺序运行。
您可能会觉得输入工作增加了,而步骤定义不必要地增加了,但是这实际上会帮助您将字段从应用程序本身中删除,您只需要从未来的文件中删除相关步骤。更重要的是,您可以简单地通过注释特性文件中的一个步骤来测试字段的验证。而且您的代码将更易于维护,因为每个步骤都是独立工作的。
当然,接下来的工作也会实现。
https://stackoverflow.com/questions/35800983
复制相似问题