I have a bundler application to perform cucumber tests for two of my applications named as "validation" and "recruiters". The directory structure of my test application is as follows:
root-folder
|_features
|_recruiters
|_recruitment_navigation.feature
|_step_definitions
|_recruitment_navigation_steps.rb
|_validation
|_FEATURE FILES
|_step_definitions
|_DEFINITION STEPS
|_support
|_env.rb
For the above directory structure. undefined method visit for #<Object:0x870c080> (NoMethodError) for every test which requires page visit.
But for the following directory structure, the tests are working fine:
root-folder
|_features
|_FEATURE FILES
|_step_definitions
|_DEFINITION STEPS
|_support
|_env.rb
Here is the env.rb file:
require 'capybara'
require 'capybara/dsl'
require 'capybara/cucumber'
require 'rspec/expectations'
require "ruby-debug"
Capybara.app_host = ENV["host"]
Capybara.run_server = false
Capybara.default_driver = :selenium
World(Capybara)`enter code here`
Please help me with this. Is there any different kind of setup for Capybara that is required for first type of directory structure?
You will need to tell cucumber to correctly require the features directory when running features in a subfolder.
e.g.
cucumber host=<host-url> -r features features/recruiters/<feature file to be tested>
Should work I think.
Related
I have been working on a Rspec/Selenium Webdriver test framework where I need to run my Rspec tests distributed across multiple files in same directory spec/*_test.rb.I was looking for the existing solutions available and stumbled upon deep test gem (https://github.com/qxjit/deep-test) which helps driving the tests in parallel leveraging selenium grid but I was not able to implement it based on the documentation available and looks like there is no active development going on with it.Are there any ported version of deep test available to work with RSpec > 2.0.
I also looked into parallel_tests (https://github.com/grosser/parallel_tests) but not sure how we can use it for running multiple process on the same cpu with each process running a different rSpec test.
Here is a snippet from one of my spec file,
require 'selenium-webdriver'
require File.join(File.dirname(__FILE__),'../support/Setup')
require 'rspec'
require File.join(File.dirname(__FILE__),'../support/spec_helper')
require File.join(File.dirname(__FILE__),'../support/Helper')
describe "Test", :type => :selenium do
it "should search for flights" do
airline.home_page.queryFlight('oneWay', 'ATL', 'ORD', 'today')
end
it "should list all the flight results and select one flight" do
airline.flight_list_page.selectFlight
end
end
Similarly i have other spec files which I am trying to run in parallel.
In a rails project, i add a feature file and step definition try to use capybara+selenium to load an external url,but it can not work? it has no any browser loading behavior,and from the console, it did not request the external url 'http://baidu.com' but 'http://localhost:3000' why, how can check for me,thanks very much!
**env.rb**
require 'cucumber/rails'
require 'capybara/rails'
require 'capybara/cucumber'
require 'selenium/client'
Capybara.current_driver = :selenium
Capybara.default_selector = :css
**setp_definition:**
When /^i visit baidu$/ do
visit('http://www.baidu.com/')
end
Then /^you should see Baidu$/ do
page.should have_content('Baidu')
end
****the infor on the console:****
[31m expected there to be text "Baidu" in "Browse the documentation Rails
Guides Rails API Ruby core Ruby standard library Welcome aboard You鈥檙e riding
Ruby on Rails! About your application鈥檚 environment Getting started Here鈥檚 h
ow to get rolling: Use rails generate to create your models and controllers To s
ee all available options, run it without parameters. Set up a default route and
remove public/index.html Routes are set up in config/routes.rb. Create your data
base Run rake db:create to create your database. If you're not using SQLite (the
default), edit config/database.yml
with your username and password." (RSpec::Ex
pectations::ExpectationNotMetError)[0m.
I have an Rails 3 App, here i have a unit test in test/unit/test.rb
require "test/unit"
require "lib/feeder"
class LinkParserTest < Test::Unit::TestCase
def test_with_single_host
**some code**
assert_equal(links,Feeder.link_parser(entry_summary,host))
end
if i run test in console by using ruby -I. test/unit/link_parser_test.rb command everything works perfectly
but if i run test in RubyMine it says require: no such file to load -- lib/feeder (LoadError)
what i am doing wrong?
got it, rubymine runing ruby -Itest test/unit/link_parser_test.rb command (so it starts in test folder) i had to change require to require "./lib/feeder" and it works normaly
It is pretty easy with the added generator of rspec-rails to set up RSpec for testing a Rails application. But how about adding RSpec for testing a gem in development?
I am not using jeweler or such tools. I just used Bundler (bundle gem my_gem) to setup the structure for the new gem and edit the *.gemspec manually.
I also added s.add_development_dependency "rspec", ">= 2.0.0" to gemspec and did a bundle install.
Is there some nice tutorial what to do next to get RSpec working?
I've updated this answer to match current best practices:
Bundler supports gem development perfectly. If you are creating a gem, the only thing you need to have in your Gemfile is the following:
source "https://rubygems.org"
gemspec
This tells Bundler to look inside your gemspec file for the dependencies when you run bundle install.
Next up, make sure that RSpec is a development dependency of your gem. Edit the gemspec so it reads:
spec.add_development_dependency "rspec"
Next, create spec/spec_helper.rb and add something like:
require 'bundler/setup'
Bundler.setup
require 'your_gem_name' # and any other gems you need
RSpec.configure do |config|
# some (optional) config here
end
The first two lines tell Bundler to load only the gems inside your gemspec. When you install your own gem on your own machine, this will force your specs to use your current code, not the version you have installed separately.
Create a spec, for example spec/foobar_spec.rb:
require 'spec_helper'
describe Foobar do
pending "write it"
end
Optional: add a .rspec file for default options and put it in your gem's root path:
--color
--format documentation
Finally: run the specs:
$ rspec spec/foobar_spec.rb
Iain's solution above works great!
If you also want a Rakefile, this is all you need:
require 'rspec/core/rake_task'
RSpec::Core::RakeTask.new(:spec)
# If you want to make this the default task
task default: :spec
Check the RDoc for RakeTask for various options that you can optionally pass into the task definition.
You can generate your new gem with rspec by running bundler gem --test=rspec my_gem. No additional Setup!
I always forget this. It's implemented here: https://github.com/bundler/bundler/blob/33d2f67d56fe8bf00b0189c26125d27527ef1516/lib/bundler/cli/gem.rb#L36
Here's a cheap and easy (though not officially recommended) way:
Make a dir in your gem's root called spec, put your specs in there. You probably already have rspec installed, but if you don't, just do a gem install rspec and forget Gemfiles and bundler.
Next, you'll make a spec, and you need to tell it where your app is, where your files are, and include the file you want to test (along with any dependencies it has):
# spec/awesome_gem/awesome.rb
APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..', '..'))
$: << File.join(APP_ROOT, 'lib/awesome_gem') # so rspec knows where your file could be
require 'some_file_in_the_above_dir' # this loads the class you want to test
describe AwesomeGem::Awesome do
before do
#dog = AwesomeGem::Awesome.new(name: 'woofer!')
end
it 'should have a name' do
#dog.name.should eq 'woofer!'
end
context '#lick_things' do
it 'should return the dog\'s name in a string' do
#dog.lick_things.should include 'woofer!:'
end
end
end
Open up Terminal and run rspec:
~/awesome_gem $ rspec
..
Finished in 0.56 seconds
2 examples, 0 failures
If you want some .rspec options love, go make a .rspec file and put it in your gem's root path. Mine looks like this:
# .rspec
--format documentation --color --debug --fail-fast
Easy, fast, neat!
I like this because you don't have to add any dependencies to your project at all, and the whole thing remains very fast. bundle exec slows things down a little, which is what you'd have to do to make sure you're using the same version of rspec all the time. That 0.56 seconds it took to run two tests was 99% taken up by the time it took my computer to load up rspec. Running hundreds of specs should be extremely fast. The only issue you could run into that I'm aware of is if you change versions of rspec and the new version isn't backwards compatible with some function you used in your test, you might have to re-write some tests.
This is nice if you are doing one-off specs or have some good reason to NOT include rspec in your gemspec, however it's not very good for enabling sharing or enforcing compatibility.
This seems right, but doesn't seem to work.
env.rb:
class MyWorld
set :environment, :test
end
app.rb:
configure :development do
DataMapper::setup(:default, "sqlite3://development.sqlite3")
end
configure :test do
DataMapper::setup(:default, "sqlite3://test.sqlite3")
end
It keeps using the development environment. Am I missing something, or am I doing it wrong?
Put this at the top of env.rb, and things work perfectly:
env.rb
ENV['RACK_ENV'] = 'test'
Alternatively, this will do the same without having to edit any files:
$ RACK_ENV=test cucumber features
You might want to look into the cucumber-sinatra gem. It has options to autogenerate a minimal amount of code (including your Sinatra app & rackup file). It should provide the correct syntax for getting cucumber scripts to run in test configuration.