我尝试做一些简单的脚本
steps.rb
档案
Given(/^I am on the YouTube home page$/) do
visit 'http://www.youtube.com'
end
When(/^I search for "(.*?)"$/) do
|search_term|
fill_in 'search_query', :with => search_term
click_on 'search-btn'
end
Then(/^videos of large rodents are returned$/) do
expect(page).to have_content 'Largest rodents'
end
和youtube.feature:
Feature: Search for Videos on YouTube
Scenario: Search for Videos of Large Rodents
Given I am on the YouTube home page
When I search for "capybara"
Then videos of large rodents are returned
of course gemfile and env.rb
after run i got:
RSpec::Expectations::ExpectationNotMetError: expected to find text "Largest rodents" in "Remind me later Review A privacy reminder from YouTube, a Google company PL (...)
./features/step_definitions/steps.rb:10:in `/^videos of large rodents are returned$/'
./features/youtube_search.feature:5:in `Then videos of large rodents are returned'
有什么不对?
答案 0 :(得分:0)
我建议您在执行预期之前在代码中添加sleep
。这样您就可以手动浏览页面并确认文本是否存在。
如果它存在,则可能是默认等待时间太短,并且文本可能需要一些时间才能在页面中显示。如果是这样,我建议你这样做:
expect(page).to have_content("Largest rodents", wait: 15)
在这种情况下,期望将允许最大等待时间为15秒,然后再触发期望未满足消息。