【问题标题】:How to use page object variable on protractor .each() function?如何在量角器 .each() 函数上使用页面对象变量?
【发布时间】:2015-08-16 17:22:57
【问题描述】:

我想知道如何在.each() 函数上使用页面对象变量。

场景是我每次点击删除链接,都会显示甜蜜警报确认,我必须确认该对话框才能删除数据。

这是我的页面对象:

'use strict';

// page object name
var Data = function()
{
    // all delete links
    this.delete_links = element.all(by.css('div[ng-click="delete(Data.id)"]'));

    // confirm button
    this.btn_confirm = element(by.css('.confirm'));

    // delete function
    this.delete = function()
    {
        // delete all links with confirmation
        this.delete_links.each(function(element, index)
        {
            // click delete link
            element.click().then(function()
            {
                browser.sleep(1000);
            });

            // click yes
            this.btn_confirm.click().then(function()
            {
                browser.sleep(1000);
            });
        });
    };
};

module.exports = Data;

【问题讨论】:

  • 但是您遇到了什么困难? delete 函数是否符合您的预期?谢谢。
  • @alecxe 不,它说“TypeError:无法读取未定义的属性'btn_confirm'”先生。

标签: javascript selenium testing protractor pageobjects


【解决方案1】:

this 在“each”函数/回调中不引用页面对象本身。要修复它,请定义一个变量并将其设置为this.btn_confirm

this.delete = function()
{
    // delete all links with confirmation
    this.delete_links.each(function(element, index)
    {
        var confirmButton = this.btn_confirm;

        // click delete link
        element.click().then(function()
        {
            browser.sleep(1000);
        });

        // click yes
        confirmButton.click().then(function()
        {
            browser.sleep(1000);
        });
    });
};

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-07-18
    • 2014-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-13
    相关资源
    最近更新 更多