如何存储1页的文本并验证该文本是否出现在另一页上?
答案 0 :(得分:2)
将第1页中的文本设置为变量,然后检查第2页上是否存在变量文本
这是Java中的示例代码
String expectedText = firstPage.textElement1.getText();
// Do something to get into second page
Assert.assertTrue(secondPage.isTextExist(expectedText));
答案 1 :(得分:0)
据我所知,webdriver没有通用的'findTextOnPage'-Methode。 我使用辅助方法,其思路如下:
WebDriver driver = new WebDriver(); //insert your favored webDriver
driver.get(urlOfPage1); //get Page1
//get Text...just you know your Webpage
String expectedText = YourMethodeToGetTheText();
driver.get(urlOfPage2); //change to Page2
//and now the 'hard' part:
WebElement body = driver.findElement(By.tagName("body"));
String bodyText = body.getText(); //get whole text of the body-Element
Assert.assertTrue(countTextPresentInString(expectedText, bodyText)>0);
并且Helper方法写道:
public static int countTextPresentInString(String textToCheck, String text) {
int count = 0;
// search textToCheck in text
while (text.contains(textToCheck)) {
// if found, increment count
count++;
// proceed search just in that part, that hasn't been searched jet
text = text.substring(text.indexOf(textToCheck) + textToCheck.length());
}
return count;
}
我希望这有帮助,如果你从第1页获取文本时遇到问题,请告诉我。