Selenium webdriver ruby assertTextPresent equivalent
I cannot figure out what the assertTextPresent equivalent for seleniums webd开发者_JS百科river is. I found several answers for java, but none for ruby. Does anybody have any ideas?
Text assertions are not part of WebDriver, but you can do:
assert driver.find_element(:tag_name, "body").text.include?(str)
selenium-webdriver does not come with an inbuilt assertion library, you need to have an additional one to cater to you assertion needs.
Now coming to language which is ruby then the best one would be to use rspec.
How do you use that :
1) Install rspec of put it in your Gemfile and do a bundle install
2) require 'rspec' in your framework
3) use rspec-expectations
expect(actual-text).to include(expected-text)
Here is full single script example
require 'selenium-webdriver'
require 'rspec'
include RSpec::Matchers
def assert_text_present(expected_text)
expect(driver.find_element(:tag_name=>'body').text.include(expected_text)).to be true
end
driver = Selenium::WebDriver.for :chrome
driver.get("https://rubygems.org/gems/testnow")
assert_text_present("Kaushal")
Additionally you can define this method def assert_text_present
in a utility or a helper file of your framework and use it repeatedly when required.
Note: If you put this method in a framework, you can use the include matcher directly (expect(driver.find_element(:tag_name=>'body').text).to include(expected_text)
)
Hope it helps!!
I recommend rspec-expectations
https://github.com/rspec/rspec-expectations
It is realy comprehensive assertion "library".
In this case you could use following matcher:
expect(actual_text).to eq(expected_text)
精彩评论