简体   繁体   English

Capybara Selenium Webdriver事务性回滚解决方法

[英]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. 我正在为应用程序编写几个功能规范,并使用Capybara随附的默认Selenium Webdriver。 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: 此代码段面临2个问题:

1) Capybara isn't waiting for the XHR to get over and is coming out of the test before that. 1)Capybara不在等待XHR过去并且在此之前退出测试。 It works if I give a sleep condition for about 10 sec. 如果我保持睡眠状态约10秒钟,它会起作用。

UPDATE Solved 1) by setting Capybara.default_wait_time = 15 and writing a helper to make sure jQuery isn't active. 更新通过设置Capybara.default_wait_time = 15解决了1)问题,并编写了一个帮助程序来确保jQuery不处于活动状态。 page.evaluate_script('jQuery.active').zero?

2) I'm not able to rollback the DB transaction that takes place when selenim simulates the test. 2)我无法回退selenim模拟测试时发生的数据库事务。 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. 我在test.log看到一个INSERTCOMMIT ,但是没有ROLLBACK ,因此每次运行测试时我都需要不断更改规格。 If I use, DatabaseCleaner.strategy = :truncation , my entire DB gets wiped out and that is not something I want. 如果我使用DatabaseCleaner.strategy = :truncation ,我的整个数据库就会被清除掉,那不是我想要的。

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. 我点击了此链接https://relishapp.com/rspec/rspec-rails/docs/transactions并将我的规范放入before(:each)块中,并将该值存储在@value1实例变量中,以将其与所需值进行比较在it块内。 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. 对于1),我认为have_content或have_selector将起作用。 These methods will wait for some seconds before checking the content/selector exist. 这些方法将等待几秒钟,然后检查内容/选择器是否存在。 You could config this time via spec_helper.rb. 您这次可以通过spec_helper.rb进行配置。 You could put have_content/have_selector BEFORE your find(..).click to make sure it is exist before next tests. 您可以在您的find(..)。click之前放置have_content / have_selector,以确保它在下一次测试之前存在。

Finally found a work around. 终于找到了解决方法。 I added this code snippet in spec_helper.rb . 我在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 . 参考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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Capybara / Selenium Webdriver + DataTables不能很好地播放 - Capybara/Selenium Webdriver + DataTables not playing nicely 如何访问 cookies(Capybara/Selenium Chrome Webdriver) - How to access cookies (Capybara/Selenium Chrome Webdriver) Capybara / selenium-webdriver:将值设置为input [type =“ number”]无效 - Capybara/selenium-webdriver: set value to input[type=“number”] does not work 黄瓜/水豚/ Selenium-Webdriver错误? - Cucumber/Capybara/Selenium-Webdriver error? 使用Selenium WebDriver进行水豚测试失败 - Capybara tests fail using selenium webdriver 在Capybara集成测试(和Selenium Webdriver)过程中,设计用户创建失败 - Devise user creation fails during Capybara integration testing (and Selenium webdriver) 使用Capybara和selenium-webdriver打开新的私人窗口 - Open new private window with Capybara and selenium-webdriver 使用Capybara / Selenium Webdriver测试HTML5文件上传-Ruby - Testing HTML5 File Upload with Capybara/Selenium Webdriver - Ruby Capybara错误:Selenium :: WebDriver :: Error :: ElementNotVisibleError:元素不可见 - Capybara error: Selenium::WebDriver::Error::ElementNotVisibleError: element not visible 如何使用Rails 3 Rspec和Capybara访问Selenium 2的默认webdriver? - How can I access the default webdriver for Selenium 2 with Rails 3 Rspec and Capybara?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM