var funcs = [];
// let's create 3 functions
for (var i = 0; i < 3; i++) {
// and store them in funcs
funcs[i] = function() {
// each should log its value.
console.log("My value: " + i);
};
}
for (var j = 0; j < 3; j++) {
// and now let's run each one to see
funcs[j]();
}Run Code Online (Sandbox Code Playgroud)
它输出这个:
我的价值:3
我的价值:3
我的价值:3
而我希望它输出:
我的价值:0
我的价值:1
我的价值:2
使用事件侦听器导致运行函数的延迟时,会出现同样的问题:
var buttons = document.getElementsByTagName("button");
// let's create 3 …Run Code Online (Sandbox Code Playgroud)我正在编写量角器测试并喜欢它,虽然有时似乎陷入了似乎应该简单的事情.例如,我想循环浏览其中一个页面上具有"Nominate"文本的所有按钮.页面上有几十个,但只有1或2个可见.所以我想点击第一个.这是我目前使用的代码:
var nominateButtons = element.all(by.buttonText('Nominate'));
nominateButtons.then(function(els){
for(var x = 0;x < els.length;x++){
//Since isDisplayed returns a promise, I need to do it this way to get to the actual value
els[x].isDisplayed().then(function(isVisible){
//isVisible now has the right value
if(isVisible){
//But now els is not defined because of the scope of the promise!!
els[x].click();
}
});
}
});
Run Code Online (Sandbox Code Playgroud)
当我运行此代码时,我得到一个'无法调用方法点击未定义'错误,因为els [x]不再在范围内,但我似乎无法在不使用promise的情况下检查可见性.所以我的问题是,你怎么能循环遍历一系列元素,检查它们的可见性,然后点击第一个可见元素?(我试图不使用期望来检查可见性,因为我知道大多数按钮都不可见)
提前致谢
(我看过这个 SO 讨论,但不确定如何将其应用于我的案例,所以我提出了一个新问题。希望它不是重复的)
我正在使用带有 Cucumber.js 的量角器测试用 Angular 编写的表单。
所以我想要做的是告诉量角器点击一个字段的标题(这是一个链接),然后,当该字段出现时,在其中输入一些文本,然后转到下一个字段的标题,等等。
这是我在黄瓜中的步骤:
When I fill the form with the following data
| field | content |
| First Name | John |
| Last Name | Doe |
| Address | Some test address |
# and so forth
Run Code Online (Sandbox Code Playgroud)
这是对步骤定义的一个半心半意的尝试:
this.When(/^I fill the form with the following data$/, function (table, callback) {
data = table.hashes();
# that gives me an array of objects such as this one:
# [ { …Run Code Online (Sandbox Code Playgroud)