我需要使用一个变量,其值是根据css样式像素确定的。 测试找到左像素的值,然后选择特定的单元格。但是,当我运行此测试时,该值始终为0而不是实际值。
'Test' : function() {
var left = 0;
var remote = this.remote;
return remote
.setFindTimeout(5000)
.findByXpath("//div[@class = 'grid']//div[@class = 'gridCell' and position() = 1]/div[3]")
.getAttribute("style")
.then( function(width) {
left = parseInt(width.substring(width.indexOf("left")+6,width.indexOf("width")-4));
}).end()
.f_selectCell("", 0, left)
},
答案 0 :(得分:1)
虽然命令链中的调用将按顺序执行,但解析链表达式本身并在执行开始之前解析参数。所以在
的情况下return remote
.findByXpath('...')
.getAttribute('style')
.then(function (width) {
left = parseInt(width);
})
.f_selectCell('', 0, left);
left
的{{1}}参数在链开始执行之前进行评估。在f_selectCell
回调中重新分配left
时,then
无法了解它,因为它已将f_selectCell
评估为0。
相反,您需要在left
回调中调用f_selectCell
方法,或者将then
传递给可以分配属性的object
。
return remote
// ...
.then(function (width) {
left = parseInt(width);
})
.then(function () {
// I'm not entirely sure where f_selectCell is coming from...
return f_selectCell('', 0, left);
});
或
// Put all args to selectCell in this
var selectData = {};
return remote
// ...
.then(function (width) {
selectData.left = parseInt(width);
})
// selectCell now takes an object with all args
// The object is never reassigned during execution.
.f_selectCell(selectData);