Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails: specific rake task dependencies for different environments

My main rakefile has some tasks 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 forces the requirement to have the selenuim gem installed to use rake regardless of the rails environment. Where can I put this code so it will only be loaded when the rails environment is set to test?

Rails 2.3

Cheers

like image 711
user448157 Avatar asked Jul 28 '26 07:07

user448157


1 Answers

Are you using Rails 3 or Rails 2?

Rails 3 add a blocks like so:

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's deprecated) like this:

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

Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!