简体   繁体   中英

How to use page object variable on protractor .each() function?

I was wondering how to use page object variable on .each() function.

The scenario is every I click delete link, the sweet alert confirmation will be shown, and I must confirm that dialog to delete the data.

Here is my page object:

'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;

this inside the "each" function/callback does not refer to the page object itself. To fix it, define a variable and set it to 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);
        });
    });
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM