【问题标题】:Page Object Pattern asynchronous using node.js selenium使用 node.js selenium 异步页面对象模式
【发布时间】: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


【解决方案1】:

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

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

Cucumber 具有包含场景的功能文件 每个场景都是一个测试。场景按照它们在功能文件中列出的顺序执行。因此,组织事物的一种方法是将测试之前的所有先决条件包含在功能文件中,您可以在功能语句之前添加tag(s),以便在执行该标记时运行整个功能文件。也许第一个场景将数据库(一个子集)重置为已知状态。

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

【讨论】:

【解决方案2】:

使用黄瓜的方法是为每个单独的操作划分步骤。

例如:

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 步定义,这些定义当然会按顺序运行。

您可能会觉得打字工作增加了,步骤定义增加了不必要的,但这实际上会在从应用程序本身中删除字段时帮助您,您只需要从将来的文件中删除相关步骤。 此外,您只需在功能文件中注释其中一个步骤即可轻松测试字段验证。 由于每个步骤都是独立工作的,因此您的代码将更易于维护。

当然,顺序工作也会得到实现。

【讨论】:

  • OP 没有提到 Cucumber。我仍然很感兴趣,因为我正在尝试将 Cucumber 与节点一起使用。即使有分离步骤,我对 selenium 守护进程的异步调用都没有执行。每个黄瓜步骤似乎都将命令排队并在完成之前继续前进。程序在一些异步请求甚至发送到服务器之前就结束了。
  • 我相信您正在为功能中的每个步骤创建相应/单独的步骤定义。
  • 这可能会让你感兴趣spin.atomicobject.com/2014/12/17/…
猜你喜欢
  • 1970-01-01
  • 2017-06-17
  • 2011-10-26
  • 1970-01-01
  • 2012-09-02
  • 2012-05-10
  • 2023-03-27
  • 1970-01-01
  • 2021-07-20
相关资源
最近更新 更多