![](/img/trans.png)
[英]Capybara/Selenium Webdriver + DataTables not playing nicely
[英]Capybara Selenium webdriver Transactional Rollbacks work arounds
我正在为应用程序编写几个功能规范,并使用Capybara随附的默认Selenium Webdriver。 这是我写的规范。
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
此代码段面临2个问题:
1)Capybara不在等待XHR过去并且在此之前退出测试。 如果我保持睡眠状态约10秒钟,它会起作用。
更新通过设置Capybara.default_wait_time = 15解决了1)问题,并编写了一个帮助程序来确保jQuery不处于活动状态。 page.evaluate_script('jQuery.active').zero?
2)我无法回退selenim模拟测试时发生的数据库事务。 我在test.log
看到一个INSERT
和COMMIT
,但是没有ROLLBACK
,因此每次运行测试时我都需要不断更改规格。 如果我使用DatabaseCleaner.strategy = :truncation
,我的整个数据库就会被清除掉,那不是我想要的。
我已经在这个问题上进行了广泛的搜索,但未能找到有效的解决方案。 我也尝试过将相同的事务性线程用于测试服务器。 也没有取得丰硕的成果! 任何提示或帮助将不胜感激。 提前致谢!
更新
我点击了此链接https://relishapp.com/rspec/rspec-rails/docs/transactions并将我的规范放入before(:each)
块中,并将该值存储在@value1
实例变量中,以将其与所需值进行比较在it
块内。 我也没有运气。
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
对于1),我认为have_content或have_selector将起作用。 这些方法将等待几秒钟,然后检查内容/选择器是否存在。 您这次可以通过spec_helper.rb进行配置。 您可以在您的find(..)。click之前放置have_content / have_selector,以确保它在下一次测试之前存在。
终于找到了解决方法。 我在spec_helper.rb
添加了此代码段。 不再使用数据库清理程序。
参考 : http : //www.opinionatedprogrammer.com/2011/02/capybara-and-selenium-with-rspec-and-rails-3/#comment-441060846 。 整个注释线程非常有用。
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.