CapyBara Click Link Different Cases and Solutions

Learning with Earning

To click generic link whom text is not multiple times used:

click_link "Help"

To click generic link whom text is multiple times used:
Like show, destroy, edit links on your index page


first(:link, 'Show').click
first(:link, link_name).click

Click Link with id


 all('a').select {|elt| elt.text == "#tag1" }.first.click

OR
Which of the links do you want to click on? If it’s the first (or it doesn’t matter), you can do


find('#tag1').click
Otherwise you can do

all('#tag1')[1].click
to click the second one.

If  you have, nested html and link lies in




 


first('.item').click_link('Agree')

View original post

Advertisements

Cucumber Setup and Excution command

To install Cucumber, first add the cucumber-rails gem and a utility gem called database_cleaner to the :test group in the Gemfile

.
.
group :test do
  .
  .
  .
  gem 'cucumber-rails', '1.4.0', :require => false
  gem 'database_cleaner', github: 'bmabey/database_cleaner'
end
.
.

Then run these two following commands in your project directory through terminal:

  $ bundle install
  $ rails generate cucumber:install

This creates a features/ directory where the files associated with Cucumber will live.

Example cucumber feature:
In features/signing_in.feature

Feature: Signing in

  Scenario: Unsuccessful signin
    Given a user visits the signin page
    When he submits invalid signin information
    Then he should see an error message

  Scenario: Successful signin
    Given a user visits the signin page
      And the user has an account
    When the user submits valid signin information
    Then he should see his profile page
      And he should see a signout link

To run the features, we use the cucumber executable:

  $ bundle exec cucumber features/

OR

  $ bundle exec rake cucumber

Ref: http://ruby.railstutorial.org/chapters/sign-in-sign-out#sec-installation_and_setup

——————————-In progress———————-
Cucumber books:
http://www.amazon.com/gp/product/1934356379
http://www.amazon.com/gp/product/1934356808

Tests inside Sublime Text

Go to your Sublime Text 2 Packages directory:

  OS X: ~/Library/Application\ Support/Sublime\ Text\ 2/Packages
  Windows: %APPDATA%/Sublime Text 2/Packages/
  Linux: ~/.config/sublime-text-2/Packages/

and clone the repository using the command below:

  git clone https://github.com/maltize/sublime-text-2-ruby-tests.git RubyTest

Ref: https://github.com/maltize/sublime-text-2-ruby-tests

After restarting Sublime Text, the RubyTest package supplies the following commands:
Commands:

Command-Shift-R: run a single test (if run on an it block) or group of tests (if run on a describe block)
Command-Shift-E: run the last test(s)
Command-Shift-T: run all the tests in current file
Command-Shift-X: Show test panel (when test panel visible hit esc to hide it)
Alt-Shift-V: Check RB, ERB file syntax

Switching between code and test (create a file if not found):
Command-. : Single View
Command-Ctrl-. : Split View

Eliminate the bundle exec from Execution Command

1st and preffered method is to use RVM 1.11.x or greater.

You can check RVM version using above command:

  rvm -v

also can update:

  rvm get head && rvm reload

Now you can run test cases using

  rspec spec/

and omit the leading bundle exec.

2nd, in case you are restricted to use earlier version of rvm for any reason then:
use RVM Bundler integration to configure the Ruby Version Manager to include the proper executables automatically in the local environment. The steps are simple if somewhat mysterious. First, run these two commands:

  rvm get head && rvm reload
  chmod +x $rvm_path/hooks/after_cd_bundler
  cd ~/rails_project
  bundle install --without production --binstubs=./bundler_stubs

such that rake and rspec are automatically executed in the right environment.
These files are specific to your local setup, you should add the bundler_stubs directory to your .gitignore file

# Ignore other unneeded files.
doc/
*.swp
*~
.project
.DS_Store
bundler_stubs/

If you add another executable (such as guard in Section 3.6.2), you should re-run the bundle install command:

bundle install --binstubs=./bundler_stubs

3rd, If you are not using RVM, you can still avoid typing bundle exec. Bundler allows the creation of the associated binaries as follows:

bundle --binstubs

(In fact, this step, with a different target directory, is also used when using RVM.) This command creates all the necessary executables in the bin/ directory of the application, so that we can now run the test suite as follows:

bin/rspec spec/

The same goes for rake, etc.:

bin/rake db:migrate

If you add another executable (such as guard in Section 3.6.2), you should re-run the bundle –binstubs command.

Steps for TDD setup and execution

1. On creating project tells Rails not to generate a test directory associated with the default Test::Unit framework. we will be using an alternate testing framework called RSpec to write a thorough test suite.

  rails new [project name] --skip-test-unit

2. Add Gems:
Add following gem in development mode so that we have access to RSpec-specific generators, and it includes it in test mode in order to run the tests. We don’t have to install RSpec itself because it is a dependency of rspec-rails and will thus be installed automatically:

  group :development, :test do
    .
    .
    gem 'rspec-rails', '2.11.0'
    .
    .
  end

Include the Capybara gem in test mode, which allows us to simulate a user’s interaction with the application using a natural English-like syntax.

  group :test do
    gem 'capybara', '1.1.2'
  end

To install and include the new gems, we run bundle update and bundle install

  bundle update
  bundle install --without production

3. Next, we need to configure Rails to use RSpec in place of Test::Unit. This can be accomplished with:

  rails generate rspec:install

If your system complains about the lack of a JavaScript runtime, visit the execjs page at GitHub for a list of possibilities. I particularly recommend installing Node.js.

4. Genrate controller with following command to suppress the generation of the default RSpec tests, which we won’t be using. Instead, we’ll create the tests by hand

  rails g controller [controller name] --no-test-framework

5. Generate an integration test (request spec) for our abc pages:

  rails generate integration_test abc_pages

Above command will create file abc_pages_spec.rb in spec/request directory

6. Write test cases in above created file. (how to write test case have a look at: …)

7. Excute Test cases:

  bundle exec rspec spec/requests/abc_pages_spec.rb

to run specific test

 bundle exec rspec spec/requests/abc_pages_spec.rb -e "[test case name]" 

For automate the running of the tests use ‘Guard’ gem.
You can speed up tests execution by using ‘Spork’ gem.