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