
GKTCS Innovations Pvt. Ltd. Cucumber, Watir Surendra Panpaliya Director, GKTCS Innovations Pvt. Ltd, Pune. 18+ Years of Experience ( MCA, PGDCS, BSc. [Electronics] , CCNA) Director , GKTCS Innovations Pvt. Ltd. Pune [ Nov 2009 – Till date ] IT Manager and Consultant at Rolta India Ltd, Mumbai [ April 2007 – Oct 2009 ] Associate Professor at Sinhgad Institute , Pune [Jan 2002 – April 2007] Instructor , ITM Bangalore [ May 2000 – Jan 2002 ] Project Trainee, DSYS Software Group, Bangalore [ Jan 1999 to April 2000] Skills ❑ Ruby, Rail,Appium, Python, Jython, Django, Android , PHP, LAMP ❑ Cucumber, Rspec, Watir, Pry, Selenium ❑ UNIX /Linux Shell Scripting, System Programming ❑ Data Communication & Networking, CCNA Author of 4 Books National Paper Presentation Awards at BARC Mumbai 2 Agenda ( Day 2) • Introduction to Cucumber • Installation cucumber • cucumber commands • cucumber help • cucumber languages • Given, When, Then, And, Scenario, Background • Gherikin Introduction • Gherkin Syntax • BDD • Calculator Example • cash withdrawal Case Study using Cucumber. • Watir Framework 3 Introduction What is Cucumber? • Cucumber is a software tool that computer programmers use for testing other software. It runs automated acceptance tests written in a behavior-driven development (BDD) style. • Cucumber is written in the Ruby programming language. • Cucumber projects are available for other platforms beyond Ruby. 4 Introduction What is Cucumber? • Cucumber was originally a Ruby tool. • It grew out of the Ruby unit- level BDD framework rspec. • The Ruby version of Cucumber is still the reference implementation and the most widely used, but there are now versions for Java, .NET, JavaScript, as well as several o t h e r l a n g u a g e s a n d platforms. 5 BDD 6 cucumber at a glance 7 cucumber at a glance 8 cucumber Integration 9 Introduction 2. Write a step definition in Ruby 1. Describe behaviour in plain text 3. Run and watch it fail 4. Write code to make the step pass 10 Introduction 5. Run again and see the step pass 6. Repeat 2-5 until green like a cuke 7. Repeat 1-6 until the money runs out Cucumber works with Ruby, Java, .NET, Flex or web applications written in any language. It Cucumber lets software development teams has been translated to over 40 spoken describe how software should behave in plain languages. text. The text is written in a business- readable domain-specific language and serves as documentation, automated tests and development-aid - all rolled into one format. 11 Behavior-driven development It is a software development process that emerged from test-driven development (TDD). It combines the general techniques and principles of TDD with ideas from domain-driven design and object- oriented analysis and design to provide software development and management teams with shared tools and a shared process to collaborate on software development. BDD is principally an idea about how software development should be managed by both business interests and technical insight, the practice of BDD does assume the use of specialised software tools to support the development process. The tools serve to add automation to the ubiquitous language that is a central theme of BDD. 12 Behavior-driven development BDD is largely facilitated through the use of a simple domain specific language (DSL) using natural language constructs (e.g., English-like sentences) that can express the behavior and the expected outcomes. Test scripts have long been a popular application of DSLs with varying degrees of sophistication. DSLs convert structured natural language statements into executable tests. The result is a closer relationship to acceptance criteria for a given function and the tests used to validate that functionality. As such it is a natural extension of TDD testing in general. BDD focused on: Where to start in the process What to test and what not to test How much to test in one go What to call the tests How to understand why a test fails 13 Behavior-driven development At the heart of BDD is a rethinking of the approach to the unit testing and acceptance testing that naturally arise with these issues. For example, BDD suggests that unit test names be whole sentences starting with a conditional verb ("should" in English for example) and should be written in order of business value. Acceptance tests should be written using the standard agile framework of a User story: "As a [role] I want [feature] so that [benefit]". Acceptance criteria should be written in terms of scenarios and implemented as classes: Given [initial context], when [event occurs], then [ensure some outcomes] . 14 Behavior-driven development 15 The TDD Cycle 16 Installation • Cucumber is installed with Ruby's package manager, RubyGems. • Assuming you already have a current version of Ruby to install Cucumber simply open a command window and run. $gem install cucumber • This will install Cucumber along with its dependencies. • If you are using Bundler, just add it to your Gemfile: • group :test do • gem 'cucumber' • end 17 What is Bundler? • Bundler provides a consistent environment for Ruby projects by tracking and installing the exact gems and versions that are needed. • Bundler is an exit from dependency hell, and ensures that the gems you need are present in development, staging, and production. Starting work on a project is as simple as bundle install. • $ gem install bundler • $ bundle install • $ git add Gemfile Gemfile.lock 18 How Cucumber Works? • Cucumber is a command-line tool. When you run it, it reads in your specifications from plain-language text files called features, examines them for scenarios to test, and runs the scenarios against your system. • Each scenario is a list of steps for Cucumber to work through. So that Cucumber can understand these feature files, they must follow some basic syntax rules. • The name for this set of rules is Gherkin. • Along with the features, you give Cucumber a set of step definitions, which map the business-readable language of each step into Ruby code to carry out whatever action is being described by the step. $cucumber • No such file or directory - features. You can use `cucumber --init` to get started. 19 How Cucumber Works? 20 How Cucumber Works? • Surendras-MacBook-Pro:cucumber_ex SurendraMac$ cucumber --init create features create features/step_definitions create features/support create features/support/env.rb Surendras-MacBook-Pro:cucumber_ex SurendraMac$cucumber 0 scenarios 0 steps 0m0.000s Surendras-MacBook-Pro:cucumber_ex SurendraMac$ cucumber --version 2.1.0 Surendras-MacBook-Pro:cucumber_ex SurendraMac$ cucumber --help 21 Creating a Project • SurendraMac$ mkdir calculator • $cucumber calculator • No such file or directory - calculator. You can use `cucumber --init` to get started. • cucumber --init • create features • create features/step_definitions • create features/support • create features/support/env.rb • Surendras-MacBook-Pro:calculator SurendraMac$ 22 Creating a Project SurendraMac$ ls features SurendraMac$ cd features/ :features SurendraMac$ ls -l total 0 drwxr-xr-x 2 SurendraMac staff 68 Nov 28 09:46 step_definitions drwxr-xr-x 3 SurendraMac staff 102 Nov 28 09:46 support 23 Creating a Project #create file adding.feature Feature: Adding Scenario Outline: Add two numbers Given the input "<input>" When the calculator is run Then the output should be "<output>" Examples: | input | output | | 2+2 | 4 | | 98+1 | 99 | 24 step_definitions $cd step_definitions/ #calculator_steps.rb Given /^the input "([^"]*)"$/ do |input| @input = input end When /^the calculator is run$/ do @output = `ruby calc.rb #{@input}` raise('Command failed!') unless $?.success? end Then /^the output should be "([^"]*)"$/ do |expected_output| @output.should == expected_output end Surendras-MacBook-Pro:step_definitions SurendraMac$ 25 calc.rb 5) Create another ruby file in calculator folder >> “calc.rb” #calc.rb print eval(ARGV[0]) • First we read the command-line argument ARGV[0] and send it to Ruby’s eval method. That should be able to work out what to do with the calculation input. Finally, we print the result of eval to the console. • Finally we have completed our first program to run the program go to the terminal • Type cucumber it will display $cucumber and hit enter. 26 Test your First Project Now $cucumber Feature: Adding Scenario Outline: Add two numbers # features/adding.feature:3 Given the input "<input>" # features/adding.feature:4 When the calculator is run # features/adding.feature:5 Then the output should be "<output>" # features/adding.feature:6 Examples: | input | output | |2+2 | 4 | | 98+1 | 99 | 2 scenarios (2 passed) 6 steps (6 passed) 0m0.097s 27 Gherkin Basics Gherkin is the language that Cucumber understands. It is a Business Readable, Domain Specific Language that lets you describe software’s behaviour without detailing how that behaviour is implemented. Gherkin serves two purposes — documentation and automated tests. The third is a bonus feature — when it yells in red it’s talking to you, telling you what code you should write. Gherkin’s grammar is defined in the Treetop grammar that is part of the Cucumber codebase. The grammar exists in different flavours for many spoken languages (37 at the time of writing), so that your team can use the keywords in your own language. There are a few conventions. Single Gherkin source file contains a description of a single feature. Source files have .feature extension. 28 Gherkin Basics Gherkin, the language we use for writing Cucumber features. To write specifications for your software that can be both read by your stakeholders and tested by Cucumber. Learn what each of the Gherkin keywords does and how they all fit together to make readable, executable Cucumber specifications. “The hardest single part of building a software system is deciding precisely what to build.” Better communication between developers and stakeholders is essential to help avoid this kind of wasted time. One technique that really helps facilitate 29 Gherkin Syntax Gherkin is a line-oriented language that uses indentation to define structure. Line endings terminate statements (eg, steps).
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages169 Page
-
File Size-