javascript - 如何在 Protractor .each() 函数上使用页面对象变量?

标签 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;

最佳答案

“each”函数/回调中的

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

关于javascript - 如何在 Protractor .each() 函数上使用页面对象变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30604190/

相关文章:

javascript - 从 fs.readFile 获取数据

Selenium 无法按类名查找元素

java - 如果所有元素 xpath 都相同,如何区分元素

testing - 下面的伪代码中 i 的值是多少?

javascript - 有没有办法在 JavaScript 中获取对象内存地址和对象哈希

javascript - 在 Angular 中更新表行数据

javascript - execute_script() 在带有 selenium phantomjs 的 python 中不起作用

javascript - 如何计算网站的页面加载时间,包括总时间而不仅仅是 document.ready() 时间?

PhpStorm 测试 : Cannot select PHPUnit to run test

google-chrome - 在较旧的 google chrome 版本中进行测试