Running capybara in the controller to check for several things, but can only run once

Yes, I know it shouldn't be, but I just need to run a little Capybara script in the controller.

The problem is that it opens the browser and that’s all, however I want to finish testing or run it in isolation.

So, for example, if I ran something like:

Capybara.current_driver = :selenium Capybara.app_host = 'https://www.google.com' Capybara.visit("/") Capybara.has_content?('foo') Capybara.reset_sessions! 

The browser and session remain open.

If I close the browser and run the test again, it will select the exception "Connection refused - connect (2)."

I need to restart the entire rails application in order to be able to restart the test.

Is this a way to run and re-run several Capybara tests over and over without restarting?

Something hypothetical like this would be nice:

 Capybara.new do #the tests... end 

or is it at the end ... Capybara.shutdown

I can not find anything in the documents.

+4
source share
2 answers

The Capybara selenium driver has a quit method . When Capybara launches a browser, it logs an at_exit hook, which will trigger quit.

But since you want to drop it, you have to do two things:

  • Call Capybara.page.driver.quit yourself
  • Monkeypatch Capybara so you don't get errors when Capybara tries to close the browser when you have already closed it:

     # from https://github.com/jnicklas/capybara/blob/master/lib/capybara/selenium/driver.rb#L9 # code inside at_exit hook is removed class Capybara::Selenium::Driver def browser unless @browser @browser = Selenium::WebDriver.for(options[:browser], options.reject { |key,val| SPECIAL_OPTIONS.include?(key) }) end @browser end end 

Here is the POC code - https://gist.github.com/abotalov/6274926


Since you just need a “few Capybara commands”, you can also write a helper method if you want:

 def with_capybara(&block) Capybara.app_host = 'http://www.stackoverflow.com' session = Capybara::Session.new(:selenium) block.call(session) session.driver.browser.quit end 

Here is also the POC code - https://gist.github.com/abotalov/6274999

+4
source

I can’t say that I understand why you are trying to do this ... However, here are a few things to try.

From: https://github.com/jnicklas/capybara

Transactions and database setup

Some Capybara drivers must run against a real HTTP server. Capybara will take care of this and start one for you in the same process as your test, but in a different thread. Selenium is one of these drivers, while RackTest is not.

Given that you must be running a real HTTP server when using Selenium Capybara, you should try an alternative driver such as RackTest. Capybara may not understand that you need to close the HTTP server.

Also from the same section:

You can also force ORM to use the same transaction for all topics. This can have consequences for thread safety and can cause strange failures, so be careful with this approach. It can be implemented in ActiveRecord through the following monkey patch:

 class ActiveRecord::Base mattr_accessor :shared_connection @@shared_connection = nil def self.connection @@shared_connection || retrieve_connection end end ActiveRecord::Base.shared_connection = ActiveRecord::Base.connection 

You can try the provided patch to make sure Capybara initiates threads that also do not close.

In any case, I have no idea if these options will help, but this is something to try.

-2
source

Source: https://habr.com/ru/post/1497615/


All Articles