bdd with jbehave and selenium

83
Behaviour Driven Behaviour Driven Development with Development with Java Java Nikolay Vasilev 15 July 2011

Upload: nikolay-vasilev

Post on 27-Nov-2014

37.084 views

Category:

Technology


21 download

DESCRIPTION

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

TRANSCRIPT

Page 1: BDD with JBehave and Selenium

Behaviour Driven Behaviour Driven Development withDevelopment with JavaJava

Nikolay Vasilev15 July 2011

Page 2: BDD with JBehave and Selenium

ContentContentWhat BDD is?JbehaveJBehave and SeleniumJbehave – Pros and ConsQuestions and AnswersReferences

2

Page 3: BDD with JBehave and Selenium

BDDBDD

3

Page 4: BDD with JBehave and Selenium

Test-Driven Development Test-Driven Development (TDD)(TDD)Approach for developing software

by writing tests before writing the code being tested

4

Page 5: BDD with JBehave and Selenium

Test-Driven Development Test-Driven Development (TDD)(TDD)

Facilitates Software Design

Express Software Behaviour

Documents the Code

5

Page 6: BDD with JBehave and Selenium

Unit TestingUnit TestingA form of TDDMeets developer’s expectations

for the code behaviour

Fragile – Too coupled to the implementation

Should meet user’s expectations

6

Page 7: BDD with JBehave and Selenium

User’s ExpectationsUser’s ExpectationsUse Cases

User Stories ◦preferred in Agile projects

Both of them have no validating mechanisms that the code meets user’s expectations

7

Page 8: BDD with JBehave and Selenium

Testing Types and Testing Types and ExpectationsExpectations

8

Application

Levels

Testing Type

Expectations

UI / Presentation / …

Controls / Services / …

Classes / Models / …

Code MeetsCustomer’s

Expectations

Code MeetsProgramme

r’sExpectation

sUnit

Testing

BDD

Selenium

Page 9: BDD with JBehave and Selenium

DDD and Ubiquitous DDD and Ubiquitous LanguageLanguageDomain Driven Design

◦Approach to developing software for complex needs by deeply connecting the implementation to an evolving model of the core business concepts

Ubiquitous Language (UL)◦"A language structured around the

domain model and used by all team members to connect all the activities of the team with the software" – Domain Driven Design by Eric Evans.

9

Page 10: BDD with JBehave and Selenium

Executable Executable DocumentationDocumentationThe system requirements are usually

expressed by textual documentation◦In the ideal world the documentation uses

UL

If this documentation was executable, it would be perfect!

It would assure that the code continues to meet the expectations in long term

10

Page 11: BDD with JBehave and Selenium

Behaviour Driven DesignBehaviour Driven DesignA TDD approach

It is an executable documentation

Helps for UL development and usage

Clears out the misunderstandings between customers, domain experts, developers, QA team etc.

Can be used by everybody involved in a project

11

Page 12: BDD with JBehave and Selenium

BDD as Stories and BDD as Stories and BehaviourBehaviourBDD can be used to express Stories

and BehaviourStories

◦Correspond to User Stories◦Express application behaviour at high

level◦Stories framework needed (eventually)

Behaviour (or Spec)◦Corresponds to expectations at class

level◦Expresses behaviour at

service/component level12

Page 13: BDD with JBehave and Selenium

Stories and BehaviourStories and BehaviourUser Stories

◦Series of acceptance criteria ◦Called scenarios◦Each scenario has givens, events and

outcomes◦Used in Agile projects

Behaviour◦Expressed as a test method◦It defines what the application

should / shouldn’t do13

Page 14: BDD with JBehave and Selenium

A Story StructureA Story StructureNarrative

◦As a [some_role_here]◦I want [some_feature_here]◦So that

[behefit/value_of_the_feature_here]

Scenario(s) (this is acceptance criteria)◦Given [some_initial_condition(s)]◦When [event(s)_occurs]◦Then [ensure_some_outcome(s)]

14

Page 15: BDD with JBehave and Selenium

BDD ProcessBDD Process

15

Exercise expectations

againstMatches to

Customer QADomain Expert Developer

Page 16: BDD with JBehave and Selenium

BDD ProcessBDD Process

16

Page 17: BDD with JBehave and Selenium

BDD ProcessBDD ProcessCreate scenarios for expected

featuresThen follow the process:

◦ For each scenario describing a feature◦ Run the scenario – it fails (go red)◦ Define the first step – go red◦ Write down the application code getting

the step to pass – go green◦ Refactor the code and repeat steps 4 &

5 for each step until 6◦ The scenario passes – go green◦ Refactor the application code

17

Page 18: BDD with JBehave and Selenium

BDD ToolsBDD ToolsC – CspecC++ – CppSpec, Spec-CPP.Net – NBehave, NSpecify, SpecFlowGroovy – GSpec, easyb, Cuke4DukePHP – PHPSpecPython – SpecipyRuby – RSpec, Shoulda, CucumberMore…

18

Page 19: BDD with JBehave and Selenium

BDD Tools (Java Related)BDD Tools (Java Related)JBehave – Java annotations based, Test

frameworks agnosticCuke4duke – Cucumber support for JVMJDave – RSpec (Ruby) inspired, Mojo 2 &

Hamcrest basedbeanSpec – Java basedeasyb – Java based, Specifications

written in Groovyinstinct – BDD framework for Java,

providing annotations for contexts. Inspired by Rspec

BDoc - Extracts behaviour from unit tests

19

Page 20: BDD with JBehave and Selenium

JBehaveJBehave

20

Page 21: BDD with JBehave and Selenium

JBehaveJBehaveOpen Source Java-based BDD

Framework◦Created by Dan North (2003) to

compare BDD to TDD

Current version 3.4.2

Components◦JBehave Main◦JBehave Web

21

Page 22: BDD with JBehave and Selenium

JBehave - FeaturesJBehave - FeaturesPure Java implementationText-based user storiesAnnotation-based binding of textual

steps to Java methodsDI support (Spring, Guice, Pico,

Weld)Story reporting (HTML, TXT, XML)User stories localizationIDE integrationAnt and Maven integrationTest frameworks agnostic (JUnit,

hamcrest, etc.)22

Page 23: BDD with JBehave and Selenium

JBehave Web - FeaturesJBehave Web - FeaturesExtension of JBehave providing

web integration layer for BDDFeatures

◦ JBehave Queue – allows generic stories to be run asynchronously

◦ Web Runner – allows generic stories to be run synchronously

◦ Selenium Integration – automation of stories for web apps using Selenium

23

Page 24: BDD with JBehave and Selenium

JBehave – Five Steps JBehave – Five Steps OverviewOverview

24

1. Write a story2. Map steps to Java3. Configure Stories4. Run Stories5. View Reports

Page 25: BDD with JBehave and Selenium

JBehave – Five Steps JBehave – Five Steps OverviewOverviewStep 1 – Write a story

25

Narrative:In order to calculate BMI with easeAs a doctorI want to have BMI Calculator application

Meta:@author Nikolay Vasilev@bg-jug

Scenario: Simple BMI calculator validation

Given a body mass index calculatorWhen a patient's is with mass 77 kg and height 1.75 mThen patient's body mass index is 25.14285659790039

Page 26: BDD with JBehave and Selenium

JBehave – Five Steps JBehave – Five Steps OverviewOverviewStep 2 – Map steps to Java

26

Page 27: BDD with JBehave and Selenium

JBehave – Five Steps JBehave – Five Steps OverviewOverviewStep 3 – Configure stories

27

Page 28: BDD with JBehave and Selenium

JBehave – Five Steps JBehave – Five Steps OverviewOverviewStep 4 – Run stories with:

28

Page 29: BDD with JBehave and Selenium

JBehave – Five Steps JBehave – Five Steps OverviewOverviewStep 5 – View Reports (HTML,

TXT, XML)

29

Page 30: BDD with JBehave and Selenium

JBehave – Use CaseJBehave – Use CaseBody Mass Index Calculator and

Classifier

Domain Classes

30

Page 31: BDD with JBehave and Selenium

JBehave – Use CaseJBehave – Use CaseBMICalculator component

Computes BMI by the formula:

Where◦BMI – body mass index◦W – a person’s mass in kilograms◦h – a person’s height in meters

31

Page 32: BDD with JBehave and Selenium

JBehave – Use CaseJBehave – Use CaseWeightClassifier component

32

Page 33: BDD with JBehave and Selenium

JBehave StoriesJBehave StoriesPlain-text stories

◦ Consists of narrative and multiple scenarios◦ Usually stored as .story file, but this is

configurableNarrative

◦ Optional◦ As a, In order to, I want to

A scenario consists of:◦ Given, When, Then◦ And maps to any of them◦Possible use of When after Then◦ Scenarios could depend on others scenario

GivenScenariosComments (!--)

33

Page 34: BDD with JBehave and Selenium

JBehave StepsJBehave StepsDeclared as Java annotationsSteps annotations

◦ @Given, @When, @Then

Lifecycle annotations ◦ @BeforeStory, @AfterStory

◦ @BeforeScenario, @AfterScenario

Pending steps◦ @Pending keyword

34

Page 35: BDD with JBehave and Selenium

JBehave StepsJBehave StepsStep annotations take regex

◦See Code ExampleAlias(es) – mapping various text

strings to one method ◦ @Alias, @Aliases

◦See Code ExampleParameter Injection

◦Captured values are converted to method parameters

◦ @Named – Explicit mapping of values to parameters

◦See Code Example35

Page 36: BDD with JBehave and Selenium

JBehave ConfigurationJBehave ConfigurationEverything is configurable (powerful)Everything has defaults (makes life

easy)Main Configuration

◦How to load stories (classpath, file, etc.)◦Story file naming and parsing

configuration◦Error strategy and Pending error strategy

How to handle failure and missing steps

Step Configuration◦Parameter Converters

36

Page 37: BDD with JBehave and Selenium

JBehave ConfigurationJBehave ConfigurationEmbedder and Embeddable

37

Page 38: BDD with JBehave and Selenium

JBehave ConfigurationJBehave ConfigurationJbehave provides two Embeddable

implementations◦ ConfigurableEmbedder

◦ InjectableEmbedder (example AnnotatedEmbedder)

JUnit-enabled Embeddables◦Ran like JUnit tests◦ JUnitStory - one-to-one mapping with

the textual story◦ JUnitStories - many-to-one mapping

Exntending Embedder◦ EclipseEmbedder example

38

Page 39: BDD with JBehave and Selenium

JBehave – AdvancedJBehave – AdvancedParameterized Scenarios

◦ Examples: - required in the story◦Followed by table of parameters◦The mapped parameters should be

surrounded by '<‘ and '>‘ in the storyTable examples require named

parameters for the step candidates to be matched to the Java methods◦The step annotation pattern must hold

the verbatim textual stepThe tables could be loaded from

external resources

39

Page 40: BDD with JBehave and Selenium

JBehave – AdvancedJBehave – AdvancedNon-English KeywordsTabular parameters

◦Multiline values◦Example:

40

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

Page 41: BDD with JBehave and Selenium

JBehave – AdvancedJBehave – AdvancedMeta information

◦ Meta

◦Followed by name value properties separated by @

◦Example:

Meta filtering◦Used for selecting of stories

41

Meta@author Nikolay Vasilev@organization Bulgarian Java Users Group

-author *Vasilev

Page 42: BDD with JBehave and Selenium

JBehave – AdvancedJBehave – AdvancedAnt tasks and Maven pluginIntegration with Spring, Pico, GuiceReporting options

◦Console, Text, HTML, XML◦Default reports location in a Maven

project: ${project.dir}/target/jbehave

◦ExampleTest integration (JUnit and TestNG)

◦See AnnotatedEmbedder example

42

Page 43: BDD with JBehave and Selenium

JBehave Reporting JBehave Reporting OptionsOptionsReports HTML Report

43

Page 44: BDD with JBehave and Selenium

JBehave Reporting JBehave Reporting OptionsOptionsFailing Story HTML Report

44

Page 45: BDD with JBehave and Selenium

JBehave Reporting JBehave Reporting OptionsOptionsPending Steps Story HTML Report

45

Page 46: BDD with JBehave and Selenium

JBehave Reporting JBehave Reporting OptionsOptionsParametrized Story HTML Report

46

Page 47: BDD with JBehave and Selenium

JBehave – Eclipse PluginJBehave – Eclipse PluginHome Page Status: In developmentNon-trivial installation (next slides)Not very rich functionality but

makes the life easierProvides

◦Basic highlighting◦On match, the step in the story is linked

to the mapped method◦Certain autocomplete when writing

stories

47

Page 48: BDD with JBehave and Selenium

JBehave – Eclipse PluginJBehave – Eclipse PluginExample

◦Keywords highlighting◦“Jumping” from story to Steps class◦Autocomplete

48

Page 49: BDD with JBehave and Selenium

JBehave – Install Eclipse JBehave – Install Eclipse PluginPlugin1. Download the plugin

49

Page 50: BDD with JBehave and Selenium

JBehave – Install Eclipse JBehave – Install Eclipse PluginPlugin2. Eclipse > File > Import

50

Page 51: BDD with JBehave and Selenium

JBehave – Install Eclipse JBehave – Install Eclipse PluginPlugin3. Existing Projects into Workspace

51

Page 52: BDD with JBehave and Selenium

JBehave – Install Eclipse JBehave – Install Eclipse PluginPlugin4. Select archive file: > Browse

52

Page 53: BDD with JBehave and Selenium

JBehave – Install Eclipse JBehave – Install Eclipse PluginPlugin5. Select archive file > Open

53

Page 54: BDD with JBehave and Selenium

JBehave – Install Eclipse JBehave – Install Eclipse PluginPlugin6. Select archive file:

54

Page 55: BDD with JBehave and Selenium

JBehave – Install Eclipse JBehave – Install Eclipse PluginPlugin7. Finish

55

Page 56: BDD with JBehave and Selenium

JBehave – Install Eclipse JBehave – Install Eclipse PluginPlugin8. Project Editor > Select the new

project

56

Page 57: BDD with JBehave and Selenium

JBehave – Install Eclipse JBehave – Install Eclipse PluginPlugin9. File > Export

57

Page 58: BDD with JBehave and Selenium

JBehave – Install Eclipse JBehave – Install Eclipse PluginPlugin10.Select > Deployable plug-ins

and frag…

58

Page 59: BDD with JBehave and Selenium

JBehave – Install Eclipse JBehave – Install Eclipse PluginPlugin11.Select > Deployable plug-ins

and frag…

59

Page 60: BDD with JBehave and Selenium

JBehave – Install Eclipse JBehave – Install Eclipse PluginPlugin12.Select the Plug-in & Chose

Install into host

60

Page 61: BDD with JBehave and Selenium

JBehave – Install Eclipse JBehave – Install Eclipse PluginPlugin13.Restart Eclipse (Chose ‘Yes’)

61

Page 62: BDD with JBehave and Selenium

JBehave – Install Eclipse JBehave – Install Eclipse PluginPlugin14.The story files are with different

icon

62

Page 63: BDD with JBehave and Selenium

JBehave – Install Eclipse JBehave – Install Eclipse PluginPlugin15.Not “recognized” stories >

Select + Right Click > Open With > Behavior Editor

63

Page 64: BDD with JBehave and Selenium

JBehave – Install Eclipse JBehave – Install Eclipse PluginPlugin16.Now the keywords are

highlighted

64

Page 65: BDD with JBehave and Selenium

Jbehave and SeleniumJbehave and Selenium

65

Page 66: BDD with JBehave and Selenium

JBehave and SeleniumJBehave and SeleniumSelenium

◦A suite of tools to automate web app testing across many platforms.

◦Abstracts away the interaction with the web layer (browser/server)

66

Java, Perl, C#, C, etc.

Selenium Object

Tested WebApp

Selenium RC

Server

Browsers

Page 67: BDD with JBehave and Selenium

JBehave and SeleniumJBehave and SeleniumSelenium would let us focus on

testing the web-app functionalityAlthough, steps classes get

cluttered by selenium calls

67

Steps Selenium Object

Tested WebApp

Selenium RC

ServerBrowsers

Stories

Page 68: BDD with JBehave and Selenium

JBehave and SeleniumJBehave and SeleniumJBehave and Selenium

◦First thought - Steps will interact with the selenium object from the steps classes

68

Steps Selenium Object

Tested WebApp

Selenium RC

ServerBrowsers

Stories

Page 69: BDD with JBehave and Selenium

JBehave and SeleniumJBehave and SeleniumPage Objects Pattern

◦Page object represent the services offered by a page

◦Hides the knowledge of the page structure from the developer

69

Steps

Selenium Object Tested

WebApp

Selenium RC

ServerBrowsersStories

PageObject

Page 70: BDD with JBehave and Selenium

JBehave and SeleniumJBehave and SeleniumRules of Thumb

◦Keep assertions in the steps mapping classes

◦Keep selenium interaction in the page objects

◦Stories should address the functionality which the pages offer

70

Page 71: BDD with JBehave and Selenium

JBehave and SeleniumJBehave and SeleniumWhat do we need to integrate

JBehave and Selenium◦ SeleniumConfiguration (part of JBehave Web)◦ @BeforeScenario, @AfterScenario steps

Responsible of start/stop selenium object

◦Pages and Steps implementationRunning selenium tests in an

automated way (maven – integration-test phase)

Tip: Configure Firefox for testing with Selenium

71

Page 72: BDD with JBehave and Selenium

Use Case – Web TestingUse Case – Web TestingWeb app for our Use Case

◦2 JSPs – one for main form, another for result

In order to run it are required:◦Run the web app on a web server◦Run the Selenium RC server with the

selenium profile (see tip from prev. slide)

◦Execute WebBmiCalculatorStories embedder as JUnit test

72

Page 73: BDD with JBehave and Selenium

Use Case – Web TestingUse Case – Web TestingMain form

73

Page 74: BDD with JBehave and Selenium

Use Case – Web TestingUse Case – Web TestingResult page

74

Page 75: BDD with JBehave and Selenium

Use Case – Web TestingUse Case – Web TestingThe main form is also error page

75

Page 76: BDD with JBehave and Selenium

JBehave - Pros and ConsJBehave - Pros and Cons

76

Page 77: BDD with JBehave and Selenium

Questions and AnswersQuestions and Answers

77

Page 78: BDD with JBehave and Selenium

Use Case & Examples Use Case & Examples CodeCodeUse Case Code –

◦Consists of 3 Maven 2 projects

jug-bg-bdd-domain

◦Domain Classes of the Applicationjug-bg-bdd-jbehave

◦JBehave Examples (depending on the first project)

jug-bg-bdd-web

◦JBehave & Selenium Examples (depending on the first project)

78

Page 79: BDD with JBehave and Selenium

Use Case & Examples Use Case & Examples CodeCodeSource Code could be found on:

http://code.google.com/p/bg-jug-resource-repository/

79

Page 80: BDD with JBehave and Selenium

ReferencesReferencesBDD WikiJbehaveJBehave TutorialDan North – Introducing BDDDan North - What’s in a Story?[pdf] BDD in Java and Groovy[pdf] TDD Beyond JunitBDD in a Nutshell

80

Page 81: BDD with JBehave and Selenium

ReferencesReferencesRyan Greenhall – BDD by ExampleLocalizing JBehave ScenariosEric Evans - "Domain-Driven Design:

Tackling Complexity in the Heart of Software", Addison-Wesley

JBehave Site :: Dependency InjectionJBehave + Spring + Selenium

Example

81

Page 82: BDD with JBehave and Selenium

ReferencesReferencesSelenium Project Page Objects PatternCreating Firefox Profile for Selenium

RC TestsJBehave Site :: Using SeleniumBDD with JBehave Web 3, Selenium

and Maven 2 for OS X LeopardNice JBehave + Selenium + Maven

ExampleSelenium Tutorial to set up a Selenium

Grid82

Page 83: BDD with JBehave and Selenium

Thank you for your Thank you for your attention!attention!

83