When running selenium with capybara/rails, how do I configure the server?

前端 未结 2 974
说谎
说谎 2020-12-24 15:35

Normally I use the rails dev server at port 3000.

Capybara/Cucumber uses Capybara.server_port = 31337. If I swap from the default driver to selenium, it

相关标签:
2条回答
  • 2020-12-24 15:50

    If you use selenium, you should start web server in test environment. You can start it on a different port from development. Then you should configure Capybara to use that port:

      Capybara.run_server = true #Whether start server when testing
      Capybara.server_port = 8200
      Capybara.default_selector = :css #:xpath #default selector , you can change to :css
      Capybara.default_wait_time = 5 #When we testing AJAX, we can set a default wait time
      Capybara.ignore_hidden_elements = false #Ignore hidden elements when testing, make helpful when you hide or show elements using javascript
      Capybara.javascript_driver = :selenium #default driver when you using @javascript tag
      # Other option is:
      # Capybara.javascript_driver = :webkit
    

    If you don't want to start server every time before you run your cucumber scenarios I suggest you to use capybara webkit driver. All javascript scenarios will run at background. It also will work on continuous integration server.

    If you are on Linux platform, capybara-webkit requires an X server to run. To setup it you can use headless gem. Here is headless configuration for cucumber:

    # place code below in features/support/headless.rb
    if Capybara.current_driver == :webkit
      require 'headless'
    
      headless = Headless.new
      headless.start
    
      at_exit do
        headless.destroy
      end
    end
    
    0 讨论(0)
  • 2020-12-24 15:57

    This may or may not answer your question, but here's my env.rb file (the relevant bits).

    Capybara.app_host = "http://0.0.0.0:3000"
    Capybara.server_port = 3000
    

    I also have this in there:

    Capybara.register_driver :iphone do |app|
      require 'selenium/webdriver'
      profile = Selenium::WebDriver::Firefox::Profile.new
      profile['general.useragent.override'] = "iPhone"
    
      Capybara::Selenium::Driver.new(app, :profile => profile)
    end
    

    Maybe you can have the Capybara.app_host and .server_port lines inside the register_driver bit and switch driver in your Cucumber scripts. :/

    0 讨论(0)
提交回复
热议问题