简体   繁体   中英

Cucumber+Ruby+Capybara+Selenium: How to make the 'visit' method wait for dynamic content

Here is the issue that has been nagging for weeks and all solutions found online do not seem to work... ie. wait for ajax, etc...

here is versions of gems:

  • capybara (2.10.1, 2.7.1)
  • selenium-webdriver (3.0.1, 3.0.0)
  • rspec (3.5.0)

running ruby 2.2.5 ruby 2.2.5p319 (2016-04-26 revision 54774) [x64-mingw32]

in the env.rb

Capybara.register_driver :selenium do | app |
browser = (ENV['browser'] || 'firefox').to_sym
Capybara::Driver::Selenium.new(app, :browser => browser.to_sym, :resynchronize => true)
Capybara.default_max_wait_time = 5
end

Here is my dynamicpage.feature

Given I visit page X
Then placeholder text appears
And the placeholder text is replaced by the content provided by the json service 

and the step.rb

When(/^I visit page X$/) do
visit('mysite.com/productx/')
end

When(/^placeholder text appears$/) do
  expect(page).to have_css(".text-replacer-pending")
end

Then(/^the placeholder text is replaced by the content provided by the json service$/) do
    expect(page).to have_css(".text-replacer-done")
end

the webpage in question, which I cannot add it here as it is not publicly accessible, contains the following on page load:

1- <span class="text-replacer-pending">Placeholder Text</span>

after a call to an external service (which provides the Json data), the same span class gets refreshed/updated to the following;

2- <span class="text-replacer-done">Correct Data</span>

The problem I have with the "visit" method in capybara + selenium is that as soon as it visits the page, it thinks everything loaded and freezes the browser, and it never lets the service be called to dynamically update the content. I tried the following solutions but without success:

  1. Capybara.default_max_wait_time = 5
  2. Capybara::Driver::Selenium.new(app, :browser => browser.to_sym, :resynchronize => true)
  3. add sleep 5 after the visit method
  4. wait for ajax solution from several websites, etc...
  5. adding after hooks etc...

I am at a complete loss why "visit" can't wait or at least provide a simple solution to an issue i am sure is very common. I am aware of the capybara methods that wait and those that don't wait such as 'visit' but the issue is;

  1. there is no content that goes from hidden to displayed
  2. there is there is no user interaction either, just the content is getting updated.

also unsure if this is a capybara issue or a selenium or both.

Anyhow have insight on any solutions? i am fairly new to ruby and cucumber so specifically what code goes in what file/folder would be much appreciated.

Mel

Restore wait_until method (add it to your spec_helpers.rb )

def wait_until(timeout = DEFAULT_WAIT_TIME)
  Timeout.timeout(timeout) do
    sleep(0.1) until value = yield
    value
  end
end

And then:

# time in seconds
wait_until(20) { has_no_css?('.text-replacer-pending') }
expect(page).to have_css(".text-replacer-done")

@maxple and @nattf0dd Just to close the loop on our issue here...

After looking at this problem from a different angle, we finally found out Cucumber/Capybara/ is not a problem at all :-)

The issue we are having lies with the browser Firefox driver (SSL related), since we have no issues when running the same test with the Chrome driver.

I do appreciate the replies and suggestions and will keep those in mind for future. thanks again!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM