isTextPresent
Selenium 2 (WebDriver) 中没有
使用 WebDriver 在页面上断言某些文本存在的正确方法是什么?
isTextPresent
Selenium 2 (WebDriver) 中没有
使用 WebDriver 在页面上断言某些文本存在的正确方法是什么?
我通常会按照以下方式做一些事情:
assertEquals(driver.getPageSource().contains("sometext"), true);
assertTrue(driver.getPageSource().contains("sometext"));
页面源包含可能会破坏您的搜索文本并导致误报的 HTML 标记。我发现这个解决方案很像 Selenium RC 的 isTextPresent API。
WebDriver driver = new FirefoxDriver(); //or some other driver
driver.findElement(By.tagName("body")).getText().contains("Some text to search")
做 getText 然后 contains 确实有性能权衡。您可能希望通过使用更具体的 WebElement 来缩小搜索树的范围。
我知道这有点旧,但我在这里找到了一个很好的答案:Selenium 2.0 Web Driver: implementation of isTextPresent
在 Python 中,这看起来像:
def is_text_present(self, text):
try: el = self.driver.find_element_by_tag_name("body")
except NoSuchElementException, e: return False
return text in el.text
或者,如果您想实际检查WebElement的文本内容,您可以执行以下操作:
assertEquals(getMyWebElement().getText(), "Expected text");
JUnit4 中 isTextPresent 的 Selenium2 Java 代码(Selenium IDE 代码)
public boolean isTextPresent(String str)
{
WebElement bodyElement = driver.findElement(By.tagName("body"));
return bodyElement.getText().contains(str);
}
@Test
public void testText() throws Exception {
assertTrue(isTextPresent("Some Text to search"));
}
在 WebDriver 中使用 Java 的以下代码应该可以工作:
assertTrue(driver.getPageSource().contains("Welcome Ripon Al Wasim"));
assertTrue(driver.findElement(By.id("widget_205_after_login")).getText().matches("^[\\s\\S]*Welcome ripon[\\s\\S]*$"));
我写了以下方法:
public boolean isTextPresent(String text){
try{
boolean b = driver.getPageSource().contains(text);
return b;
}
catch(Exception e){
return false;
}
}
上述方法调用如下:
assertTrue(isTextPresent("some text"));
它运行良好。
使用 firefox 作为目标浏览器测试 Ruby 中是否存在文本(一种初学者方法)。
1)您当然需要下载并运行 selenium 服务器 jar 文件,例如:
java - jar C:\Users\wmj\Downloads\selenium-server-standalone-2.25.0.jar
2)您需要安装 ruby,并在其 bin 文件夹中运行命令以安装其他 gem:
gem install selenium-webdriver
gem install test-unit
3)创建一个文件 test-it.rb 包含:
require "selenium-webdriver"
require "test/unit"
class TestIt < Test::Unit::TestCase
def setup
@driver = Selenium::WebDriver.for :firefox
@base_url = "http://www.yoursitehere.com"
@driver.manage.timeouts.implicit_wait = 30
@verification_errors = []
@wait = Selenium::WebDriver::Wait.new :timeout => 10
end
def teardown
@driver.quit
assert_equal [], @verification_errors
end
def element_present?(how, what)
@driver.find_element(how, what)
true
rescue Selenium::WebDriver::Error::NoSuchElementError
false
end
def verify(&blk)
yield
rescue Test::Unit::AssertionFailedError => ex
@verification_errors << ex
end
def test_simple
@driver.get(@base_url + "/")
# simulate a click on a span that is contained in a "a href" link
@driver.find_element(:css, "#linkLogin > span").click
# we clear username textbox
@driver.find_element(:id, "UserName").clear
# we enter username
@driver.find_element(:id, "UserName").send_keys "bozo"
# we clear password
@driver.find_element(:id, "Password").clear
# we enter password
@driver.find_element(:id, "Password").send_keys "123456"
# we click on a button where its css is named as "btn"
@driver.find_element(:css, "input.btn").click
# you can wait for page to load, to check if text "My account" is present in body tag
assert_nothing_raised do
@wait.until { @driver.find_element(:tag_name=>"body").text.include? "My account" }
end
# or you can use direct assertion to check if text "My account" is present in body tag
assert(@driver.find_element(:tag_name => "body").text.include?("My account"),"My account text check!")
@driver.find_element(:css, "input.btn").click
end
end
4)运行红宝石:
ruby test-it.rb