在实习功能测试中使用变量

时间:2015-12-21 18:27:07

标签: intern leadfoot

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

1 个答案:

答案 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);