Javascript tests using poltergeist and capybara accessing internal api - ruby-on-rails

I am using capbara with poltergeist to run my javascript tests and i often get this error, it is trying to access internal api,it is even worse when my internet connection is off as more tests start failing, I am also using vcr and webmock to test external apis. What might the problem be
Failure/Error: visit user_session_url
Capybara::Poltergeist::StatusFailError:
Request to 'http://www.example.com:65264/users/sign_in' failed to r
reach server, check DNS and/or server status - Timed out with no
open resource requests

Could be a restriction on the app to allow traffic hitting internal API's.
On a side note, PhantomJS is not being actively supported/developed anymore. Consider alternative headless browser solutions. Chrome Headless was suggested by the guy that declared PhantomJS dead.

Related

Mocking login for remote services - Cucumber, Capybara, Rails

I have a Rails application that has changed from user authentication via devise to accessing a remote API for authentication. For the test suite, this requires that I mock the https request for authentication and return a valid response. We are using Cucumber and Capybara for testing and I am attempting to use the webmock Gem to mock the login response.
The login is initiated by a button click on a form (Capybara action 'click_button').
Unfortunately, although webmock appears to be installed correctly (I am able to make a Net::HTTP POST request and this is recognized by Webmock), the POST to the remote authorization facility is not being captured by webmock. I know that the form POST is making its way to the controller, because the stacktrace shows that the POST is being executed in the controller as it should and the error message is "Errno::ECONNREFUSED at ... Failed to open TCP connection".
I have tried:
WebMock.stub_request(:any, '127.0.0.1').to_return(body: {STATUSCODE: 1}.to_json)
and
WebMock::stub_request(:any, '127.0.0.1').to_return(body: {STATUSCODE: 1}.to_json)
and
stub_request(:any, '127.0.0.1').to_return(body: {STATUSCODE: 1}.to_json)
I have tried putting the stub_request call in the devise_steps.rb file, just before the "click_button" command, as well as in the features/support/env.rb and features/support/webmock.rb file.
I assume that what I am doing is so common that it has to be possible, but I have found nothing that indicates why the stub_request is not successful.
So the domain of the remote API for authentication is localhost? So it would be running on the same server with a different port? Then you have to mock the address with the port.
For instance your Rails app is running on port 80 and your auth API is running on 8080 then you have to do this.
stub_request(:any, '127.0.0.1:8080').to_return(body: {STATUSCODE: 1}.to_json)
I think Webmock should support different ports but I'm not 100% sure. Also have you set WebMock.disable_net_connect!(allow_localhost: true)?
https://github.com/bblimke/webmock#external-requests-can-be-disabled-while-allowing-localhost
Edit:
If this is not working, I suggest you make the API URL configurable and e.g. set it in tests to auth-api.com and then mock this.
The root cause of the problem was that the path on the stub_request was not complete. I had misread the webmock documentation....

HTTP proxy for Appium in native apps?

I was wondering if somebody has an idea what Java/Groovy library/tool to use for sniffing, recording and processing HTTP traffic of native apps when running Appium tests ? Does anybody has experience with Browser Mob for that specific case ? Thanks! Best, Daniel
Yes browsermob can be used used with native apps and selenium. We tried many other network sniffing proxies but browsermob works best for selenium.
Can be done in 3 simple steps(not so simple by the way).
Run browsermob from standalone server and get response as har in localhost
Write code to validate your output.
Call browsermob methods in your selenium code to call
Validate the response you got with code you have already written for validation.
Mostly validation involves parsing JSON files as most of https responses will be in JSON format.
If you need help in any specific area of browsermob proxy or selenium or running feel free to ask me.

How to mock a request in ruby on rails

How to mock a request in ruby on rails
I am making a call to multiple external sites via API and facing lot of failures in the response due to net read timeout error and parameters errror , run time errors, etc.
I want to mock the request before making a original call to the API. So that i can avoid more failures in my app.
Can any one help me ?
Try using the VCR gem, it records HTTP request for you, the first time you run your tests. For subsequent tests VCR uses the previously recorded HTTP response.
This should solve your timeout errors and allow you to work with external API's more easily.
Checkout these resources for more info:
https://www.relishapp.com/vcr/vcr/docs
http://natashatherobot.com/vcr-gem-rails-rspec/

How to run rails request specs on a cloud deployment?

I am new to rails testing. Two days of running down leads with Google has turned up no solutions for what ought to be a frequent need.
If I write request (integration) specs to use a Selenium or other browser-based driver, is it possible to redirect the test's i/o to a staging deployment on a cloud server (in my case Heroku)?
If so, how? If not, what prevents this from working?
So far I have been using rspec/capybara, but would switch to anything of similar power if necessary.
You can use Capybara with Selenium driver, and set Capybara.app_host to specify the IP of you staging app server. While doing so you can turn off Capybara local rack with Capybara.run_server = false
Remote testing will allow you only to perform human kind of action and test the returned generated HTML/JS/Json etc .. but no access to controller, view, or any other app internal objects.
On thing you could do (I never tried, but I don't see why it wouldn't work) is to set-up your database.yml test configuration to remotely access you staging database, allowing you to control the database during your tests. It's not really secure so you may want to do that over a SSH tunnel , or a similar solution.

Connection refused while web scraping using HTMLUnit

I am trying to build a java application to scrape a website using HTMLUnit. After extracting some data the application encounters following exception -
java.lang.RuntimeException: org.apache.http.conn.HttpHostConnectException: Connection to siteURL refused.
If I run application again, it is able to extract some data again before failing with the same exception. Probably the server see lot of requests from same client IP and refuses connection for a request.
Also, when application encounters this problem, I am able to connect to the site using a browser.
How can I overcome this problem? In web scraping applications how are such problems approached and resolved?
This is how I debug such issues :
Download Fiddler
By default, fiddler listens on port 8888, all you have to do, is configure webClient to use fiddler as proxy & then all requests being sent can be seen (analyzed, modified & re-sent too) in fiddler.
client.getOptions().setProxyConfig(new ProxyConfig("127.0.0.1", 8888));
From what I can say from my previous experience is that the target site blocks after some-time, you can try adding a pause or rotating proxies & user-agents. You can also try clearing cookies.

Resources