2016-03-04 46 views
0

我有一个很难试图调整使用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搜索并对各种主题进行了研究。如果我没有提供足够的代码,请告诉我,我将提供一个简化的可运行示例。

回答

1

一个值得称赞的目标是使相互独立的测试。如果对应用程序进行更改(例如,错误修复),则只需执行受影响的测试。此外,它使移动到可以想象的网格。

但是,这是在实践中难以实现。您的测试必须包含满足先决条件所需的所有测试。

Cucumber具有包含场景的功能文件每个场景都是测试。方案按照它们在功能文件中列出的顺序执行。因此,要整理东西的一种方法是包括所有先决条件的情况之前您在要素文件测试,您可以将功能语句前加上tag(s)这样,当你执行该标签的整个功能文件运行。也许第一个场景将数据库(的一个子集)重置为知道状态。

诀窍是在多台机器上并行运行功能。如果您将这些多个客户端指向同一服务器,请注意这些功能不应创建或更新由服务器写入数据库时​​可能发生冲突的重叠实体。例如。 “你这个用户'tom'是什么意思?”每个功能都需要创建一个唯一的用户名。

+0

是promise在node.js中正常吗?我开发的每个解决方案最终都会在测试结束时一个接一个地链接在一起。 – Grim

+0

这是否[Nodejs /问:链接承诺顺序](http://stackoverflow.com/questions/27322994/nodejs-q-chaining-promises-sequentially)帮助? – MikeJRamsey56

+0

也看看这个[幻灯片集]。(OPT没有提及黄瓜。)(http://pt.slideshare.net/alan_parkinson/cross-browser-testing-java-script-47642180) – MikeJRamsey56

0

使用黄瓜的方法是将每个操作的步骤分开。

例:

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 

然后我们会写这当然会按顺序运行3步骤定义。

您可能会觉得打字工作增加了,而且步骤定义不必要地增加了,但这实际上可以帮助您将字段从应用程序本身中删除,您只需要从将来的文件中删除相关步骤。 只需评论功能文件中的某一步,就可以轻松测试字段验证。 随着每个步骤独立工作,您的代码将更容易维护。

当然顺序工作将会实现。

+0

OP没有提及黄瓜。尽管如此,我仍然感兴趣,因为我正在尝试将Cucumber与节点一起使用。即使分离出来了,我的异步调用selenium守护进程也不会执行。每个黄瓜步骤似乎排队命令,并继续前进。程序在一些异步请求被发送到服务器之前结束。 – Akron

+0

我相信你正在为功能中的每一步创建相应/单独的步骤定义。 –

+0

这可能会让你感兴趣 https://spin.atomicobject.com/2014/12/17/asynchronous-testing-protractor-angular/ –