Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Ruby onrails cucumber-rspec-capybara


Published on

  • Be the first to comment

Ruby onrails cucumber-rspec-capybara

  1. 1. BDD(ROR) using Cucumber/Rspec:acceptance=> :capybara By: Bindesh Vijayan
  2. 2. 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.
  3. 3. 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”
  4. 4. #Example: user.featureFeature: User login In order to login As a User I want to specify a valid credential and login
  5. 5. 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
  6. 6. 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
  7. 7. 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
  8. 8. Cucumber on terminal● Installing gem install cucumber● Cucumber help cucumber –help● Running cucumber tests cucumber features/user.feature
  9. 9. 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
  10. 10. 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
  11. 11. 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)
  12. 12. 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)
  13. 13. ● Querying page.has_selector?(table tr) page.has_selector?(:xpath, //table/tr) page.has_xpath?(//table/tr) page.has_css?(table page.has_content?(foo)
  14. 14. 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”)
  15. 15. 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
  16. 16. 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
  17. 17. 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
  18. 18. Examplerequire spec_helperdescribe home page do it welcomes the user do visit / page.shouldhave_content(Welcome) endend