量角器 - 等待 class 变化

Protractor - wait for class change

量角器如何等待元素改变属性?当我进入主页时,连接按钮有class=红色,想等到它从class变成绿色

这是您要解决的任务的通用方法

/**
*  waitUntilElementHasAttribute
*  @param    {ElementFinder}    $element                    Locator of element
*  @param    {String}           attributeName               Attribute of an element to evaluate
*  @param    {string}           [attributeString=""]        A target attribute value
*  @param    {number}           [timeout=1000]              Time in ms
*  @return   {Promise}
*/
waitUntilElementHasAttribute($element, attributeName, attributeString = '', timeout = 1000) {
  return browser.wait(
    () =>
      $element.getAttribute(attributeName).then(attributeValue => {
        if (attributeValue !== null) {
          return attributeValue.includes(attributeString);
        } else {
          return false;
        }
      }),
      timeout,
      "Wait until element '" + $element.locator() + "' HAS " + attributeName + ': ' + attributeString
  );
},

注意这个等待,直到指定的属性与字符串部分匹配

这样调用就可以使用

await waitUntilElementHasAttribute(
  $element,
  'class',
  'green',
  3000
)