2015-11-08 90 views
2

我有一個相當簡單的量角器測試,它應該檢查ng重複行中的文本值。量角器 - getText()返回一個數組而不是一個字符串

這裏是我的HTML:

<div ng-repeat="destination in destinations"> 
    <span>{{destination.city}}, {{destination.country}}</span> 
</div> 

這是我的JS:

lastDestination = element.all(by.repeater('destination in destinations').row(1)); 
expect(lastDestination.getText()).toEqual("Madrid, Spain"); 

documentation for getText()狀態:

Get the visible (i.e. not hidden by CSS) innerText of this element, including sub-elements, without any leading or trailing whitespace.

所以我期望從該行的跨度標籤的文字被返回,但是當運行量角器測試時,我得到以下錯誤的斷言:

預計['馬德里,西班牙']等於'西班牙馬德里'。

GetText()似乎是返回一個數組而不是一個字符串。

我試圖解決的getText()的承諾,但仍然得到了同樣的錯誤:

lastDestination = element.all(by.repeater('destination in destinations').row(1)); 

lastDestination.getText().then(function (text) { 
    expect(text).toEqual("Madrid, Spain"); 
}); 

我可以在陣列中針對第一個值獲得解決此問題:

expect(text[0]).toEqual("Madrid, Spain"); 

但我仍然想知道爲什麼這不起作用。

任何想法?

更新:量角器的Github的頁面上的similar bug has been reported,所以它可能是一個Gettext()函數只是不工作,因爲它應該。

回答

4

通過文檔:

// Returns a promise that resolves to an array of WebElements containing 
// the DIVs for the second book. 
bookInfo = element.all(by.repeater('book in library').row(1)); 

您正試圖使用​​gettext上一個承諾,你需要首先解決它。

var lastDestination; 
element.all(by.repeater('destination in destinations').row(1)).then(
    function(elements){ 
      lastDestination = elements[0]; 
}); 
expect(lastDestination.getText()).toEqual("Madrid, Spain"); 

來源:http://angular.github.io/protractor/#/api?view=ProtractorBy.prototype.repeater

這是在幕後發生了什麼。假設你在WebElement類上調用getText()。 element將傳遞給core.text.getElementText

Selenium(量角器)處理髮送的參數。

這是獲取內容的代碼,如果使用了WebElement。我不知道如果解析爲數組的promise是明確的thisArg會發生什麼。

explicitThisArg.getText()//the explicit thisArg is the object that the function is called from. 

    core.text.getElementText = function(element) { 
    var text = ''; 
    var isRecentFirefox = 
     (goog.userAgent.GECKO && goog.userAgent.VERSION >= '1.8'); 

    if (isRecentFirefox || goog.userAgent.WEBKIT || goog.userAgent.IE) { 
    text = core.text.getTextContent_(element, false); 
    } else { 
    if (element.textContent) { 
     text = element.textContent; 
    } else { 
     if (element.innerText) { 
     text = element.innerText; 
     } 
    } 
    } 

    text = core.text.normalizeNewlines_(text); 
    text = core.text.normalizeSpaces_(text); 

    return goog.string.trim(text); 
}; 
+0

你的代碼的工作,但是,對於gettext的文檔()指出,「獲得可見的(即不是由CSS隱藏)這個元素,包括子元素,沒有任何開頭或結尾空白的innerText」。所以我相信該行上的getText()也應該起作用。我在這裏發現了類似的問題https://github.com/angular/protractor/issues/1794,所以我認爲這可能是一個錯誤。 – Matt

+0

我應該注意到,你在一個承諾上調用getText,而這不是所使用的文檔。 –

+0

有道理,謝謝你鑽研這個和詳細的答案:) – Matt

相關問題