2016-05-24 54 views
0

我正在測試一個窗口小部件,爲了獲取行車路線,我必須輸入from(van)和To(naar)字段。輸入和輸出字段爲javascript並在輸入3個字符後自動完成。在選擇了一個位置之後,我想聲明所選位置確實在「收件人」或「發件人」字段中。但是,如果我嘗試用assertEquals腳本來聲明它,它告訴我該字段中沒有數據。以下是我用來測試它的一些頁面代碼和代碼。無法斷言我剛剛填充的字段

enter image description here

這是HTML代碼輸入字段:

<input id="van" class="textfield" type="text" data-reactid=".0.1.1.0.1.0.0.1.0" value="" tabindex="0" required="" placeholder="Place" autocomplete="off" aria-label="Van"></input> 

這是我的文本輸入到輸入框,然後選擇第二個可用的選項:

driver.findElement (By.id( 「貨車」))明確(); sendKeys(Ams);

driver.findElement(By.id(「van」))。sendKeys(Ams);

現在我展示含有阿姆斯特丹幾個選項,我選擇第二個選項:

driver.findElement(By.xpath(「// DIV [@類= '自動完成,下拉']/ul/li 2「))。click();

如果我看着屏幕,現在讓我看到下面

enter image description here

我現在想斷言從字段包含什麼,我只是選擇(阿姆斯特丹,北荷蘭省)。我用下面這樣做:

assertEquals(「Amsterdam,Noord-Holland」,driver.findElement(By.id(「van」)).getText());

在此然而結果:

org.junit.ComparisonFailure:預期:< [阿姆斯特丹,北荷蘭省]>卻被:< []>

,如果我檢查它顯示給我的元素:

<input id="van" class="textfield" type="text" data-reactid=".0.1.1.0.1.0.0.1.0" value="" tabindex="0" required="" placeholder="Place" autocomplete="off" aria-label="Van"></input> 

所以看起來ComparisonFailure是corr但我找不到爲什麼我不能斷言我在屏幕上看到的東西。

我使用Java,Selenium和Eclipse進行測試。

+0

嘗試: Webelement A = driver.findElement(通過。的xpath( 「// DIV [@類= '自動完成-下拉']/UL/LI2」)); String b = a.gettext(); assertEquals(「Amsterdam,Noord-Holland」,b); –

回答

1

你好,請做如下圖所示

在斷言做這樣

driver.findElement(By.id("van")).getAttribute("value"); 

// please note when you want 
to check the value entered by you inside the input box then do not use getText() 
as it returns inner visible html of the tag 
So to get the value there is a hidden attribute for every input box known as "value" 
which keeps value entered by you. 

希望這有助於你感謝

+0

謝謝,我沒有意識到任何隱藏的屬性,因爲屬性值是空的,我沒有想到要檢查它。這現在就像一個魅力 – Hugo

+0

你的歡迎,我很高興它幫助你 –

相關問題