2016-07-22 91 views
0

我正在爲React前端應用程序編寫測試。 其中一項測試必須檢查從服務器返回值的方式。 也就是說,我有以下跨度:使用webdriverio從React包裹的跨度中獲取文本

<span id='result'> 
    <!-- our version of react wraps the resulting text by default --> 
    <span data-reactid>the actual result</span> 
</span> 


// test.js 
getResult() { 
    let a = browser.getText('#result span'); 
    console.log('a:', a); 
} // unfortunately, getting it this way will mean having to rewrite the test once we upgrade to a newer version of React (after 15 is does not do the wrapping anymore) 

所以,基本上..我怎麼能以這樣的方式,要麼所產生的反應跨度有一個id或類......或任何其他獲取文本值如何改變dom結構並不意味着必須重寫測試?

+0

你有沒有試過'browser.getText('#result')'? –

+0

是的..但因爲反應總是圍繞東西包裝一個,我必須使用browser.getText('#result span'); – JeanAlesi

回答

1

我不知道如何使用webDriver API來做到這一點,但你可以使用一些JavaScript來獲得價值。事情是這樣的:

var id = 'result'; 

client.execute(function(a) { 
    return document.getElementById(a).textContent; 
}, id).then(function(ret) { 
    console.log(ret.value); // outputs: the actual result 
}); 

中的textContent,即使你選擇目標的父跨度,它會在輸出文本時忽略了孩子<span>標籤。

+0

優秀!使用webdriver,我只需要獲取父級,然後在其上執行此操作。謝謝。 – JeanAlesi