how to interact with javascript JSON response with rspec and capybara - selenium

I'm using rspec and capybara and would like to test a JSON response that I get in response to for example click_button. I understand that I can use :js => true but I would like to parse back the JSON that I get. I see that I can do something like:
get '/your/path', format: 'js'
Is there a way to do something like:
click_button('Save', format: 'js')
::JSON.parse(response)....
?
thx

The aim of browser elumation libraries like Selenium is to approach real browsers and allow emulating behavior of real users. Real users can't read responses to AJAX actions so browser emulation libraries don't give such ability. Also it will be quiet hard for Selenium guys to implement it.
There are several possibilities that you have:
Write this test using one of http client libraries. But you can't really test end-to-end something that involves lots of Javascript and AJAX using this method
It's likely that AJAX action changes something in the DOM of your application. You can easily write a check for it using Capybara.
But if really want to test your app end-to-end and want to directly check HTTP response to AJAX action you should use a proxy that will record all responses. Then you make a search through those responses. Selenium maintainers advice to use Browsermob proxy to do something like this. Here are Ruby bindings for it that are written by a maintainer of Selenium's Ruby bindings

I had to do this for a special use case as well.
You can strip the html tags and parse it:
JSON.parse(ActionView::Base.full_sanitizer.sanitize(page.html))

Related

How can I get page status code using behat + mink with selenium driver?

Selenium does not support this code
$statusCode = $this->getSession()->getStatusCode();
It is possible to check status any other way?
This is not what Selenium was designed for. One of their project member in reply to request to implement this in Selenium said:
We will not be adding this feature to the WebDriver API as it falls outside of our current scope (emulating user actions).
The solution is to either use another driver that supports status codes, or try to implement one of available hacks (given in other stack overflow questions asking the very same thing).
You can use Restler, a micro framework which supports behavior Driven API testing using Behat and Guzzle.
For example (see: status_codes.feature):
#restler
Feature: HTTP Status Codes
Scenario: I should be able to suppress status code 404
When I request "/examples/_001_helloworld?suppress_response_codes=true"
Then the response status code should be 200

How to test javascript function?

Is it possible to test existance of js function in page using Capybara? if so, can I make a call and check result?
Capybara is used to test the overall functionality of your web application by actually rendering the page. When you are using an engine that can execute Javascript (the default Rack::Test engine does not), you can use it to check that your page is indeed rendered correctly after executing the relevant Javascript. There are ways to test a function's existence by calling page.execute_script("typeof yourFunctionName == 'function'") (and only select engines support this) and check its value but know that Capybara it is not designed to be a Javascript test framework. You should use Jasmine for that.

Injecting arbitrary Javascript on all my Rails view

I'm trying to implement a javascript tracker that i need to inject arbitrarily on all of the views rendered by my application, just like the newrelic client instrumentation works.
My app allows user to edit their liquid html templates, so the idea doing this is to inject the script in a way that the user is uncapable to remove it (auto added)
I look the code in the newrelic gem but is too confusing and i wondering if there is a more simple way to do it.
Thanks in advance!
Well I have a solution for this you could write a middleware where you could just check
if the request is for html page or (css or javascript)
if the request is for the html page
append the javascript to the html page before sending the response for the server
here the catch you need very sure what you are doing this I have ran into this problem
Make sure your middleware placement is correct since the development everything is single thread and wrong placement would result in deadlock error
When HTML page is consider what if the request is an ajax request what then you have to be very specific on that regards
Hope This help

Jasmine: testing my rails site's interface with it

I've been looking for ways to test a Rails 3 app that has quite a lot of JS code for its rich interface. I tried with Capybara, but that didn't work out, so now I'm giving Jasmine a try. But I'm having a hard time understanding how I should go about it.
From what I gathered, Jasmine alone is good for testing the JS components of a site, but what if I want to test the interface directly? I need something like:
describe "Sign in"
Visit '/home'
When user clicks "Sign in" link
The sign in form should appear
Can I actually do something like that with Jasmine? So far, my tests are included on a results page generated by the rails-jasmine gem and obviously they run over that DOM, not over my site's DOM.
I'm now trying to use evergreen, but I get the same result (I can't even include jQuery).
Any ideas?
Thanks!
To answer your question - no, Jasmine will not inherently function in a way which will let you navigate your site and test at such a high level, like an integration test with Cucumber.
Jasmine is built primarily to test the API of your js, and so essentially you will only be able to test URL routes or something with it if they are part of your API.
For example, you can test how a Backbone Router will respond to your window's current location, or navigating from one location to the next, but that is because Backbone Routers explicitly handle URL locations.
This might help: http://railscasts.com/episodes/275-how-i-test
You could try using jasminerice with fixtures. If the intent is to test the JS DOM interaction, then fixtures would be the way to go.
But if the idea is to test the server interactions as well, then going with something like cucumber makes sense.

How might I provided a URL use the FireShot API to take a screenshot, upload to Imgur, and return some output (eg. markdown)

I am looking for a way to utilize the FireShot API with JS to given a URL (or perhaps a list) use the FireShot API to take screenshot, upload to Imgur, then return the user the URLs or perhaps something like markdown to use quickly in forums.
Method 1: Open new window
I tried opening the URL in a new window, but found that I cant control that page with JS dues to cross domain problems. The same with iFrames.
Method 2: simple $.get()
A simple $.get() wont work because of the same cross domain issues I guess?
http://jsfiddle.net/t6aeq/
$.get($url.val(), function(data) {
console.log(data);
});
Via PHP "Proxy"
So I tried creating a simple PHP script that gets the HTML of the URL and returns it to my JS (using file_get_contents($url)). But some sites like Microsoft will detect that I am using some automated methods and give an error page of sorts. I also cant seem to find a way to use jQuery to query that returned HTML for link[rel=stylesheet], script, style and body to append to the head and a div respectively. I posted abt that on another question
A new Idea: Embed scripts on browser level
So I thought away of getting around these is using iMacros or GreeseMonkey or something to insert scripts into pages on the browser level instead? But any guidance or tips on how can I do that? Also, I'd prefer a pure JS/PHP method if available so users are not limited to using Browser plugin/scripts (tho I will be the only user for now)
It suddenly came to my mind that this may not work because the FireShot API key and Imgur is limited to the domain? Any solutions?
You might be able to inject the FireShot script using Greasemonkey. But, first use GM_xmlhttpRequest() to fetch an API key, for that page's domain, from the "Create FireShot API Key" page.
Note that GM_xmlhttpRequest() does not have the same cross-domain issues that $.get() has.
However, at this point you might be better off just writing your own Firefox add-on. Maybe start with FireShot's code for ideas. Also see the Screengrab add-on.