0

I am using Selenium-WebDriver for Ruby and I am trying to verify that text is present on a page. I have done many searches and tried many things and the best answer I have found is to use something like

  def check_page(textcheck)
  if verify {@driver.find_element(:id=>"body").text.include?(textcheck)}
    yield it_to "fail"
  else
    yield it_to "pass"
  end
  end

The expected outcome if the value of textcheck is present in the body would be pass and if the value of textcheck is not present in the body it would be fail. What is actually happening is if :id=>"body" is present then it is pass and if it is not present then it is fail regardless of .text.include?(textcheck)

If anyone could point me in the right direction for how to verify text is present on a page using Selenium-WebDriver in Ruby it would be greatly appreciated. I have found workarounds for certain cases where I can do

verify {@driver.find_element(:tag_name, 'h1').text!=(textcheck)}

but the element I am trying to verify I can't get to so easily. I looked into css locators and was very confused on how to simplify the tag so I could use it. Any help would be greatly appreciated. Thank you very much. If you require any more information from me please let me know and I will provide it as soon as possible.

I am using Ruby 1.93 with Selenium-WebDriver 2.25 testing in Firefox 14.0.1

1 Answer 1

4

I do it this way

@wait = Selenium::WebDriver::Wait.new(:timeout => 30)
begin
  @wait.until { @driver.find_element(:tag_name => "body").text.include?("your text")}
rescue  
  puts "Failure! text is not present on the page"
  #Or do one of the options below
  #raise
  #assert_match "true","false", "The text is not present"
end

UPDATE


Answer to your question in the comments section.

There are two kind of "waits", implicit wait and explicit wait. You can read more about it here. The reason your code failed was because you were searching by "id"=>"body" and not by "tag_name"=>"body". Usually all text is encompassed within the "body" HTML tags in your DOM.

8
  • Thank you for the response but whenever I do @driver.find_element(:tag_name=>"body").text.include?("texthere") it will only give me the resulte of @driver.find_element(:tag_name=>"body) completely ignoring the .text.include?("texthere") i could put the texthere as "total nonsense that doesn't exist!" and as long as the page has a tag named body it will give me the positive result. Oct 16, 2012 at 21:00
  • That should not happen, can you prove it with an example? Like send me code with a site and the text validation which works when it shouldn/
    – Amey
    Oct 16, 2012 at 22:43
  • Here is an example of what I am trying to do. The first verify should fail while the second should pass but both pass for me. Oct 17, 2012 at 17:44
  • @driver.get("http://www.campusexplorer.com") if verify{@driver.find_element(:id=>"body").text.include?("Some Complete Nonsense")} puts "Silly text has been found despite not being on the page!" else puts "Unable to find silly text." end Oct 17, 2012 at 17:51
  • Very sorry I'm having trouble formatting the code properly for the comments and couldn't figure out how to private message you. The code there is what I've been doing based on what I found online and it should return to me that it is unable to find but will always return that the text can be found so long as the element is there regardless of the text. Oct 17, 2012 at 17:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.