量角器:自定义ExpectedConditions-属性更改时

时间:2018-10-13 12:24:11

标签: javascript selenium-webdriver protractor angularjs-1.7

我正在尝试实现自定义的ExpectedConditions方法,该方法将等待element属性更改。

这是我的解决方法:

const ECC = function() {
  /**
   * Expect element attribute to have specific value.
   *
   * @param {ElementFinder} elementFinder
   * @param {string} attrName attribute name to check
   * @param {string} attrVal attribute value to check for
   *
   * @return {boolean}
   */
  this.attributeToHave = async (elementFinder, attrName, attrVal) => {
    const EC = protractor.ExpectedConditions;
    const hasAttr = async () => {
      const actualText = elementFinder.getAttribute(attrName);
      return actualText.indexOf(attrVal) !== -1;
    };

    return await EC.and(EC.presenceOf(elementFinder), await hasAttr);
  };
};

module.exports = new ECC();

在我的onPrepare中:

const {expectedConditions} = require('@utils/protractor');
global.ECC = expectedConditions;

最后在我的测试套件中:

 await browser.wait(await ECC.attributeToHave(dropdown, 'aria-hidden', 'false'), 3000);

但是它一直在说Failed: Wait timed out after 3006ms,请问我做错了吗?

1 个答案:

答案 0 :(得分:4)

请通过以下更改重试:

this.attributeToHave = async (elementFinder, attrName, attrVal) => {
    const EC = protractor.ExpectedConditions;
    const hasAttr = async () => {
      const actualText = await elementFinder.getAttribute(attrName); // should add await
      return actualText.indexOf(attrVal) !== -1;
    };

    return await EC.and(EC.presenceOf(elementFinder), await hasAttr()); // should call `hasAttr()` this function.
};