1

How can I run a watir test in the context of the app that's being tested? I'd like my test to browse the app and then access ActionMailer::Base.deliveries for emails or check models directly. This is how I understand what's being described here.

UPDATE: They probably use Capybara to be able to acces the email array and be in the context of the "server" which is instantiated just for the test.

m33lky
  • 7,055
  • 9
  • 41
  • 48

3 Answers3

0

I suggest checking out the Rails unit testing docs, then writing a simple Rails test case that starts your app - then try adding a line or two of Watir code to access your app:

http://guides.rubyonrails.org/testing.html

As far as I know you should be able to write a Rails unit test, then put Watir code inside one of your test methods - and if all goes well you should be able to instantiate your web app, use Watir to launch a browser to test it, and in the same method(s) perform non-Watir low-level testing (e.g. checking models/data/etc.)

I've never used Watir inside a Rails test, but I don't see why it wouldn't work.

Bill Agee
  • 3,606
  • 20
  • 17
  • It seems like it's not about just loading the Rails context, it's running precisely in the context of the server. How else could you read those emails? I think the other possibility is that they are running something like an `ActionDispatch::IntegrationTest` with `Capybara` where the "server" is launched from within your test suite. – m33lky Jan 16 '12 at 08:22
0

Watir is about driving browsers to automate functional testing. You could I suppose use it for unit testing of the top level UI stuff, but more often in a 'unit test' context that would be done using a headless browser emulation, Capybara, celerity, or watir-webdriver using the headless option, purely for speed of operation since driving an actual browser can be slow even with a fast browser like chrome.

Most of the times people use Watir it's for more functional tests, often from a test runner framework like Cucumber, sometimes Rspec depending on your needs. You might combine that with other ruby code to access or create test data, to validate something made it into the DB from the UI, but everything in the Watir gem is all about the browser and interacting with it much like a human would, and driving the browser is it's function within the set of tools you might use.

Chuck van der Linden
  • 6,660
  • 2
  • 28
  • 43
0

I had the same need and found the following solution: https://stackoverflow.com/a/9687562/90741. I reproduce it here, as the linked question seems to be dead, with its owner...

I had the same need (Watir+RSpec), but the problem is: there is no Rack stack running by default during tests...

I found this solution:

In spec_helper.rb:

HTTP_PORT = 4000

test_instance_pid = fork do
  exec 'unicorn_rails -E test -p %d' % HTTP_PORT
end

at_exit do
  Process.kill "INT", test_instance_pid
  Process.wait
end

Which start the test stack once for all spec tests, and kill it at the end. (In this sample, I am using unicorn, but we can imagine using any other server)

In the spec, I reuse the HTTP_PORT constant to build URL:

browser.goto "http://localhost:#{HTTP_PORT}/"
Community
  • 1
  • 1
Mike Aski
  • 9,180
  • 4
  • 46
  • 63