我的HTML代码如下:
<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>
验证我使用以下代码的页眉文本:
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());
收到错误:
java.lang.AssertionError: expected [Welcome to the Automation Software Testing] but found [welcome to the]
答案 0 :(得分: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);
答案 1 :(得分: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);
答案 2 :(得分:0)
使用findElements()
代替并在验证前将结果连接成一个字符串。您可能希望对空格进行标准化(即修剪前导/尾随空格,将换行符转换为空格以及将多个空格转换为单个空格)并执行比较不区分大小写。
答案 3 :(得分: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());