我是 Selenium 的新手,我认为 Selenium IDE 可以很好地工作,但是当我导出 Selenium IDE(2.5.0)测试用例到 Ruby Test :: Unit / WebDriver 测试。当我从 Selenium IDE 运行它时,我的测试工作正常,但在导出到 Ruby 之后,以下代码不起作用:
verify { assert_match /.*Welcome/, @driver.find_element(:xpath, "//title").text }
在Selenium IDE上,相同的测试定义为:
命令:verifyText
目标://title
价值:regexp:.*Welcome
它工作正常(附加屏幕截图)
以下是 Test :: Unit 错误消息:verify { assert_match /.*Welcome/, @driver.find_element(:xpath, "//title").text }
因此我猜RegEx
存在某种问题。 标题应以“欢迎”一词结尾。
jyrki@ubu:~/rails_projects/p3$ rake test test/selenium/employer_signup_ruby2_test.rb
Loaded suite /home/jyrki/.rvm/gems/ruby-2.1.0/bin/rake
Started
F
===============================================================================
Failure:
test_employer_signup_ruby2(EmployerSignupRuby2)
/home/jyrki/rails_projects/p3/test/selenium/employer_signup_ruby2_test.rb:17:in `teardown'
<[]> expected but was
<[#<Test::Unit::AssertionFailedError: <""> expected to be =~
</.*Welcome/>.>]>
diff:
+ [#<Test::Unit::AssertionFailedError: <""> expected to be =~
? [ ]
? </.*Welcome/>.>
===============================================================================
Finished in 12.747676835 seconds.
1 tests, 2 assertions, 1 failures, 0 errors, 0 pendings, 0 omissions, 0 notifications
100% passed
0.08 tests/s, 0.16 assertions/s
Run options: --seed 25544
# Running tests:
Finished tests in 0.003802s, 0.0000 tests/s, 0.0000 assertions/s.
0 tests, 0 assertions, 0 failures, 0 errors, 0 skips
以下代码与代码的一行不同:
require "json"
require "selenium-webdriver"
gem "test-unit" #?
require "test/unit"
class EmployerSignupRuby2 < Test::Unit::TestCase
def setup
@driver = Selenium::WebDriver.for :firefox
@base_url = "http://0.0.0.0:3000/"
@accept_next_alert = true
@driver.manage.timeouts.implicit_wait = 30
@verification_errors = []
end
def teardown
@driver.quit
assert_equal [], @verification_errors
end
def test_employer_signup_ruby2
@driver.get(@base_url + "/employer_signup")
@driver.find_element(:id, "employer_first_name").clear
@driver.find_element(:id, "employer_first_name").send_keys "tester"
@driver.find_element(:id, "employer_last_name").clear
@driver.find_element(:id, "employer_last_name").send_keys "login-tester"
@driver.find_element(:id, "employer_email").clear
@driver.find_element(:id, "employer_email").send_keys "tester012345678901@test.com"
@driver.find_element(:id, "employer_password").clear
@driver.find_element(:id, "employer_password").send_keys "foobar"
@driver.find_element(:id, "employer_password_confirmation").clear
@driver.find_element(:id, "employer_password_confirmation").send_keys "foobar"
@driver.find_element(:name, "commit").click
verify { assert_match /.*Welcome/, @driver.find_element(:xpath, "//title").text }
end
def element_present?(how, what)
@driver.find_element(how, what)
#${receiver}.find_element(how, what)
true
rescue Selenium::WebDriver::Error::NoSuchElementError
false
end
def alert_present?()
@driver.find_element(how, what)
#${receiver}.switch_to.alert
true
rescue Selenium::WebDriver::Error::NoAlertPresentError
false
end
def verify(&blk)
yield
rescue Test::Unit::AssertionFailedError => ex
@verification_errors << ex
end
def close_alert_and_get_its_text(how, what)
alert = @driver.switch_to().alert()
#alert = ${receiver}.switch_to().alert()
alert_text = alert.text
if (@accept_next_alert) then
alert.accept()
else
alert.dismiss()
end
alert_text
ensure
@accept_next_alert = true
end
end
实际上,当我用这个替换一行代码时,我可以让测试工作(某种程度):
verify { assert @driver.find_element(:xpath, '//title[contains(.,"Welcome")]') } #works!!
但是如果缺少“欢迎”文本(=测试失败),那么修复将无限期地保持循环,并且我有中断(Ctrl + C)测试。到目前为止,这是我发现的最好的解决方案。如果您知道更好的解决方案,请告诉我,谢谢:-)
我的Gemfile有以下rails / selenium / test版本:
gem 'rails', '4.0.2'
gem 'selenium-webdriver', '~> 2.41.0' #this is the latest version
gem 'test-unit', '2.5.5'
gem 'rake', '10.2.2'
-
$ bundle show json
The gem json has been deleted. It was installed at:
/home/jyrki/.rvm/rubies/ruby-2.1.0/lib/ruby/gems/2.1.0/gems/json-1.8.1
Ruby 2.1.0
此外,导出会创建不起作用的ruby代码:${receiver}.find_element(how, what)
;所以我需要将其更改为@driver.find_element(how, what)
才能使其正常工作。
此外,除了test-unit的require语句(第3-4行)之外,export还会创建一个gem语句:
gem "test-unit" #?
require "test/unit"
我可能会从Selenium Users Google小组以及
询问此问题顺便问一下,你知道Selenium IDE出口是否比RSpec更好地工作而不是Test :: Unit?