简体   繁体   中英

Capybara Selenium webdriver Transactional Rollbacks work arounds

I'm writing a couple of Feature Specs for an app and using the default Selenium webdriver that comes with Capybara. This is the spec I have written.

DatabaseCleaner.cleaning do
      find(:css,'.dropdown-toggle').click
      click_on "Locations"  
      find(:css, "#location-8-upgradesub-60").click 
      value1 = find(:css, "#location-8-review-subscription").text
      value1.should be == '(2) Reviews (Paid)'    
end

I'm facing 2 issues with this snippet:

1) Capybara isn't waiting for the XHR to get over and is coming out of the test before that. It works if I give a sleep condition for about 10 sec.

UPDATE Solved 1) by setting Capybara.default_wait_time = 15 and writing a helper to make sure jQuery isn't active. page.evaluate_script('jQuery.active').zero?

2) I'm not able to rollback the DB transaction that takes place when selenim simulates the test. I see an INSERT and COMMIT in the test.log but no ROLLBACK because of which I need to keep changing my specs every time I run the test. If I use, DatabaseCleaner.strategy = :truncation , my entire DB gets wiped out and that is not something I want.

I've done some extensive googling on this issue and haven't been able to find an efficient work around. I've tried using the same transactional thread too, for the test server. Haven't had fruitful results with too! Any heads up or help would be greatly appreciated. Thanks in advance!

UPDATE

I followed this link https://relishapp.com/rspec/rspec-rails/docs/transactions and put my spec inside a before(:each) block and stored the value in an @value1 instance variable to compare it with the desired value within the it block. I haven't had any luck with that too.

before(:each) do
  find(:css,'.dropdown-toggle').click
  click_on "Locations"
  find(:css, "#location-8-upgradesub-60").click
  wait_for_ajax #Helper method to wait for ajax call to get over 
  find(:css, "#location-8-review-subscription").should be_visible
  @value1 = find(:css, "#location-8-review-subscription").text
end

it "should open the dropdown, find Location and open it's modal", js:true do
    @value1.should be == '(2) Reviews (Paid)'
end

With 1), I think have_content or have_selector will work. These methods will wait for some seconds before checking the content/selector exist. You could config this time via spec_helper.rb. You could put have_content/have_selector BEFORE your find(..).click to make sure it is exist before next tests.

Finally found a work around. I added this code snippet in spec_helper.rb . Not using Database Cleaner anymore.

Reference : http://www.opinionatedprogrammer.com/2011/02/capybara-and-selenium-with-rspec-and-rails-3/#comment-441060846 . The entire comment thread is pretty useful.

ActiveRecord::ConnectionAdapters::ConnectionPool.class_eval do
  def current_connection_id
    # Thread.current.object_id
    Thread.main.object_id
  end
end

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