teampoltergeist / poltergeist

A PhantomJS driver for Capybara

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Weird behavior of page.evaluate_script('window.location.reload()')

srghma opened this issue · comments

Poltergeist can't reload page from rspec

expect(page).to have_current_path checkout_path(:complete)
# but if I do pry.binding here and enter the same manually - it will work
page.evaluate_script('window.location.reload()')
expect(page).to have_current_path cart_path # fail, expected "/checkout/complete" to equal "/cart"

Same with pry

[1] pry(#<RSpec::ExampleGroups::CheckoutPage::Confirm::WhenSubmitting>)> current_url
=> "http://127.0.0.1:38015/checkout/complete"
[2] pry(#<RSpec::ExampleGroups::CheckoutPage::Confirm::WhenSubmitting>)> page.evaluate_script("window.location.reload()")
=> nil
[3] pry(#<RSpec::ExampleGroups::CheckoutPage::Confirm::WhenSubmitting>)> current_url
=> "http://127.0.0.1:38015/cart"

Also I have tried sleep 1, visit current_url, and this working fine in selenium

config, code

So a quick run through your test shows that putting a short sleep before reloading the page makes it pass. I haven't looked any further but I guess you're running some JS on the checkout complete page that changes state back on the server? The pages could match (page loaded) before that JS runs/completes and then reloading could cancel that request.

On a separate note you have a ton of places where you're using current_path with the eq matcher which is bad practice if you want stable tests, and stubbing the current order in feature tests is also a bad idea since then your tests are no longer full stack (which is the whole purpose of feature tests)

On further investigation the test.log shows that without a sleep before reload you basically end up with two GET requests to /checkout/complete occurring simultaneously. To make your test pass you should check for expected content that would be visible on the page rather than checking the current path, however the test does show your app has an issue in that it is processing and sending emails for two different orders if a reload occurs while it is still processing the first order.

Wow, thanks)

  def reload_page
    sleep 0.1 if Capybara.current_driver == :poltergeist
    page.evaluate_script('window.location.reload()')
  end

And I had changed current_path eq, and I am not stabbing current_order actually, it just the name ))

  def stub_current_order_with(order)
    # selenium cant set cookie without page, poltergeist - vice versa
    visit('/') if Capybara.current_driver == :selenium_chrome
    create_cookie(CurrentOrder::KEY, order.id)
  end