【问题标题】:Promise::3138 {[[PromiseStatus]]: "pending"} : can't return numberPromise::3138 {[[PromiseStatus]]: "pending"} : 不能返回数字
【发布时间】:2016-02-22 17:42:27
【问题描述】:

我尝试在量角器中使用 IndexOf 方法。而且我不能返回数值而不是 Promise。

  PsGrid.prototype.getQuickFilterByName = function(name) {
       var num;
       num = this.all(by.xpath(".//tr[contains(@class,'n-grid__head-row')]/th[contains(@class,'n-grid__title')]//div[contains(@class,'n-grid__text')]")).getText().then(function(textArray){
       return textArray.indexOf(name) + 2;
       });

    expect(num).toEqual(8);  // num == 2
    console.log("sdf" + num); // num == Promise::3138 {[[PromiseStatus]]: "pending"} 
    return this.element(by.xpath('.//tr[2]/td[' + num + ']//input')); //protractor can't find element
};

表格的完整HTML代码:

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.1/angular.min.js"></script>
<table class="n-grid">
      <colgroup>
        <col ng-repeat="column in $view.columns track by column.$id" ps-link-element="column.elements.col" ng-attr-width="{{column.settings.width}}" class="ng-scope" width=""><col ng-repeat="column in $view.columns track by column.$id" ps-link-element="column.elements.col" ng-attr-width="{{column.settings.width}}" class="ng-scope" width="185">
        <col ng-if="$view.fillerColumn.needVisible()" ng-attr-width="{{$view.fillerColumn.width}}" class="ng-scope" width="*">
      </colgroup>
      <thead class="n-grid__head" ps-event="{ mousedown: [
                         {exp: 'onMouseDown($event,column,internalOptions.states.preMoveColumn)', selector: 'div.n-grid__event'},
                         {exp: 'onMouseDown($event,column,internalOptions.states.resizeColumn)',  selector: 'div.n-grid-col-resize'}]
                       }">
        <tr class="n-grid__head-row" ng-show="$view.hasHeader">
          <th class="n-grid__title ng-scope ng-isolate-scope n-grid__status" ng-class="column.design.classes.title" ng-repeat="column in $view.columns track by column.$id" ps-link-element="column.elements.head" on-link-element="onLinkHead(column, $last)" ps-context-menu="menuOptions.show(column, $event)" ps-grid-bind-element="column.elements.title"><span ng-if="psGrid.isMultiSelect()" class="n-grid__select-all ng-scope"></span></th><th class="n-grid__title ng-scope ng-isolate-scope" ng-class="column.design.classes.title" ng-repeat="column in $view.columns track by column.$id" ps-link-element="column.elements.head" on-link-element="onLinkHead(column, $last)" ps-context-menu="menuOptions.show(column, $event)" ps-grid-bind-element="column.elements.title">
            <div class="n-grid-title-in" ng-style="{'z-index': $view.columns.length - $index}" style="z-index: 2;">
              <div class="n-grid__event n-grid__event_sort" ng-class="{'n-grid__event_sort': column.sortable}">
                <div class="n-grid__text ng-binding" ng-attr-title="{{column.title()}}" ng-bind="column.caption()" title="Имя клиента">Имя клиента</div>
                <span class="n-grid__sort-icon"></span>
              </div>
              <div ng-if="column.resizable" class="n-grid-col-resize ng-scope" ng-dblclick="menuOptions.onColumnAutoSize(column)"></div>
            </div>
          </th><th class="n-grid__title ng-scope ng-isolate-scope n-grid__title_sortable" ng-class="column.design.classes.title" ng-repeat="column in $view.columns track by column.$id" ps-link-element="column.elements.head" on-link-element="onLinkHead(column, $last)" ps-context-menu="menuOptions.show(column, $event)" ps-grid-bind-element="column.elements.title">
            <div class="n-grid-title-in" ng-style="{'z-index': $view.columns.length - $index}" style="z-index: 1;">
              <div class="n-grid__event n-grid__event_sort" ng-class="{'n-grid__event_sort': column.sortable}">
                <div class="n-grid__text ng-binding" ng-attr-title="{{column.title()}}" ng-bind="column.caption()" title="Номер">Номер</div>
                <span class="n-grid__sort-icon"></span>
              </div>
              <div ng-if="column.resizable" class="n-grid-col-resize ng-scope" ng-dblclick="menuOptions.onColumnAutoSize(column)"></div>
            </div>
          </th>
          <th ng-if="$view.fillerColumn.needVisible()" class="n-grid__title ng-scope ng-isolate-scope" ps-context-menu="">
            <div class="n-grid-title-in">&nbsp;</div>
          </th>
        </tr>

        <tr ng-if="$view.hasVisibleFilters" class="n-grid__head-row ng-scope" ps-event="{ 'keydown': {exp: '$event.keyCode === 13 &amp;&amp; $currentScope.onFilterApply()', selector: '.inp-text', apply: true},
                        'apply.value.component': {exp: '$currentScope.onFilterApply()', apply: true}}">
          <td ng-class="column.design.classes.filter" ng-repeat="column in $view.columns track by column.$id" ps-grid-bind-element="column.elements.filter || column.elements.filter_empty" class="ng-scope n-grid__title n-grid__status"><a ng-if="$view.hasVisibleFilters" ng-click="onFilterClear()" class="n-grid__cancel-filter ng-scope" ng-attr-title="{{locale.psGrid.clearFilter}}" title="Очистить фильтр"><span class="n-grid__filter-icon"></span></a></td><td ng-class="column.design.classes.filter" ng-repeat="column in $view.columns track by column.$id" ps-grid-bind-element="column.elements.filter || column.elements.filter_empty" class="ng-scope n-grid__filter">
          <input class="inp-text ng-scope ng-pristine ng-valid" ng-mousedown="in.filter.dirty.customer.filterMethod=4" ng-model="in.filter.dirty.customer.name" maxlength="64">
        </td><td ng-class="column.design.classes.filter" ng-repeat="column in $view.columns track by column.$id" ps-grid-bind-element="column.elements.filter || column.elements.filter_empty" class="ng-scope n-grid__filter">
          <input class="inp-text ng-scope ng-pristine ng-valid" ng-model="in.filter.dirty.inquiry.ids" ps-input-allow="/^[\s,;0-9]*$/">
        </td>
          <td ng-if="$view.fillerColumn.needVisible()" class="n-grid__filter ng-scope">
            <span class="n-grid__empty-placeholder">&nbsp;</span>
          </td>
        </tr>
      </thead>
    </table>

列名 - “Номер”。

我尝试编写 addLocator 方法,但我认为我在那里有一些误解。

【问题讨论】:

  • 有一个替代解决方案。您能否发布表格的完整 HTML(包括标题)和示例“名称”值?谢谢。

标签: angularjs promise protractor angularjs-e2e


【解决方案1】:

您可以使用相当复杂的 XPath 表达式一次性将元素定位到所需列中。这个想法是获取所需标题单元格的位置并使用该位置来定位所需的“数据”单元格。为了便于阅读,分成多行:

.//tr[2]/td[
    count(//preceding::tr[contains(@class,'n-grid__head-row')]/th[
        contains(@class,'n-grid__title') and .//div/@title = 'Номер'
    ]/preceding-sibling::th) + 3
]//input

其中Номер 是标头名称的示例。为什么+ 3? - 1 需要定义标题的位置(我们正在计算前面的th 元素来计算标题位置),另一个2 来自您提供的代码:return textArray.indexOf(name) + 2;

希望我想在这里做什么很清楚。

【讨论】:

【解决方案2】:

是的,num 是一个承诺。所以如果你想用它做任何事情,它需要进入一个then回调;而你的函数只能返回另一个承诺。

PsGrid.prototype.getQuickFilterByName = function(name) {
    return this.all(by.xpath(".//tr[contains(@class,'n-grid__head-row')]/th[contains(@class,'n-grid__title')]//div[contains(@class,'n-grid__text')]")).getText().then(function(textArray){
        return textArray.indexOf(name) + 2;
    }).then(function(index) {
        expect(index).toEqual(8);
        console.log("sdf" + index);
        return this.element(by.xpath('.//tr[2]/td[' + index + ']//input'));
    });
};

【讨论】:

  • 谢谢!但是后来我返回了这个元素,我不能使用像 click() 这样的方法。我总是有一个错误“失败:未定义不是一个函数”。也因为它是我想的承诺。
  • 你想在哪里打电话给click?试试.then(function(element) { element.click(…); })
  • 是的,但总的来说,我想在这个方法中用我的元素做很多动作。我进行了一项研究,并了解到使用 Promises 我无法做到这一点。我应该找到另一种选择。
猜你喜欢
  • 2018-08-19
  • 2019-12-23
  • 2020-07-09
  • 2017-10-16
  • 1970-01-01
  • 2023-03-21
  • 2021-10-26
  • 1970-01-01
  • 2023-04-01
相关资源
最近更新 更多