如何在量角器

时间:2017-05-23 15:30:32

标签: javascript promise protractor

我在UI组件中有一个'select'元素,我需要从中检索所选的选项(如果有的话)。作为JavaScript和量角器的初学者,我无法在没有一堆嵌套承诺的情况下弄清楚如何实现这一点:

我有两个定位器 - 一个用于选择器的当前选择,一个用于所有选项:

selector = element(by.model("something.someId"));
this.selectorOptions = element.all(by.repeater("repeat in someOptions | orderBy:'name'"));

getSelectedOption = function () {
    return this.selector.getText().then( function (selectionText) {
        return this.selectorOptions.filter(function (option) {
            option.getText().then(function (optionText) {
                if(optionText === selectionText) {
                    option.getAttribute("value").then(function (value) {
                        // Some logic here which uses the value to return an pojo representing the selection
                    })
                }
            })
        })
    })
};

上述情况非常糟糕,我相信这可以做得更好。我已经看了很多例子,但我没有找到一个涉及处理嵌套的promises,需要接受参数,然后根据值做一些条件,所以我很难将它们应用到我的情况,主要是因为我对异步编程感到不舒服。我如何处理上面的混乱并将其重构为不是嵌套回调地狱的东西?

1 个答案:

答案 0 :(得分:0)

也许玩一些承诺,量角器,参数和绑定你可以让它更清洁。 然后你使用量角器filter方法,它需要返回一个布尔值,以便过滤你的值。但是,从你使用它的方式来看,也许吧 你在寻找each()http://www.protractortest.org/#/api?view=ElementArrayFinder.prototype.each

我没有机会测试以下代码,因此很可能无效:D

selector = element(by.model("something.someId"));
this.selectorOptions = element.all(by.repeater("repeat in someOptions | orderBy:'name'"));

getSelectedOption = function () {
  return this.selector.getText().then(firstText.bind(this));
};

function firstText(text) {
  return this.selectorOptions.filter(filterSelector.bind(this, text));
}

function filterSelector(text, option) {
  return option.getText().then(optionText.bind(this, text, option));
}

function optionText(text, option, optionText) {
  if(optionText === text) {
    return option.getAttribute("value").then(someLogic);
  }
}

function someLogic(value) {
  console.log(value);
  // value should be your value
  // Some logic here which uses the value to return an pojo representing the selection
  // return true or false, filter is still waiting for a boolean...
}

另一个版本只使用没有函数参数的参数。特别按照打印的参数,查看订单是否正确:

selector = element(by.model("something.someId"));
this.selectorOptions = element.all(by.repeater("repeat in someOptions | orderBy:'name'"));

getSelectedOption = function () {
    return this.selector.getText().then(firstText.bind(this));
};

function firstText() {
  console.log(arguments);
  // arguments[0] should be your selectionText
    return this.selectorOptions.filter(filterSelector.bind(this, arguments[0]));
}

function filterSelector() {
  console.log(arguments);
  // arguments[0] should be your previous selectionText 
  // arguments[1] should be your option
    return arguments[1].getText().then(optionText.bind(this, arguments[0], arguments[1]));
}

function optionText() {
  console.log(arguments);
  // arguments[0] should be your optionText
  // arguments[1] should be your selectionText
  // arguments[2] should be your option
    if(arguments[0] === arguments[1]) {
    return arguments[2].getAttribute("value").then(someLogic);
  }
}

function someLogic(value) {
  console.log(value);
  // value should be your value
    // Some logic here which uses the value to return an pojo representing the selection
  // return true or false, filter is still waiting for a boolean...
}