Ruby中Selenium::Webdriver::Element对象的断言
我对使用Selenium Webdriver(和Cucumber)非常陌生,但这是我目前非常感兴趣的职位所需要的技能,所以我真的在努力掌握它们的工作原理 以下是我目前正在尝试测试的功能部分(结合使用Selenium和Cucumber): 以下是相关步骤:Ruby中Selenium::Webdriver::Element对象的断言,ruby,selenium,selenium-webdriver,cucumber,Ruby,Selenium,Selenium Webdriver,Cucumber,我对使用Selenium Webdriver(和Cucumber)非常陌生,但这是我目前非常感兴趣的职位所需要的技能,所以我真的在努力掌握它们的工作原理 以下是我目前正在尝试测试的功能部分(结合使用Selenium和Cucumber): 以下是相关步骤: When(/^I click on the link "(.*?)"$/) do |month| step %[I click on link having text "#{month}"] end Then(/^I should see
When(/^I click on the link "(.*?)"$/) do |month|
step %[I click on link having text "#{month}"]
end
Then(/^I should see a small blurb for each shoe$/) do
blurbs = $driver.find_elements(:class_name, 'shoe_description')
if blurbs
blurbs.each do |blurb|
# Need to assert that blurb elements exist / have text
end
end
end
第二步是我似乎找不到明确的答案。如果我加入binding.pry,我可以看到我有所有需要迭代的对象(blurb是一个webdriver对象,当我调用blurb.text时,它显示了我想要断言的确切文本存在)
这似乎应该很简单。一个简单的解决方案是,如果宣传栏中没有文本,那么它就会失败。这将使你的简介循环成以下内容:
blurbs.each do |blurb|
fail 'blurb contains no text' if blurb.text == ''
end
如果文本为空,则该步骤和场景将失败。您还可以将其扩展为通过将文本与正确的值进行比较来检查文本是否与预期文本匹配。一个简单的断言就可以做到这一点,您可以验证blurb.text是否为空,如:
Then(/^I should see a small blurb for each shoe$/) do
blurbs = $driver.find_elements(:class_name, 'shoe_description')
if blurbs
blurbs.each do |blurb|
# Need to assert that blurb elements exist / have text
blurb.text.should_not eq ""
end
end
end.
希望这有帮助:)
Then(/^I should see a small blurb for each shoe$/) do
blurbs = $driver.find_elements(:class_name, 'shoe_description')
if blurbs
blurbs.each do |blurb|
# Need to assert that blurb elements exist / have text
blurb.text.should_not eq ""
end
end
end.