How do I use Pow as my default server in Capybara? - capybara

I am enjoying using pow as my rack server during development and I would like to know if there is a simpler way to having it as my default server during capybara tests.
I have seen some people using:
Capybara.run_server = false
and setting pow to auto reload during each request by creating a file:
tmp/always_restart.txt
inside the project's home.

Related

Rspec/Capybara/Poltergeist: How to stub the URL?

For my tests, I would like to stub/fake the URL that the tests are using, so that in Rails my request.host shows up as something besides 'www.example.com'
In my research, most people suggest changing capybara's app_host, but this only works when you're using the default driver. When using Poltergeist it will actually go to that URL.
Is there anything else I can use?
When using Capybara/Poltergeist you're doing feature tests that test the full stack. You should not be attempting to stub/mock anything when doing that (maybe you want a controller spec instead?).
Additionally when not using the default rack-test driver you should be seeing '127.0.0.1' for request.host in your app (not www.example.com). You can change the Capybara.app_host to anything you want, but would then to need make sure your local DNS resolved that to 127.0.0.1 (/etc/hosts entry, etc). Most CI systems also allow you to specify DNS resolutions - https://circleci.com/docs/configuration/#hosts

RSpec/Capybara: Cross-Domain JS Testing

I'm working on an advertising application, where there is some cross-domain JS. I'd like to write tests that verify that the cross-domain JS is working as intended.
My first thoughts are that I would need to be able to
visit some_url_thats_not_my_rails_app
However, Capybara throws a "No Route Matches", since it obviously goes to only relative paths, and is intended for testing your OWN website. But I really need to go on ANOTHER page, and verify that things like the serialized token are identical.
Is Capybara the right tool for this? If so, what do I need to do to force non-relative paths?
Yes, Capybara is right tool for it. You can also use Ruby with Selenium webdriver(Would be awesome with page object gem) or Ruby with Watir webdriver.
To visit and test any web application, you can use Ruby and Capybara. For this you need to set app_host. Add this one in support/env.rb:
Capybara.app_host = "http://flipkart.com"
And in hooks.rb
Before do
visit('/')
end
You can use Capybara with Selenium webdriver and you will be good to test any deployed application in production, staging or prep env.

Rails, Cucumber, Capybara, Selenium, Pow, domains, subdomains and javascript

TL:DR - How can I make Cucumber request a page through the app but pretend that the request came from "http://mysubdomain.mydomain.dev", and run the javascript properly?
Ok, this may take a while to explain, but please bear with me...
I'm a 'relatively' experienced Rails dev, but only recently took the plunge into full BDD/TDD.
The page I'm testing in cucumber has some javascript in it for creating a new nested object (very similar to this RailsCast).
Now the problem lies in the fact that not only do I have variable sub-domains, I have variable Domains too (Just trust me on that one...)
So the app needs to query the request.host to find the current_domain and the current_subdomain before everything ties up in the background and it can carry on serving the right bits of the app.
I've managed to get all of the tests passing fine using the host! domain and Capybara.default_host = domain techniques in the background steps.
However, when I use the #javascript tag on the feature that tests a page with .js on it, I get Firefox grabbing focus, then trying to load the full url.
Now it also happens that I'm running Pow, and have those urls hooked up to the dev server. Unsurprisingly, they don't work as when the user tries to log in, it's looking in the dev DB. I tried running the suite after turning the pow server off, and it just timed out.
Surely the javascript webdriver shouldn't be actually accessing the url, but simply running the app itself and pretending that the host is what I tell it to be???
I'm obviously missing something here - How can I make Cucumber build the page internally, but pretend that the request came from "http://mysubdomain.mydomain.dev"?
edit: Jason - the variable domain trick is achieved exactly the same as subdomains... If you can query the db for an account based on request.subdomains.first, you can query for a domain through request.domain. You just have to double check a few things like capitalisation etc. to minimize the risk of a malformed url breaking stuff, and obviously you need to ensure that the domain record exists in the db first...
Oh - and cache the domain record requests with care...
It means that you can serve the same app, but with different styling and landing pages etc. Useful for PaaS apps with a broad customer base - you can re-brand it and sell it as a specific solution to one group's problems, even though it's the same guts underneath.
I am looking to do something very similar. I thought about adding an additional pow directory for test, then using the pow directives to override the environment. I think this is done within a ".powenv" inside your app directory. Here is an example of a quick fix:
echo export RAILS_ENV=cucumber > .powenv && touch tmp/restart.txt
But it would be better to do something dynamic so that before you start the test, you can tell pow what env to run, then when finished switch back, maybe even run the test server on a different port on a temp basis. Pow is the only great solution I know of so far for handeling subdomains.
EDITED: I have this working now in my environment with the following addition to my features/support/env.rb file.
# Switch Pow to For Cucumber Tests
Capybara.default_driver = :selenium # Subdomain testing will only work with pow and selenium
pow_config = "#{Rails.root}/.powenv" # Dont change, this is the Config Files Location.
pow_config_stash = "#{Rails.root}/.powenv_original" # This is what the config will be stashed as during testing.
Before do
# Set the default host
Capybara.app_host = "http://www.resipsa.dev"
# Stash the existing config
File.rename(pow_config,pow_config_stash) if File.exists? pow_config
# Write the new pow config
f = File.new("#{Rails.root}/.powenv", "w")
f.write "export RAILS_ENV=test"
f.close
# Touch tmp/restart.txt to force a restart
FileUtils.touch "#{Rails.root}/tmp/restart.txt"
end
After do
# Delete the temp config
File.delete(pow_config)
# Restore the Original Config
File.rename(pow_config_stash,pow_config) if File.exists? pow_config_stash
# Touch tmp/restart.txt to force a restart
FileUtils.touch "#{Rails.root}/tmp/restart.txt"
end

Can I change config.cache_classes programatically in Rails 3?

I have some iPhone client tests that run against my development rails server. The whole suite runs an order of magnitude faster if I turn on class caching in the Rails config. On the other hand, that slows down development when I'm not actually running the tests.
I want the test suite to hit an action at the beginning to turn on class caching and another action at the end to turn class caching off again.
Is this even possible? If so, how?
Not without some serious hacking. Rails goes to quite a lot of trouble to make sure your files are reloaded on every request (when cache_classes=false). The value of the cache_classes configuration variable is used by initializers in several places not the least of which being:
using require to load ruby files when cache_classes is true (meaning they are no longer reloadable)
setting up dispatcher callbacks to reaload the application on every request when cache_classes is false
You do have access to the value of the cache_classes variable, and you can even change it if you like:
Rails.configuration.cache_classes = true
But, this will have no effect on the running rails instance as the initializers where that value is used only run once when the rails app starts up.
What this means is this, unless you're prepared to invest some serious time and hacking effort you can't really avoid a restart of your server. So, what you need to look at is controlling this restart process via your test suite.
For example you can try to restart rails from within rails. This would allow you to define an action that your test suite can hit right before it begins executing (to restart the server in the right mode), and another action which the server can hit after all tests have finished, to restart everything with cache_classes set to what it used to be. You would control the value of cache classes via an environment variable like this post suggests.
It would still require a bit of work to set all of this up and get it to hang together, but this is probably your best bet if you want an 'auto-magical' solution.
I don't think doing what you suggest will work.
But I suggest you may be looking for the wrong solution.
If what you want is to access your development database from your iphone testing,
then why not add a new environment.
Add a new file config/environments/iphone_dev.rb
require File.dirname(__FILE__)+"/development.rb"
config.cache_classes = true
And in your database.yml (or mongoid.yml or whatever)
iphone_dev:
host: localhost
database: my_app_development
There is no reason the database cant be the same
Now just run rails server -eiphone_dev -p3001
You should have a server, almost the same as your dev server,
but running on a different port, with caching enabled.

Rails auto-reloading plug in development mode

I am trying to have a plugin I am developing auto-reload every time I change my code, emulating the same auto-reloading that happens normally in Rail's development mode. My plugin is primarily an ActiveRecord mixin module. I have tried all suggestions I have been able to find in related Google searches. Nothing has worked yet.
In my plugin's init.rb:
require 'activesupport' unless defined? ActiveSupport
require 'activerecord' unless defined? ActiveRecord
if RAILS_ENV == 'development'
ActiveSupport::Dependencies.load_once_paths.delete lib_path
ActiveSupport::Dependencies.load_once_paths.delete File.join(lib_path, 'crowd_compass', 'publisher.rb')
ActiveSupport::Dependencies.load_paths << lib_path
ActiveSupport::Dependencies.load_paths << File.join(lib_path, 'crowd_compass', 'publisher.rb')
end
ActiveRecord::Base.send(:include, CrowdCompass::Publisher)
Looking in the rails changelog, I did notice the feature to auto reload all plugins.
config.reload_plugins = true if RAILS_ENV == 'development'
This did not work as I expected it to when I added it to my conf/environment.rb
My plugin is structured so all files are auto-loaded by namespace => directory. I did this so I could avoid using "require", as I thought require was inhibiting my plugin from being auto-reloaded.
I have been doing all of my work in development mode through the rails console and I do not know if this behaves any different than running through mongrel (or like web server).
The plugin works as expected, but I have to reload every time I make any change to the code. Does anyone know a way to get plugins to reload?
The console definitely doesn't work like a mongrel. All of the techniques you're using are made to reload on every request, which is akin to every time you start up the console.
There isn't a way to reload code in the console without calling reload!.

Resources