NodeJS Selenium-webdriver 自定义等待方法未返回所需结果

NodeJS Selenium-webdriver custom wait method not returning desired result

我正在尝试在 Mocha 中编写一组自动化测试来测试节点 Web 应用程序。

我制作了一个自定义等待方法,以便让 webdriver 等到输入框或文本区域包含从 sendKeys() 方法发送的文本,然后再继续检查。

我无法让自定义等待方法实际 return 我想要的值。

    function waitForAttributeValue(element, attribute, value)
    {
       return element.getAttribute(attribute).then(result => 
       {
          if(result === value)
             return result;
          else
             return false;
       });
    }

然后我像这样放置一个等待:

let result = this.driver.wait(waitForAttributeValue(element, 'value', 'hello'), 4000);

结果有时会 return 我期望的值(在本例中为 'hello'),有时会 return false 导致我的测试失败。

基于 documentation

To define a custom condition, simply call WebDriver.wait with a function that will eventually return a truthy-value (neither null, undefined, false, 0, or the empty string)

我是否遗漏了什么或者我是否误解了文档?

此外,我正在使用: 节点 v.8.11.3 和 Selenium 节点包 v.4.0.0-alpha.1

感谢您提前提出任何建议。

driver.wait() 期望一个函数作为参数,但你传入了一个 promise。

您应该将函数 waitForAttributeValue 更改为 return 函数,如下所示:

function waitForAttributeValue(element, attribute, value) {
    return function () {
        return element.getAttribute(attribute).then(result => {
            if (result === value)
                // return result; recommend to return true as here
                // because empty string will be treated as False
                return true;
            else
                return false;

            // or you cam simply return result === value to avoid
            // using  if/else.
            return result === value;

        });
    };
}