khr*_*ris 46 validation selenium assert webdriver
我正在使用Selenium WebDriver,如何检查页面中是否存在某些文本?也许有人推荐我有用的资源,我可以阅读它.谢谢
Pet*_*ček 45
使用XPath,并不难.只需搜索包含给定文本的所有元素:
List<WebElement> list = driver.findElements(By.xpath("//*[contains(text(),'" + text + "')]"));
Assert.assertTrue("Text not found!", list.size() > 0);
Run Code Online (Sandbox Code Playgroud)
该官方文档也不是很支持的,像这样的任务,但它是基本的工具仍然.
该JavaDoc中也大,但它需要一些时间通过一切有用无用,并拿到.
要学习XPath,只需按照互联网.该规范也是一个令人惊讶的好读.
编辑:
或者,如果您不希望隐式等待使上述代码等待文本出现,您可以采取以下方式:
String bodyText = driver.findElement(By.tagName("body")).getText();
Assert.assertTrue("Text not found!", bodyText.contains(text));
Run Code Online (Sandbox Code Playgroud)
Roh*_*are 21
这将帮助您检查网页中是否存在所需文本.
driver.getPageSource().contains("Text which you looking for");
Run Code Online (Sandbox Code Playgroud)
JCa*_*ter 14
您可以检索整个页面的正文文本,如下所示:
bodyText = self.driver.find_element_by_tag_name('body').text
Run Code Online (Sandbox Code Playgroud)
然后使用断言来检查它:
self.assertTrue("the text you want to check for" in bodyText)
Run Code Online (Sandbox Code Playgroud)
当然,您可以是特定的并检索特定DOM元素的文本,然后检查它而不是检索整个页面.
Selenium 2 webdriver中没有verifyTextPresent,因此您需要检查页面源中的文本.看下面的一些实际例子.
在Python驱动程序中,您可以编写以下函数:
def is_text_present(self, text):
return str(text) in self.driver.page_source
Run Code Online (Sandbox Code Playgroud)
然后用它作为:
try: self.is_text_present("Some text.")
except AssertionError as e: self.verificationErrors.append(str(e))
Run Code Online (Sandbox Code Playgroud)
要使用正则表达式,请尝试:
def is_regex_text_present(self, text = "(?i)Example|Lorem|ipsum"):
self.assertRegex(self.driver.page_source, text)
return True
Run Code Online (Sandbox Code Playgroud)
请参阅:FooTest.pyfile以获取完整示例.
或者查看以下几个其他选择:
self.assertRegexpMatches(self.driver.find_element_by_xpath("html/body/div[1]/div[2]/div/div[1]/label").text, r"^[\s\S]*Weather[\s\S]*$")
assert "Weather" in self.driver.find_element_by_css_selector("div.classname1.classname2>div.clearfix>label").text
Run Code Online (Sandbox Code Playgroud)
来源:使用Selenium Python检查(断言)文本是否存在的另一种方法
在Java中有以下功能:
public void verifyTextPresent(String value)
{
driver.PageSource.Contains(value);
}
Run Code Online (Sandbox Code Playgroud)
用法是:
try
{
Assert.IsTrue(verifyTextPresent("Selenium Wiki"));
Console.WriteLine("Selenium Wiki test is present on the home page");
}
catch (Exception)
{
Console.WriteLine("Selenium Wiki test is not present on the home page");
}
Run Code Online (Sandbox Code Playgroud)
来源:在Selenium 2 Webdriver中使用verifyTextPresent
对于Behat,您可以使用Mink扩展名.它具有以下定义的方法MinkContext.php:
/**
* Checks, that page doesn't contain text matching specified pattern
* Example: Then I should see text matching "Bruce Wayne, the vigilante"
* Example: And I should not see "Bruce Wayne, the vigilante"
*
* @Then /^(?:|I )should not see text matching (?P<pattern>"(?:[^"]|\\")*")$/
*/
public function assertPageNotMatchesText($pattern)
{
$this->assertSession()->pageTextNotMatches($this->fixStepArgument($pattern));
}
/**
* Checks, that HTML response contains specified string
* Example: Then the response should contain "Batman is the hero Gotham deserves."
* Example: And the response should contain "Batman is the hero Gotham deserves."
*
* @Then /^the response should contain "(?P<text>(?:[^"]|\\")*)"$/
*/
public function assertResponseContains($text)
{
$this->assertSession()->responseContains($this->fixStepArgument($text));
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
227130 次 |
| 最近记录: |