Rails 3.1 Testing AJAX Capybara and Spec - ruby-on-rails-3.1

I have a mountable engine that provides Javascript assets to the main Rails App, but while I try testing AJAX calls (provided by the Javascript I just mentioned) in Capybara and Spec. The database doesn't seem to update.
Because of the cluster nature of testing, I can't have a clear understanding of what exactly is being run and loaded on the Dummy App. I was thinking about running the Dummy App as a standalone app and verify what is being rendered via the web browser (sort of manually checking things out). But that wasn't possible because of a Gemfile not found error every time I try running 'rails s' within the spec/dummy folder.
So my question: is there another way to check why the AJAX calls are not working in my test cases(although when I run the app normally the AJAX calls are working just fine) or does anyone know how to run the dummy app as a standalone rails App?
Thanks

The default settings of Capybara doesn't allow AJAX calls.....I had to modify the driver used by Capybara in the spec_helper.rb by adding the following:
Capybara.default_driver = :selenium

Related

Changing the base URL for Capybara Acceptance Tests

I am developing a sinatra based web application and I extensively use tests to make sure that everything is working before deployment. As testing frameworks I use minitest::specs and capybara with webkit.
My problem is that after deployment my application runs with a base url like this:
http://cool.server.net/to-the-application/
But during tests capybara assumes a clean base-url with a path to / not to to-the-application/. This means I can't test to find bugs which relate to forgetting to set the base-url within links and actions.
For dry testing I followed Changing the base URL for Rails 3 development and modified my config.ru, but I haven't found any way to get capybara to use a different base.
Any ideas how to solve this?
If using the rack_test driver the hostname is completely ignored so changing it isn't going to do anything. If using a different driver you can specify
Capybara.app_host = "http://cool.server.net"
Note that cool.server.net would generally need to resolve to 127.0.0.1 since thats where Capybara binds the app being tested.
Update: After thinking about this I'm not sure that is what you wanted. If what you want is for Capybara to mount your app under /to-the-application then you're going to have to create your own app object which you assign to Capybara.app - see https://github.com/teamcapybara/capybara/blob/2.13_stable/lib/capybara/rails.rb#L4 for how Capybara currently mounts the app.

How to find out if a page is running in Phantom JS (or Poltergeist)?

How to test within Javascript whether it is running Phantom JS (via Poltergeist) or on a regular browser?
Use case: Suppress logging during testing.
Using Poltergeist, Capybara, Ruby.

Launch sinatra from a test/spec or another ruby script

I'm experimenting, and I'm trying to launch dummy Sinatra application from RSpec and kill it when the spec is finished. Something like:
# spec/some_spec.rb
before(:all)
# launch sinatra dummy app
end
after (:all)
# kill sinatra dummy app
end
it 'should return list of whatever' do
expect(JSON.parse(make_request('0.0.0.0:4567/test.json')))
.to include('whatever')
end
I could use system("ruby test/dummy/dummy_app.rb"), but how can I kill that process only? Does anyone know how I can launch the Sinatra inside a test (or from another ruby script)? I know about WebMocks, but I want to see if I can manage to make my test work this way.
Look under RSpec on "Testing Sinatra with Rack::Test". I'd suggest you use that code as boilerplate to get started.
Just add this to your describe block:
def app
Sinatra::Application
end
I would suggest you read up RSpec.
Since you want to test an external system, by the looks of your comment, instead of system "curl whatewer.com", you can use Net::HTTP to make requests and then test against the response.
Have a look at "Testing an external API using RSpec's request specs".
As I'm writing request specs to ensure the features won't be broken I decided to rather write separate Cucumber features. The nice thing is that I can use Capybara, and thanks to Selenium Web Drive, I can launch a server before I run my tests.
So, I created a dummy Sinatra application (that will represent the external service to which the actual code I'm testing is doing requests (including a nasty system('curl whatever.com')).
All I have to do is stub out the methods passed to curl to use Capybara.current_session.server.host and Capybara.current_session.server.port.
Once I'm done with my re-factoring all I have to do is remove the Capybara server variables, and Selenium web drive from the cucumber/capybara configuration.
Tests after a brief change will be still working and will be valid.
Update
In the end I wrote it all with RSpec request tests, as doing it in Cucumber was little bit time consuming and I already spend too much time on this.
I mark these kind of request tests with RSpec tag and Before I lunch these I manually lunch simple Sinatra/Grape dummy API application to which the request are made. (Then I run RSpec tests with this tag)
So basically I end up with specs for functionality that uses net/http that uses WebMock and don't need a server, and request tests for which I need to run the server before I run the specs. So the original question remains, how to lunch a server before tests start
After I cover all the functionality I'm gonig to rewrite the curl to net/http however I'm going to keep those requests specs as I discovered they are nice idea when it comes to crazy API scenarios (like testing https + diggested authentication)

WATIR/ FireWATIR URL consistency?

We are at the start of a project to test a new web application written in jQuery and are planning to do it using WATIR and FireWATIR.
During the proof of concepts test I've discovered differences in how WATIR and FireWATIR represent URLs, for example a script which passes in FireWATIR generates this error in WATIR.
assert_equal(expandImage.src,"../WebToolKit/images/closed.jpg")
testBrowserGadget2(WebClientHomePage) [002_pub_browser.rb:108]:
<"http://172.24.4.125:8081/WebToolKit/images/closed.jpg"> expected but was
<"../WebToolKit/images/closed.jpg">.
Is there any setting in either WATIR or FireWATIR I can enable so that the URL value is consistent between when running against IE and Firefox?
I use the uri module ...
e.g.
require 'uri'
URI.parse("http://google.com/image/path.jpg").path
=> "/image/path.jpg"
i.e.
assert_equal(URI.parse(expandImage.src).path,"../WebToolKit/images/closed.jpg")
you could use assert_match http://en.wikibooks.org/wiki/Ruby_Programming/Unit_testing

File uploads using the selenium-client gem

I am using the selenium-client gem with Webrat and Cucumber to test a rails project. I have Cucumber set up pretty much as described here: https://github.com/cucumber/cucumber/wiki/Setting-up-Selenium
My problem is that Selenium is prevented from performing file uploads. Ideally I'd like to be able to replicate webrat's 'attach_file' method. I tried dodging the issue by using selenium-client's 'type' method to type the path to the required file, but this seems to fail.
Does anyone have a sugestion for a workaround using selenium-client (i.e. not the regular Selenium gem)?
The normal selenium can't do this because browser security prevents javascript from writing to input=file elements. This is a security feature so that your password file doesnt get uploaded to hackers if you go to a site thats been hacked.
http://jira.openqa.org/browse/SEL-63 has the details
I finally figured this out.
You need to be using firefox with the chrome backend, and you need to specifiy an absolute filepath.
I ended up creating a helper module to specify the filepath, and a "fixtures" folder containing test content. Here is a gist of the solution I came up with: http://gist.github.com/214185
Therefore it is totally possible to replicate Webrat's attach_file method!

Resources