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.

BDD with JBehave and Selenium

Presents BDD concepts and procedes along with introduction in JBehave as BDD implementation for Java.

  • Be the first to comment

BDD with JBehave and Selenium

  1. 1. Behaviour Driven Development with Java Nikolay Vasilev 15 July 2011
  2. 2. Content <ul><li>What BDD is? </li></ul><ul><li>Jbehave </li></ul><ul><li>JBehave and Selenium </li></ul><ul><li>Jbehave – Pros and Cons </li></ul><ul><li>Questions and Answers </li></ul><ul><li>References </li></ul>
  3. 3. BDD
  4. 4. Test-Driven Development (TDD) <ul><li>Approach for developing software by writing tests before writing the code being tested </li></ul>
  5. 5. Test-Driven Development (TDD) <ul><li>Facilitates Software Design </li></ul><ul><li>Express Software Behaviour </li></ul><ul><li>Documents the Code </li></ul>
  6. 6. Unit Testing <ul><li>A form of TDD </li></ul><ul><li>Meets developer’s expectations for the code behaviour </li></ul><ul><li>Fragile – Too coupled to the implementation </li></ul><ul><li>Should meet user’s expectations </li></ul>
  7. 7. User’s Expectations <ul><li>Use Cases </li></ul><ul><li>User Stories </li></ul><ul><ul><li>preferred in Agile projects </li></ul></ul><ul><li>Both of them have no validating mechanisms that the code meets user’s expectations </li></ul>
  8. 8. Testing Types and Expectations Application Levels Testing Type Expectations UI / Presentation / … Controls / Services / … Classes / Models / … Code Meets Customer’s Expectations Code Meets Programmer’s Expectations Unit Testing BDD Selenium
  9. 9. DDD and Ubiquitous Language <ul><li>Domain Driven Design </li></ul><ul><ul><li>Approach to developing software for complex needs by deeply connecting the implementation to an evolving model of the core business concepts </li></ul></ul><ul><li>Ubiquitous Language (UL) </li></ul><ul><ul><li>&quot; A language structured around the domain model and used by all team members to connect all the activities of the team with the software &quot; – Domain Driven Design by Eric Evans. </li></ul></ul>
  10. 10. Executable Documentation <ul><li>The system requirements are usually expressed by textual documentation </li></ul><ul><ul><li>In the ideal world the documentation uses UL </li></ul></ul><ul><li>If this documentation was executable, it would be perfect! </li></ul><ul><li>It would assure that the code continues to meet the expectations in long term </li></ul>
  11. 11. Behaviour Driven Design <ul><li>A TDD approach </li></ul><ul><li>It is an executable documentation </li></ul><ul><li>Helps for UL development and usage </li></ul><ul><li>Clears out the misunderstandings between customers, domain experts, developers, QA team etc. </li></ul><ul><li>Can be used by everybody involved in a project </li></ul>
  12. 12. BDD as Stories and Behaviour <ul><li>BDD can be used to express Stories and Behaviour </li></ul><ul><li>Stories </li></ul><ul><ul><li>Correspond to User Stories </li></ul></ul><ul><ul><li>Express application behaviour at high level </li></ul></ul><ul><ul><li>Stories framework needed (eventually) </li></ul></ul><ul><li>Behaviour (or Spec) </li></ul><ul><ul><li>Corresponds to expectations at class level </li></ul></ul><ul><ul><li>Expresses behaviour at service/component level </li></ul></ul>
  13. 13. Stories and Behaviour <ul><li>User Stories </li></ul><ul><ul><li>Series of acceptance criteria </li></ul></ul><ul><ul><li>Called scenarios </li></ul></ul><ul><ul><li>Each scenario has givens , events and outcomes </li></ul></ul><ul><ul><li>Used in Agile projects </li></ul></ul><ul><li>Behaviour </li></ul><ul><ul><li>Expressed as a test method </li></ul></ul><ul><ul><li>It defines what the application should / shouldn’t do </li></ul></ul>
  14. 14. A Story Structure <ul><li>Narrative </li></ul><ul><ul><li>As a [some_role_here] </li></ul></ul><ul><ul><li>I want [some_feature_here] </li></ul></ul><ul><ul><li>So that [behefit/value_of_the_feature_here] </li></ul></ul><ul><li>Scenario(s) (this is acceptance criteria) </li></ul><ul><ul><li>Given [some_initial_condition(s)] </li></ul></ul><ul><ul><li>When [event(s)_occurs] </li></ul></ul><ul><ul><li>Then [ensure_some_outcome(s)] </li></ul></ul>
  15. 15. BDD Process Exercise expectations against Matches to Customer QA Domain Expert Developer
  16. 16. BDD Process
  17. 17. BDD Process <ul><li>Create scenarios for expected features </li></ul><ul><li>Then follow the process: </li></ul><ul><ul><li>For each scenario describing a feature </li></ul></ul><ul><ul><li>Run the scenario – it fails (go red ) </li></ul></ul><ul><ul><li>Define the first step – go red </li></ul></ul><ul><ul><li>Write down the application code getting the step to pass – go green </li></ul></ul><ul><ul><li>Refactor  the code and repeat steps 4 & 5 for each step until 6 </li></ul></ul><ul><ul><li>The scenario passes – go green </li></ul></ul><ul><ul><li>Refactor  the application code </li></ul></ul>
  18. 18. BDD Tools <ul><li>C – Cspec </li></ul><ul><li>C++ – CppSpec, Spec-CPP </li></ul><ul><li>.Net – NBehave, NSpecify, SpecFlow </li></ul><ul><li>Groovy – GSpec, easyb, Cuke4Duke </li></ul><ul><li>PHP – PHPSpec </li></ul><ul><li>Python – Specipy </li></ul><ul><li>Ruby – RSpec, Shoulda, Cucumber </li></ul><ul><li>More… </li></ul>
  19. 19. BDD Tools (Java Related) <ul><li>JBehave – Java annotations based, Test frameworks agnostic </li></ul><ul><li>Cuke4duke – Cucumber support for JVM </li></ul><ul><li>JDave – RSpec (Ruby) inspired, Mojo 2 & Hamcrest based </li></ul><ul><li>beanSpec – Java based </li></ul><ul><li>easyb – Java based, Specifications written in Groovy </li></ul><ul><li>instinct – BDD framework for Java, providing annotations for contexts. Inspired by Rspec </li></ul><ul><li>BDoc - Extracts behaviour from unit tests </li></ul>
  20. 20. JBehave
  21. 21. JBehave <ul><li>Open Source Java-based BDD Framework </li></ul><ul><ul><li>Created by Dan North (2003) to compare BDD to TDD </li></ul></ul><ul><li>Current version 3.4.2 </li></ul><ul><li>Components </li></ul><ul><ul><li>JBehave Main </li></ul></ul><ul><ul><li>JBehave Web </li></ul></ul>
  22. 22. JBehave - Features <ul><li>Pure Java implementation </li></ul><ul><li>Text-based user stories </li></ul><ul><li>Annotation-based binding of textual steps to Java methods </li></ul><ul><li>DI support (Spring, Guice, Pico, Weld) </li></ul><ul><li>Story reporting (HTML, TXT, XML) </li></ul><ul><li>User stories localization </li></ul><ul><li>IDE integration </li></ul><ul><li>Ant and Maven integration </li></ul><ul><li>Test frameworks agnostic (JUnit, hamcrest, etc.) </li></ul>
  23. 23. JBehave Web - Features <ul><li>Extension of JBehave providing web integration layer for BDD </li></ul><ul><li>Features </li></ul><ul><ul><li>JBehave Queue – allows generic stories to be run asynchronously </li></ul></ul><ul><ul><li>Web Runner – allows generic stories to be run synchronously </li></ul></ul><ul><ul><li>Selenium Integration – automation of stories for web apps using Selenium </li></ul></ul>
  24. 24. JBehave – Five Steps Overview <ul><li>Write a story </li></ul><ul><li>Map steps to Java </li></ul><ul><li>Configure Stories </li></ul><ul><li>Run Stories </li></ul><ul><li>View Reports </li></ul>
  25. 25. JBehave – Five Steps Overview <ul><li>Step 1 – Write a story </li></ul>Narrative : In order to calculate BMI with ease As a doctor I want to have BMI Calculator application Meta : @author Nikolay Vasilev @bg-jug Scenario : Simple BMI calculator validation Given a body mass index calculator When a patient's is with mass 77 kg and height 1.75 m Then patient's body mass index is 25.14285659790039
  26. 26. JBehave – Five Steps Overview <ul><li>Step 2 – Map steps to Java </li></ul>
  27. 27. JBehave – Five Steps Overview <ul><li>Step 3 – Configure stories </li></ul>
  28. 28. JBehave – Five Steps Overview <ul><li>Step 4 – Run stories with: </li></ul>
  29. 29. JBehave – Five Steps Overview <ul><li>Step 5 – View Reports (HTML, TXT, XML) </li></ul>
  30. 30. JBehave – Use Case <ul><li>Body Mass Index Calculator and Classifier </li></ul><ul><li>Domain Classes </li></ul>
  31. 31. JBehave – Use Case <ul><li>BMICalculator component </li></ul><ul><li>Computes BMI by the formula: </li></ul><ul><li>Where </li></ul><ul><ul><li>BMI – body mass index </li></ul></ul><ul><ul><li>W – a person’s mass in kilograms </li></ul></ul><ul><ul><li>h – a person’s height in meters </li></ul></ul>
  32. 32. JBehave – Use Case <ul><li>WeightClassifier component </li></ul>
  33. 33. JBehave Stories <ul><li>Plain-text stories </li></ul><ul><ul><li>Consists of narrative and multiple scenarios </li></ul></ul><ul><ul><li>Usually stored as .story file, but this is configurable </li></ul></ul><ul><li>Narrative </li></ul><ul><ul><li>Optional </li></ul></ul><ul><ul><li>As a, In order to, I want to </li></ul></ul><ul><li>A scenario consists of: </li></ul><ul><ul><li>Given, When, Then </li></ul></ul><ul><ul><li>And maps to any of them </li></ul></ul><ul><ul><li>Possible use of When after Then </li></ul></ul><ul><ul><li>Scenarios could depend on others scenario </li></ul></ul><ul><ul><ul><li>GivenScenarios </li></ul></ul></ul><ul><li>Comments ( !-- ) </li></ul>
  34. 34. JBehave Steps <ul><li>Declared as Java annotations </li></ul><ul><li>Steps annotations </li></ul><ul><ul><li>@Given, @When, @Then </li></ul></ul><ul><li>Lifecycle annotations </li></ul><ul><ul><li>@BeforeStory, @AfterStory </li></ul></ul><ul><ul><li>@BeforeScenario, @AfterScenario </li></ul></ul><ul><li>Pending steps </li></ul><ul><ul><li>@Pending keyword </li></ul></ul>
  35. 35. JBehave Steps <ul><li>Step annotations take regex </li></ul><ul><ul><li>See Code Example </li></ul></ul><ul><li>Alias(es) – mapping various text strings to one method </li></ul><ul><ul><li>@Alias, @Aliases </li></ul></ul><ul><ul><li>See Code Example </li></ul></ul><ul><li>Parameter Injection </li></ul><ul><ul><li>Captured values are converted to method parameters </li></ul></ul><ul><ul><li>@Named – Explicit mapping of values to parameters </li></ul></ul><ul><ul><li>See Code Example </li></ul></ul>
  36. 36. JBehave Configuration <ul><li>Everything is configurable (powerful) </li></ul><ul><li>Everything has defaults (makes life easy) </li></ul><ul><li>Main Configuration </li></ul><ul><ul><li>How to load stories (classpath, file, etc.) </li></ul></ul><ul><ul><li>Story file naming and parsing configuration </li></ul></ul><ul><ul><li>Error strategy and Pending error strategy </li></ul></ul><ul><ul><ul><li>How to handle failure and missing steps </li></ul></ul></ul><ul><li>Step Configuration </li></ul><ul><ul><li>Parameter Converters </li></ul></ul>
  37. 37. JBehave Configuration <ul><li>Embedder and Embeddable </li></ul>
  38. 38. JBehave Configuration <ul><li>Jbehave provides two Embeddable implementations </li></ul><ul><ul><li>ConfigurableEmbedder </li></ul></ul><ul><ul><li>InjectableEmbedder ( example AnnotatedEmbedder ) </li></ul></ul><ul><li>JUnit-enabled Embeddables </li></ul><ul><ul><li>Ran like JUnit tests </li></ul></ul><ul><ul><li>JUnitStory - one-to-one mapping with the textual story </li></ul></ul><ul><ul><li>JUnitStories - many-to-one mapping </li></ul></ul><ul><li>Exntending Embedder </li></ul><ul><ul><li>EclipseEmbedder example </li></ul></ul>
  39. 39. JBehave – Advanced <ul><li>Parameterized Scenarios </li></ul><ul><ul><li>Examples: - required in the story </li></ul></ul><ul><ul><li>Followed by table of parameters </li></ul></ul><ul><ul><li>The mapped parameters should be surrounded by ' < ‘ and ' > ‘ in the story </li></ul></ul><ul><li>Table examples require named parameters for the step candidates to be matched to the Java methods </li></ul><ul><ul><li>The step annotation pattern must hold the verbatim textual step </li></ul></ul><ul><li>The tables could be loaded from external resources </li></ul>
  40. 40. JBehave – Advanced <ul><li>Non-English Keywords </li></ul><ul><li>Tabular parameters </li></ul><ul><ul><li>Multiline values </li></ul></ul><ul><ul><li>Example : </li></ul></ul>Given tab e|--------------------------------- B|--------------------------------- G|--------------------------------- D|----------0--0------------------- A|-0--2--3----------2--0-----0--0– E|------------------------3-------- When the guitar plays Then the following notes will be played A0 A2 A3 D0 D0 A2 A0 E3 A0 A0
  41. 41. JBehave – Advanced <ul><li>Meta information </li></ul><ul><ul><li>Meta </li></ul></ul><ul><ul><li>Followed by name value properties separated by @ </li></ul></ul><ul><ul><li>Example: </li></ul></ul><ul><li>Meta filtering </li></ul><ul><ul><li>Used for selecting of stories </li></ul></ul>Meta @author Nikolay Vasilev @organization Bulgarian Java Users Group -author *Vasilev
  42. 42. JBehave – Advanced <ul><li>Ant tasks and Maven plugin </li></ul><ul><li>Integration with Spring, Pico, Guice </li></ul><ul><li>Reporting options </li></ul><ul><ul><li>Console, Text, HTML, XML </li></ul></ul><ul><ul><li>Default reports location in a Maven project: </li></ul></ul><ul><ul><ul><li>${project.dir}/target/jbehave </li></ul></ul></ul><ul><ul><li>Example </li></ul></ul><ul><li>Test integration (JUnit and TestNG) </li></ul><ul><ul><li>See AnnotatedEmbedder example </li></ul></ul>
  43. 43. JBehave Reporting Options <ul><li>Reports HTML Report </li></ul>
  44. 44. JBehave Reporting Options <ul><li>Failing Story HTML Report </li></ul>
  45. 45. JBehave Reporting Options <ul><li>Pending Steps Story HTML Report </li></ul>
  46. 46. JBehave Reporting Options <ul><li>Parametrized Story HTML Report </li></ul>
  47. 47. JBehave – Eclipse Plugin <ul><li>Home Page </li></ul><ul><li>Status: In development </li></ul><ul><li>Non-trivial installation (next slides) </li></ul><ul><li>Not very rich functionality but makes the life easier </li></ul><ul><li>Provides </li></ul><ul><ul><li>Basic highlighting </li></ul></ul><ul><ul><li>On match, the step in the story is linked to the mapped method </li></ul></ul><ul><ul><li>Certain autocomplete when writing stories </li></ul></ul>
  48. 48. JBehave – Eclipse Plugin <ul><li>Example </li></ul><ul><ul><li>Keywords highlighting </li></ul></ul><ul><ul><li>“ Jumping” from story to Steps class </li></ul></ul><ul><ul><li>Autocomplete </li></ul></ul>
  49. 49. JBehave – Install Eclipse Plugin <ul><li>Download the plugin </li></ul>
  50. 50. JBehave – Install Eclipse Plugin <ul><li>Eclipse > File > Import </li></ul>
  51. 51. JBehave – Install Eclipse Plugin <ul><li>Existing Projects into Workspace </li></ul>
  52. 52. JBehave – Install Eclipse Plugin <ul><li>Select archive file: > Browse </li></ul>
  53. 53. JBehave – Install Eclipse Plugin <ul><li>Select archive file > Open </li></ul>
  54. 54. JBehave – Install Eclipse Plugin <ul><li>Select archive file: </li></ul>
  55. 55. JBehave – Install Eclipse Plugin <ul><li>Finish </li></ul>
  56. 56. JBehave – Install Eclipse Plugin <ul><li>Project Editor > Select the new project </li></ul>
  57. 57. JBehave – Install Eclipse Plugin <ul><li>File > Export </li></ul>
  58. 58. JBehave – Install Eclipse Plugin <ul><li>Select > Deployable plug-ins and frag… </li></ul>
  59. 59. JBehave – Install Eclipse Plugin <ul><li>Select > Deployable plug-ins and frag… </li></ul>
  60. 60. JBehave – Install Eclipse Plugin <ul><li>Select the Plug-in & Chose Install into host </li></ul>
  61. 61. JBehave – Install Eclipse Plugin <ul><li>Restart Eclipse (Chose ‘Yes’) </li></ul>
  62. 62. JBehave – Install Eclipse Plugin <ul><li>The story files are with different icon  </li></ul>
  63. 63. JBehave – Install Eclipse Plugin <ul><li>Not “recognized” stories > Select + Right Click > Open With > Behavior Editor </li></ul>
  64. 64. JBehave – Install Eclipse Plugin <ul><li>Now the keywords are highlighted </li></ul>
  65. 65. Jbehave and Selenium
  66. 66. JBehave and Selenium <ul><li>Selenium </li></ul><ul><ul><li>A suite of tools to automate web app testing across many platforms. </li></ul></ul><ul><ul><li>Abstracts away the interaction with the web layer (browser/server) </li></ul></ul>Java, Perl, C#, C, etc. Selenium Object Tested WebApp Selenium RC Server Browsers
  67. 67. JBehave and Selenium <ul><li>Selenium would let us focus on testing the web-app functionality </li></ul><ul><li>Although, steps classes get cluttered by selenium calls </li></ul>Steps Selenium Object Tested WebApp Selenium RC Server Browsers Stories
  68. 68. JBehave and Selenium <ul><li>JBehave and Selenium </li></ul><ul><ul><li>First thought - Steps will interact with the selenium object from the steps classes </li></ul></ul>Steps Selenium Object Tested WebApp Selenium RC Server Browsers Stories
  69. 69. JBehave and Selenium <ul><li>Page Objects Pattern </li></ul><ul><ul><li>Page object represent the services offered by a page </li></ul></ul><ul><ul><li>Hides the knowledge of the page structure from the developer </li></ul></ul>Steps Selenium Object Tested WebApp Selenium RC Server Browsers Stories Page Object
  70. 70. JBehave and Selenium <ul><li>Rules of Thumb </li></ul><ul><ul><li>Keep assertions in the steps mapping classes </li></ul></ul><ul><ul><li>Keep selenium interaction in the page objects </li></ul></ul><ul><ul><li>Stories should address the functionality which the pages offer </li></ul></ul>
  71. 71. JBehave and Selenium <ul><li>What do we need to integrate JBehave and Selenium </li></ul><ul><ul><li>SeleniumConfiguration (part of JBehave Web) </li></ul></ul><ul><ul><li>@BeforeScenario, @AfterScenario steps </li></ul></ul><ul><ul><ul><li>Responsible of start/stop selenium object </li></ul></ul></ul><ul><ul><li>Pages and Steps implementation </li></ul></ul><ul><li>Running selenium tests in an automated way (maven – integration-test phase) </li></ul><ul><li>Tip: Configure Firefox for testing with Selenium </li></ul>
  72. 72. Use Case – Web Testing <ul><li>Web app for our Use Case </li></ul><ul><ul><li>2 JSPs – one for main form, another for result </li></ul></ul><ul><li>In order to run it are required: </li></ul><ul><ul><li>Run the web app on a web server </li></ul></ul><ul><ul><li>Run the Selenium RC server with the selenium profile (see tip from prev. slide) </li></ul></ul><ul><ul><li>Execute WebBmiCalculatorStories embedder as JUnit test </li></ul></ul>
  73. 73. Use Case – Web Testing <ul><li>Main form </li></ul>
  74. 74. Use Case – Web Testing <ul><li>Result page </li></ul>
  75. 75. Use Case – Web Testing <ul><li>The main form is also error page </li></ul>
  76. 76. JBehave - Pros and Cons
  77. 77. Questions and Answers
  78. 78. Use Case & Examples Code <ul><li>Use Case Code – </li></ul><ul><ul><li>Consists of 3 Maven 2 projects </li></ul></ul><ul><li>jug-bg-bdd-domain </li></ul><ul><ul><li>Domain Classes of the Application </li></ul></ul><ul><ul><li>jug-bg-bdd-jbehave </li></ul></ul><ul><ul><li>JBehave Examples (depending on the first project) </li></ul></ul><ul><ul><li>jug-bg-bdd-web </li></ul></ul><ul><ul><li>JBehave & Selenium Examples (depending on the first project) </li></ul></ul>
  79. 79. Use Case & Examples Code <ul><li>Source Code could be found on: </li></ul><ul><ul><li> </li></ul></ul>
  80. 80. References <ul><li>BDD Wiki </li></ul><ul><li>Jbehave </li></ul><ul><li>JBehave Tutorial </li></ul><ul><li>Dan North – Introducing BDD </li></ul><ul><li>Dan North - What’s in a Story? </li></ul><ul><li>[pdf] BDD in Java and Groovy </li></ul><ul><li>[pdf] TDD Beyond Junit </li></ul><ul><li>BDD in a Nutshell </li></ul>
  81. 81. References <ul><li>Ryan Greenhall – BDD by Example </li></ul><ul><li>Localizing JBehave Scenarios </li></ul><ul><li>Eric Evans - &quot;Domain-Driven Design: Tackling Complexity in the Heart of Software&quot;, Addison-Wesley </li></ul><ul><li>JBehave Site :: Dependency Injection </li></ul><ul><li>JBehave + Spring + Selenium Example </li></ul>
  82. 82. References <ul><li>Selenium Project </li></ul><ul><li>Page Objects Pattern </li></ul><ul><li>Creating Firefox Profile for Selenium RC Tests </li></ul><ul><li>JBehave Site :: Using Selenium </li></ul><ul><li>BDD with JBehave Web 3, Selenium and Maven 2 for OS X Leopard </li></ul><ul><li>Nice JBehave + Selenium + Maven Example </li></ul><ul><li>Selenium Tutorial to set up a Selenium Grid </li></ul>
  83. 83. Thank you for your attention!

    Be the first to comment

    Login to see the comments

  • rohitprajapati0307

    Mar. 7, 2017
  • leandror

    Mar. 8, 2017
  • DARSHU3108

    Mar. 12, 2017
  • DARSHU3108

    Mar. 12, 2017
  • nandinirajaram5

    Apr. 8, 2017
  • BaskaranKasinathaBoo1

    Jun. 10, 2017
  • FrantiekNmeek

    Jul. 17, 2017
  • arpinarula

    Aug. 4, 2017
  • MukulKhatri2

    Aug. 25, 2017
  • realzt7

    Sep. 8, 2017
  • ChemikalaUmamaheswar

    Sep. 19, 2017
  • udayvangala

    Dec. 17, 2017
  • murenfeng

    Dec. 17, 2017
  • MarioNady

    Jan. 17, 2018
  • amitrajpurkar

    Jan. 30, 2018
  • mailtorohitverma

    Mar. 17, 2018
  • SathyanG1

    Mar. 21, 2018
  • karthickharish

    Apr. 4, 2018
  • GuillermoMiguelDiazO

    May. 3, 2018
  • anjdee

    Aug. 30, 2018

Presents BDD concepts and procedes along with introduction in JBehave as BDD implementation for Java.


Total views


On Slideshare


From embeds


Number of embeds