Rails: specific rake task dependencies for different environments

In my main rakefile there are several tasks that you need to stop and start selenuim as follows:

require 'selenium/rake/tasks'

Selenium::Rake::RemoteControlStartTask.new do |rc|
  rc.port = 4444
  rc.timeout_in_seconds = 3 * 60
  rc.background = false
  rc.wait_until_up_and_running = true
  rc.additional_args << "-singleWindow"
end

Selenium::Rake::RemoteControlStopTask.new do |rc|
  rc.host = "localhost"
  rc.port = 4444
  rc.timeout_in_seconds = 3 * 60
end

This makes it necessary to require the monster selenuim to be installed for rake use regardless of the rails environment. Where can I put this code, so it will be loaded only when the rails environment is configured to check?

Rails 2.3

Greetings

+3
source share
1 answer

Are you using Rails 3 or Rails 2?

Rails 3 add these blocks:

if Rails.env.test?
  require 'selenium/rake/tasks'

  Selenium::Rake::RemoteControlStartTask.new do |rc|
    rc.port = 4444
    rc.timeout_in_seconds = 3 * 60
    rc.background = false
    rc.wait_until_up_and_running = true
    rc.additional_args << "-singleWindow"
  end

  Selenium::Rake::RemoteControlStopTask.new do |rc|
    rc.host = "localhost"
    rc.port = 4444
    rc.timeout_in_seconds = 3 * 60
  end
end

In Rails 2 (or 3, but it is deprecated) as follows:

if RAILS_ENV == "test"
  require 'selenium/rake/tasks'

  Selenium::Rake::RemoteControlStartTask.new do |rc|
    rc.port = 4444
    rc.timeout_in_seconds = 3 * 60
    rc.background = false
    rc.wait_until_up_and_running = true
    rc.additional_args << "-singleWindow"
  end

  Selenium::Rake::RemoteControlStopTask.new do |rc|
    rc.host = "localhost"
    rc.port = 4444
    rc.timeout_in_seconds = 3 * 60
  end
end
+4
source

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


All Articles