2015-11-20 52 views
2

我嘗試在量角器中使用IndexOf方法。我無法將數字值改爲Promise。表Promise :: 3138 {[[PromiseStatus]]:「pending」}:無法返回數字

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方法,但我認爲我有一些誤解。

+1

有其他解決方案。您能發佈表格的完整HTML(包括標題)和示例「名稱」值嗎?謝謝。 – alecxe

回答

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;

希望很清楚我在這裏要做什麼。

+0

@alacxe謝謝!這個解決方案看起來很好。但我總是有錯誤:「Failed:The xpath expression。有關此錯誤的文檔,請訪問:http://seleniumhq.org/exceptions/invalid_selector_exception.html」 –

+0

@ЛилияСапурина抱歉,修復了這個問題,請嘗試出。 – alecxe

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')); 
    }); 
}; 
+0

謝謝!但是,然後我返回這個元素,我不能使用像click()這樣的方法。我總是有一個錯誤「失敗:未定義不是一個函數」。也因爲它是諾言我想。 –

+0

你想在哪裏打電話「點擊」?嘗試'.then(function(element){element.click(...);})' – Bergi

+0

是的,但總的來說,我想用這個方法中的元素做很多操作。我做了一項研究,並瞭解到有了承諾我無法做到這一點。我應該找到另一種選擇。 –