2016-11-10 77 views
-3

我的HTML代碼如下:如何使用單個xpath驗證span標記文本?

<div class="row"> 
<div class="col-lg-7"> 
<h1 class="h1_home"> 
<span>Welcome to the</span> 
<br/> 
<span>Automation Software Testing</span> 
</h1> 
<p class="col-xs-9 col-lg-12"> 
</div> 
</div> 
  1. 驗證頁面頁眉文字我用波紋管代碼:

    String expected_txt = "Welcome to the Automation Software Testing"; 
    WebElement header_txt_elm=driver.findElement(By.xpath(".//h1[@class='h1_home']//span")); 
    String actual_headertxt = header_txt_elm.getText().toString(); 
    Assert.assertEquals(actual_headertxt.toLowerCase(), expected_txt.toLowerCase()); 
    
  2. 收到錯誤:

    java.lang.AssertionError: expected [Welcome to the Automation Software Testing] but found [welcome to the] 
    
+1

僅供參考......你並不需要在'actual_headertxt'的'的ToString()'。 '.getText()'已經返回一個字符串。 – JeffC

回答

0

使用findElements(),並在驗證之前將結果連接成一個字符串。您可能需要對空格進行規範化(即修剪前導空格/尾部空格,將換行符轉換爲空格,將多個空格轉換爲單個空格),並進行不區分大小寫的比較。

0

您應該能夠從<h1>中提取所有文本。

String expected_txt = "Welcome to the Automation Software Testing"; 
WebElement header_txt_elm = driver.findElement(By.xpath("//h1[@class='h1_home']")); 
// WebElement header_txt_elm = driver.findElement(By.cssSelector("h1.h1_home")); // CSS selector version 

String actual_headertxt = header_txt_elm.getText(); 
Assert.assertEquals(actual_headertxt.toLowerCase(), expected_txt.toLowerCase()); 
1
String expected_txt = "Welcome to the Automation Software Testing"; 
WebElement header_txt_elm = driver.findElement(By.xpath("//*[text()='Welcome to the']")); 
WebElement header_txt_elm2 = driver.findElement(By.xpath("//*[text()='Automation Software Testing']")); 
String actual_txt1=header_txt_elm.getText(); 
String actual_txt2=header_txt_elm2.getText(); 
String actual_txt=actual_txt1+actual_txt2; 
Assert.assertEquals(actual_headertxt, expected_txt); 
+0

示例不夠!這將是很好,如果有效的解釋給出!謝謝!! –

+0

我從Web元素中獲取2個文本值。比我保存的字符串值,我串聯成一個字符串的字符串。那麼我將這些值與實際的文本進行比較。 –

1
String expected_value = "Welcome to the Automation Software Testing"; 
WebElement header_value_elm = driver.findElement(By.xpath("//*[text()='Welcome to the']")); 
WebElement header_value_elm2 = driver.findElement(By.xpath("//*[text()='Automation Software Testing']")); 
String actual_txt1=header_txt_elm.getText(); 
String actual_txt2=header_txt_elm2.getText(); 
String actual_txt=actual_txt1+actual_txt2;`enter code here` 
Assert.assertEquals(actual_headertxt, expected_txt); 
相關問題