1

我需要使用一个变量,其值是根据 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)               
        },
4

1 回答 1

1

尽管命令链中的调用将按顺序执行,但链表达式本身会被解析并在执行开始之前解析参数。所以在这种情况下

return remote
    .findByXpath('...')
    .getAttribute('style')
    .then(function (width) {
        left = parseInt(width);
    })
    .f_selectCell('', 0, left);

left参数 to在f_selectCell链开始执行之前进行评估。当leftthen回调中重新分配时,f_selectCell不会知道它,因为它已经评估left为 0。

相反,您需要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);
于 2016-01-02T17:27:48.803 回答