Selenium 2(WebDriver)中没有isTextPresent
使用WebDriver断言页面上是否存在某些文本的正确方法是什么?
答案 0 :(得分:14)
我通常会做以下几点:
assertEquals(driver.getPageSource().contains("sometext"), true);
assertTrue(driver.getPageSource().contains("sometext"));
答案 1 :(得分:6)
页面源包含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然后包含确实有性能权衡。您可能希望使用更具体的WebElement缩小搜索树的范围。
答案 2 :(得分:3)
我知道这有点旧,但我在这里找到了一个很好的答案: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
答案 3 :(得分:3)
或者,如果您想要实际检查 WebElement 的文本内容,您可以执行以下操作:
assertEquals(getMyWebElement().getText(), "Expected text");
答案 4 :(得分:2)
JUnit4中isTextPresent(Selenium IDE代码)的Selenium2 Java代码
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"));
}
答案 5 :(得分:2)
以下使用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]*$"));
答案 6 :(得分:1)
我写了以下方法:
public boolean isTextPresent(String text){
try{
boolean b = driver.getPageSource().contains(text);
return b;
}
catch(Exception e){
return false;
}
}
上面的方法如下所示:
assertTrue(isTextPresent("some text"));
它运作良好。
答案 7 :(得分:0)
使用firefox作为目标浏览器测试Ruby(初学者方法)中是否存在文本。
1)您当然需要下载并运行selenium server 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:
ruby test-it.rb