【问题标题】:Get textToBePresentInElementValue length获取 textToBePresentInElementValue 长度
【发布时间】:2019-08-13 06:59:57
【问题描述】:

我想检查我的隐藏输入是否包含一个值,如果不是等到它包含。

原因是当隐藏的输入有一个值并且我想使用该值时,我知道页面已完成加载。

我尝试了以下方法: 这个textToBePresentInElementValue 得到了很好的值,但我需要它等到它包含一个值。

我尝试过这样的事情: browser.wait(EC.textToBePresentInElementValue($('#isearchstring').lenght > 0), 5000);

但我得到一个错误: Cannot read property 'bind' of undefined

当我的页面加载时,我的 HTML 如下所示:<input type="hidden" name="searchstring" id="isearchstring" value=""> 几秒钟后它可能包含任何值 <input type="hidden" name="searchstring" id="isearchstring" value="xxxxx">

我不想使用browser.sleep();

【问题讨论】:

    标签: javascript automation protractor


    【解决方案1】:

    使用getAttribute('value') 获取输入值(无论是否可见)

    browser.wait(function(){
      return element(by.css('input#isearchstring'))
              .getAttribute('value')
              .then(function(value){
                  return value !== '';
              });
    }, 5000);
    

    【讨论】:

    • !=== 不是有效的运算符。将其替换为 !== 运算符。
    【解决方案2】:

    如果你想等待一个元素有文本,你可以为它写一个函数:

    const EC = protractor.ExpectedConditions;
    
    const isTextPresent = function(elm) {
      const hasText = function() {
        return elm.getText().then(function(elmText) {
          return elmText;
        });
      };
      return EC.and(EC.presenceOf(elementFinder), hasText);
    };
    

    然后使用它:

    browser.wait(isTextPresent(element(by.binding('myvar'))), 5000);

    【讨论】:

    • 对于输入应该使用getAttribute('value'),而不是getText(),并且getText()不能在隐藏元素上工作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-07
    • 2015-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多