【问题标题】:Protractor element not visible input field量角器元素不可见的输入字段
【发布时间】:2024-01-18 07:11:01
【问题描述】:

您好,我的问题是我不断收到消息,量角器中“元素不可见”。我只是想让它找到输入字段并向它发送密钥,但它不起作用。整个项目站点都是用 angualrJS 完成的,我什至有 browser.ignoreSynchronization = true;放。有什么建议么。

HTML 代码

input parse-search-query="" type="text" tabindex="1" name="search_query" autocomplete="off" placeholder="Search by City ST, Zip, or Address" data-ng-model="searchQueryFieldCtrl.searchFormController.searchParams.search_query" data-ng-change="searchQueryFieldCtrl.searchFormController.clearErrors()" data-focus-on="focusQuery || form.search_query.$error" data-uib-typeahead="suggestion as suggestion.label for suggestion in searchQueryFieldCtrl.getSuggestions($viewValue)" data-typeahead-focus-first="false" data-typeahead-min-length="0" data-typeahead-wait-ms="300" data-typeahead-on-select="searchQueryFieldCtrl.setSearch($item)" class="ng-pristine ng-untouched ng-valid ng-scope" aria-autocomplete="list" aria-expanded="false" aria-owns="typeahead-69-869">

spec.js

describe('New stack hompage test', function() {
  it('should test search form', function() {

    browser.ignoreSynchronization = true;


  var flow = protractor.promise.controlFlow();
  function waitOne() {
    return protractor.promise.delayed(3000);
  }

  function sleep() {
    flow.execute(waitOne);

  }
        browser.get('http://localhost:3000/');
        sleep() 

        expect(browser.getTitle()).toEqual('Find Homes for Sale, Rent & Real Estate Listings Nationwide at Homes.com');
        sleep() 

        var search = element.all(by.css("input[name='search_query']"));
        sleep()
        search.sendKeys('anything'); // this is not working
  });
});

【问题讨论】:

  • 如果它是一个角度应用程序,你为什么要设置ignoreSynchronization = true
  • 我收到同步错误
  • 您是否尝试过延长超时时间?大多数超时默认为 10 秒,我认为这非常低。 timeout reference
  • Yes 扩展了 22 很多人在堆栈溢出时遇到了同样的问题,不得不将其设置为 (true)。
  • element.all 返回元素数组,因此请使用数组索引号,例如搜索[0].sendKeys('anything');

标签: javascript angularjs protractor automated-tests


【解决方案1】:

您可以使用 protractor.ExpectedConditions 在向其发送输入之前检查元素是否可见。您可以按照以下代码:

var EC=protractor.ExpectedConditions;

 describe('New stack hompage test', function() {
    beforeEach(function(){
       browser.get('http://localhost:3000/');
     });
    it('should test search form', function() {
      var search =  
              element.all(by.css("input[name='search_query']")).first();
      expect(browser.getTitle()).toEqual('Find Homes for Sale, Rent & Real 
                                Estate Listings Nationwide at Homes.com');
      browser.wait(EC.visibilityOf(search).call(),8000,'search filed not 
                                                          visible');
      search.sendKeys('anything');
    });
});

【讨论】:

  • 你能解释一下 .call() 在这种情况下的作用吗?
  • @user2020347 用于处理 EC.visibilityOf(ele) 的承诺
  • 我从量角器收到此消息:失败:无法读取未定义的属性“绑定”。
  • 你是说即使这是一个输入框让我输入,我也可能无法向它发送密钥?
  • @JonathanHofler。多个元素是否具有相同的定位器 - input[name='search_query']" ?
最近更新 更多