如何在量角器.each()函数上使用页面对象变量?

时间:2015-06-02 18:48:18

标签: javascript selenium testing protractor pageobjects

我想知道如何在.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;

1 个答案:

答案 0 :(得分:0)

this在"每个" function / callback不引用页面对象本身。要修复它,请定义一个变量并将其设置为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);
        });
    });
};