Ruby onrails cucumber-rspec-capybara
Upcoming SlideShare
Loading in...5
×
 

Ruby onrails cucumber-rspec-capybara

on

  • 1,638 views

 

Statistics

Views

Total Views
1,638
Views on SlideShare
1,624
Embed Views
14

Actions

Likes
2
Downloads
11
Comments
0

1 Embed 14

http://192.168.6.184 14

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Ruby onrails cucumber-rspec-capybara Ruby onrails cucumber-rspec-capybara Presentation Transcript

  • BDD(ROR) using Cucumber/Rspec:acceptance=> :capybara By: Bindesh Vijayan
  • Cucumber● Cucumber is a tool that executes plain-text functional descriptions as automated tests.● While Cucumber can be thought of as a “testing” tool, the intent of the tool is to support BDD● This means that the “tests” (plain text feature descriptions with scenarios) are typically written before anything else and verified by business analysts, domain experts, etc. non technical stakeholders.
  • 4 steps in defining a feature● Cucumber begins by defining a “Feature” that we are trying to implement● After that we specify what we want to do using “In Order”● The we specify a user who will be interacting with that feature using “As a”● And then we finally specify what do we expect using “I want”
  • #Example: user.featureFeature: User login In order to login As a User I want to specify a valid credential and login
  • Defining the scenario for login● After the feature is specified you need to specify “Scenario” for Cucumber● Scenario consists of the following clauses Given When Then
  • Lets extend our user.featureScenario: Logging into thesystem Given when i visit the login page And I enter a valid username and password When I press login button Then I should see a success message
  • User.feature in a nutshellFeature: User login In order to login As a User I want to see a login page with username and passwordScenario: Logging into the system Given when i visit the login page And I enter a valid username and password When I press login button Then I should see a success message
  • Cucumber on terminal● Installing gem install cucumber● Cucumber help cucumber –help● Running cucumber tests cucumber features/user.feature
  • Specifying steps● Once you run cucumber it will report the missing steps with examples of how you can write them● Create a new folder name step_definitions and add a new file called <object>_steps.rb● Copy paste the code from the output● Re run, the Feature would pass but the scenario would fail
  • Cucumber steps● Cucumber internally uses capybara● You can use any other acceptance test framework e.g. Webrat● For this session we will stick to capybara
  • Capybara DSL to simulate user steps● Navigating visit(/projects) visit(post_comments_path(post))● Clicking links and buttons click_link(id-of-link) click_link(Link Text) click_button(Save) click_on(Link Text) # clicks on either links or buttons click_on(Button Value)
  • Interacting with forms fill_in(First Name, :with => John) fill_in(Password, :with => Seekrit) fill_in(Description, :with => Really Long Text...) choose(A Radio Button) check(A Checkbox) uncheck(A Checkbox) attach_file(Image, /path/to/image.jpg) select(Option, :from => Select Box)
  • ● Querying page.has_selector?(table tr) page.has_selector?(:xpath, //table/tr) page.has_xpath?(//table/tr) page.has_css?(table tr.foo) page.has_content?(foo)
  • Scoping● Capybara makes it possible to restrict certain actions, such as interacting with forms or clicking links and buttons, to within a specific area of the page● You can use within DSL to specify a scope e.g. within(“#form-group”)● You can also further restrict it to a table e.g. within_table(“customer-table”)
  • A note about Capybara drivers● Drivers are the internal selectors, if you may want to call it, used by Capybara● By default capybara uses rack_test, which is faster than others● The limitation it doesnt support Javascript and cant access external HTTP resources like remote APIs or Oauth services● You can change the default driver by specifying this line in your helper Capybara.default_driver = :selenium
  • Rspec and Capybara● Instead of using Cucumber you can use Rspec and Capybara● We use the same basic principle that we used with Rspec● Only difference is that you now use capybara dsl insidel your rspec “it” blocks
  • Configuring rspec with capybara#Gem filegroup :development, :test do gem rspec-rails gem capybaraend#spec_helperrequire rspec/rails# Add this to load Capybara integration:require capybara/rspecrequire capybara/rails
  • Examplerequire spec_helperdescribe home page do it welcomes the user do visit / page.shouldhave_content(Welcome) endend