1

为了测试,我们必须使用 intern/leadfoot 填充一个复杂的页面。页面的每个部分都由一个单独的函数处理,该函数接收必要的元素和输入数据。

现在我们遇到了问题,对子函数中这些元素的操作不能再被链接,因为它们是元素而不是命令。

是否有可能再次链接操作?我尝试了很多使用setContext()或使用自定义命令创建新命令,但到目前为止没有成功。

let inputs;
return this.remote
  .get('some/url')
  .findAllByTagName('input') // Finds two input elements
  .then(inputElements=> inputs = inputElements)
  .then(()=> Promise.all([
      inputs[0].clearValue(), // I would like to be able to write: inputs[0].clearValue().type('a')
      inputs[1].clearValue(),
  ]))
  .then(()=> Promise.all([
      inputs[0].type('a'),
      inputs[1].type('b'),
  ]))
4

1 回答 1

1

元素与命令共享许多相同的方法,但它们具有不同的 API。主要区别在于,表示动作的 Command 方法返回 Commands(Command 类似于 promise,并在动作完成时解析),但表示动作的 Element 方法不返回 Elements(Element 不类似于 promise)。这意味着您不能直接链接许多 Element 方法。

对于问题中描述的情况,您可以执行以下操作:

function clearAndType(input, value) {
    return remote.then(function (_, setContext) {
            // setContext is always the last argument to a Command then()
            // callback; the value returned by the previous Command is the
            // first argument, which is ignored here
            setContext(input);
        })
        .clearValue()
        .type(value);
}

var remote = this.remote;

return this.remote
    .get('some/url')
    .findAllByTagName('input')
    .then(function (inputs) {
        return Promise.all([
            clearAndType(inputs[0], 'a'),
            clearAndType(inputs[1], 'b'),
            // ...
        ]);
    })
于 2016-04-19T00:10:45.563 回答