Web Automation in BDD
SpecFlow + WatiN
What about me?
2
What about you?
• Who is doing test automation?
• Who is using open source Selenium or WatiN?
• Who is practicing BDD with SpecFlow or
Cucumber or other BDD tool?
3
Agenda
• BDD Introduction
• Demo
• BDD Test Framework (SpecFlow + WatiN)
• Test design patterns/practices
4
Behaviour Driven Development
 One of many agile development techniques
 Specification driven by examples
 Clear communication between domain experts, developers, testers and
customers
BDD is an agile software development technique that
encourages collaboration between developers, QA, and non-
technical or business participants in a software project. It's
more about business specifications than about tests. You
write a specification for a story and verify whether the specs
work as expected.
5
BDD Examples
Feature: Serve coffee
In order to earn money
Customers should be able to
buy coffee at all times
Scenario: Buy last coffee
Given there are 1 coffee left in the machine
And I have deposited 1$
When I press the coffee button
Then I should be served a coffee
[Test]
public void TestCoffeeMachine()
{
// Some test codes here
}
BDD vs TDD
BDD Tools
BDD Languages - Gherkin
• Domain Specific Language
• Easy to understand by customers
• Simple with few keywords
– Feature
– Scenario, Scenario Outline
– Given, When, Then
• Localized to 35+ languages
Scenario: GIVEN, WHEN, THEN
10
BDD Framework
11
How SpecFlow Works?
Drives
GUI
• Demo 1: Coffee Machine
13
• Demo 2: Google Search (Compare with WatiN Hello World example)
14
SpecFlow - Step Argument
• Step definition: use regular expression to eliminate duplicate script
15
SpecFlow - Table argument
16
SpecFlow - Context
17
Data Sharing between steps/scenarios
SpecFlow - Test Fixture
• BeforeFeature/BeforeScenario: Precondition of each
feature/scenario
• AfterFeature/AfterScenario: Teardown or cleanup after test
execution
• Tags
SpecFlow - Scenario Outline (Data Driven)
Scenario: TC1 Add two numbers
Given I have entered 1 into the calculator
And I have entered 2 into the calculator
When I press add
Then the result should be 3 on the screen
Scenario: TC2 Add two numbers
Given I have entered 2 into the calculator
And I have entered 2 into the calculator
When I press add
Then the result should be 4 on the screen
Data driven on scenarios.
From framework to platform
• Test Report/Test Log
21
Nunit
TestResults.txt
TestResults.xml
SpecificationResults.html
SpecFlow
• Test environment
• Continuous build
Patterns and Practices
• Domain Driven Design
• Singleton
• Page object pattern
• Driver pattern
• Reusable modules
22
Domain Driven Design
• Express your tests in the language of the end-user of the app.
23


Web Browser in Singleton Mode
public static class WebBrowser
{
public static IE Current
{
get {
if (!ScenarioContext.Current.ContainsKey("browser"))
ScenarioContext.Current["browser"] = new IE();
return ScenarioContext.Current["browser"] as IE;
}
}
}
24
Page Object Pattern
• A simple abstraction of the UI of your web app.
25
Driver Pattern
• Driver object defines all actions/events among
current page
26
Reusable Modules
27
Summary
• BDD Concept
• SpecFlow Test Framework
• Web Automation Practice
28
The framework and pattern matters, not the tools 
Reference
• Specification by Examples - http://specificationbyexample.com/
• MSDN article on BDD: http://msdn.microsoft.com/en-
us/magazine/gg490346.aspx
• BDD from NDC: http://vimeo.com/43536443
• WATIN and more: http://watinandmore.blogspot.com/
29
Questions? 
Suggestions? 
Thank you! 
- 余宗宝(@Sandyshow)
zbyu81@hotmail.com

Web automation in BDD

  • 1.
    Web Automation inBDD SpecFlow + WatiN
  • 2.
  • 3.
    What about you? •Who is doing test automation? • Who is using open source Selenium or WatiN? • Who is practicing BDD with SpecFlow or Cucumber or other BDD tool? 3
  • 4.
    Agenda • BDD Introduction •Demo • BDD Test Framework (SpecFlow + WatiN) • Test design patterns/practices 4
  • 5.
    Behaviour Driven Development One of many agile development techniques  Specification driven by examples  Clear communication between domain experts, developers, testers and customers BDD is an agile software development technique that encourages collaboration between developers, QA, and non- technical or business participants in a software project. It's more about business specifications than about tests. You write a specification for a story and verify whether the specs work as expected. 5
  • 6.
    BDD Examples Feature: Servecoffee In order to earn money Customers should be able to buy coffee at all times Scenario: Buy last coffee Given there are 1 coffee left in the machine And I have deposited 1$ When I press the coffee button Then I should be served a coffee [Test] public void TestCoffeeMachine() { // Some test codes here }
  • 7.
  • 8.
  • 9.
    BDD Languages -Gherkin • Domain Specific Language • Easy to understand by customers • Simple with few keywords – Feature – Scenario, Scenario Outline – Given, When, Then • Localized to 35+ languages
  • 10.
  • 11.
  • 12.
  • 13.
    • Demo 1:Coffee Machine 13
  • 14.
    • Demo 2:Google Search (Compare with WatiN Hello World example) 14
  • 15.
    SpecFlow - StepArgument • Step definition: use regular expression to eliminate duplicate script 15
  • 16.
    SpecFlow - Tableargument 16
  • 17.
    SpecFlow - Context 17 DataSharing between steps/scenarios
  • 18.
    SpecFlow - TestFixture • BeforeFeature/BeforeScenario: Precondition of each feature/scenario • AfterFeature/AfterScenario: Teardown or cleanup after test execution • Tags
  • 19.
    SpecFlow - ScenarioOutline (Data Driven) Scenario: TC1 Add two numbers Given I have entered 1 into the calculator And I have entered 2 into the calculator When I press add Then the result should be 3 on the screen Scenario: TC2 Add two numbers Given I have entered 2 into the calculator And I have entered 2 into the calculator When I press add Then the result should be 4 on the screen
  • 20.
    Data driven onscenarios.
  • 21.
    From framework toplatform • Test Report/Test Log 21 Nunit TestResults.txt TestResults.xml SpecificationResults.html SpecFlow • Test environment • Continuous build
  • 22.
    Patterns and Practices •Domain Driven Design • Singleton • Page object pattern • Driver pattern • Reusable modules 22
  • 23.
    Domain Driven Design •Express your tests in the language of the end-user of the app. 23  
  • 24.
    Web Browser inSingleton Mode public static class WebBrowser { public static IE Current { get { if (!ScenarioContext.Current.ContainsKey("browser")) ScenarioContext.Current["browser"] = new IE(); return ScenarioContext.Current["browser"] as IE; } } } 24
  • 25.
    Page Object Pattern •A simple abstraction of the UI of your web app. 25
  • 26.
    Driver Pattern • Driverobject defines all actions/events among current page 26
  • 27.
  • 28.
    Summary • BDD Concept •SpecFlow Test Framework • Web Automation Practice 28 The framework and pattern matters, not the tools 
  • 29.
    Reference • Specification byExamples - http://specificationbyexample.com/ • MSDN article on BDD: http://msdn.microsoft.com/en- us/magazine/gg490346.aspx • BDD from NDC: http://vimeo.com/43536443 • WATIN and more: http://watinandmore.blogspot.com/ 29
  • 30.
  • 31.
    Thank you!  -余宗宝(@Sandyshow) zbyu81@hotmail.com

Editor's Notes

  • #3 Thank you ChinaTest committee and all comings
  • #4 Thank you ChinaTest committee and all comings
  • #8 Why BDD?
  • #10 Together with demo to introduce in parallel
  • #19 Question: How to handle Login in your system?
  • #24 Express your tests in the language of the end-user of the our product/softwareBusiness level scenarios: To allow the creation of test scenarios ahead of the SUT delivery.Easy to maintain, less dependent on GUI changes.(domain-specific language, domain driven design....)Not functional point test: xxx dialog, xxx input box1. What must be automated?2. What should be automated?3. What could be automated?4. What won’t be automated?This method allowed us to focus our efforts on those test components that werenecessary and would give the greatest value by assessing the associated business risk,reusability, usage, complexity, and feasibility of each test component
  • #25 1. In order to walk the browser through several steps in a scenario, you need a way to pass the same browser object between steps in the step definition class.Create a WebBrowser static class towork with the WatiN IE object and the ScenarioContext that SpecFlow uses to store state between steps in a scenario.2. Wrapper browsers/ EncapsulationIE/Firefox/Chrome3. WatiN/Selenium
  • #26 This is widely used in web automation, especially in Selenium community. Here I share my practicesThree essential parts of GUI automation: Find controls, Manipulate controls, VerifyPage object pattern: Find controls
  • #27 Manipulate controlsSeparate this from page object reason:Business logic is too complexSome module is reusable(example of section assessment page, section details page)
  • #28 Object model (user class)Loadable components (Documents tool/ selector tool /master page/logout link)ConstantsWatiN/SeleniumExtensionsWait
  • #29 Go through one real project framework
  • #31 Auto coverage?Easy to use?Selenium or WatiN?Dev/QA ratio?