Specflow - Create First Test

0 comments

Problem

I want to develop automation tests using BDD framework SpecFlow

Pre-condition

1) Visual Studio is installed.
2) Solution with basic test automation framework is set-up in Visual Studio.

Solution

SpecFlow installation

1. Go to Tools -> Extensions and Updates
2. Open Tab Online, type SpecFlow to search pane, click Download for first result, and then click Install. After Installation restart VS.
3. As SpecFlow is integrated with NUnit, in order to see tests in Test Explorer of VS, you need to install NUnit Test Adapter too.
4. Add SpecFlow NuGet package for your solution. Right-click on Solution, click Manage NuGet Packages for Solution
5. Open Tab Online, type SpecFlow to search pane, click Install on found result, select test project for SpecFlow installation

First test creation

The tests in BDD are written in a human-manner language, that is called Gherkin language. Files that contain this tests are called Features in SpecFlow. Let's create a feature file with a test.
Let's say, I want to visit site Flipkart.com and navigate to Search page, perform search with a keyword and check search results are displayed.

1. In Test Project, make folder Features.
2. Add SpecFlow Feature file to test solution. Right-click on folder Features, click Add -> New Item
3. Here you can see the files that have been added during SpecFlow installation. Choose SpecFlow Feature File, name it and click Add.
4. Copy below code to created feature file
FlipkartDemo.feature
Feature: Search
     
Scenario: Verify Search functionality
Given I navigate to "http://www.flipkart.com" web site
When I perform search with "Apple iphone 5s"
Then The result should be displayed



Let's see the keywords that SpecFlow uses:
  • Feature - feature name that is tested. Can also be considered as the test class name.
  • Scenario - can also be considered as test name.
  • Given - a pre-condition action for the test
  • When - actual actions of the test follow after that keyword
  • Then - actual action step. It is usually used as an assertion step.
  • And - copies last used keyword, e.g. in this test it's the same as Then . It's named so to make scenario more human-readable.

Ok, the BDD scenario is set-up and even non-IT person can understand what this test should do. But the computer does not understand what this words mean.
To make computer understand this scenario, you need to make a Definition class. It contains bindings for the computer to transform this human-readable text into actual actions on the page.

5. Add folder Definitions to test project. Right-click on it and click Add -> New Item. Choose SpecFlow Step Definition, name it, and click Add.
6. SpecFlow will produce a template Definition class. Replace it with below code. I will use my test framework methods, but I'm sure you can replace it with yours (smile).
Definiton.cs
using System;
using System.Linq;
using TechTalk.SpecFlow;
using TestFramework.Core.BrowserUtils;
using TestFramework.Utilities.Pages;
namespace TestFramework.Tests.Definitions
{
    [Binding]
    public sealed class SomeDefinition
    {
        Actions.Actions actions = new Actions.Actions();
        ResultsPage resultPage = new ResultsPage();
        HomePage homePage = new HomePage();

        Library.Lib lib = new Library.Lib();
        [Given(@"I launch Browser")]
        public void GivenILaunchBrowser()
        {
            Assert.IsNotNull(Library.Lib.Driver);
        }

        [Given(@"I navigate to ""(.*)"" web site")]
        public void GivenINavigateToWebSite(string url)
        {
            Library.Lib.Driver.Navigate().GoToUrl(new Uri(url));
        }

        [When(@"I perform search with ""(.*)""")]
        public void WhenIPerformSearchWith(string searchString)
        {
            homePage.PerformSearch(searchString);
        }

        [Then(@"The result should be displayed")]
        public void ThenTheResultShouldBeDisplayed()
        {
            Assert.IsNotNull(Library.Lib.Driver.FindElement(By.Id("searchCount")));
        }
    }
}
Ok, let's talk about what we see here:
  • Class with [Binding]. It means that it contains "linking methods" for the computer to understand your scenario.
  • Methods marked with attributes [Given], [When], [Then]. Code in this methods will be executed when the SpecFlow interpretator hits the line in the test with defined a) keyword b) text that is standing after that keyword. Example: SpecFlow walks through scenario See Top Tracks, sees line 'Given I navigate to application'. Then it goes to Definition class and sees method marked with keyword Given and string value for it - I navigate to application. The line in scenario and definition for it are matching -> method is set for execution.
  • You can pass strings as the parameters into corresponding method. In Scenario you can see line - When I click 'Music' on Navigation Bar. Word 'Music' is marked with deep grey in Feature file.
    In Definition file you can see method marked with [When("I click '(.*)' on Navigation Bar")]. (.*) is a regex representing all text, and the text in scenario standing for this regex will be passed into the method as parameter. So the Scenario line - When I click 'Music' on Navigation Bar - will be equal to call of Definition class method NavigationBarItemClick with parameter "Music".
Placing Scenario here so you could relate Defintion methods to Scenario lines.

7. Now Rebuild your Solution. With NUnit Test Adapter installed, you can see Feature with Scenario displayed in Test Explorer.
Data-Driven approach with SpecFlow
You may also implement Data-Driven testing using SpecFlow to do this:
  1. Replace Scenario keyword with Scenario Outline
  2. Define table of data with keyword Examples
  3. Define column name and test data under it
  4. Pass test data into test with placeholder <ColumnName>
The Scenario code will look like this:

DDT Scenario
Scenario Outline: Verify Search operation iphone
Given I navigate to "http://www.flipkart.com" web site
When I perform search with "<SearchString>"
Then The result should be displayed
Examples: 
| SearchString    |
| Apple iphone 5s |
| Apple iphone 4  |
| Apple iphone 6s |

End-state

One can write tests in BDD style using SpecFlow

Reference

SpecFlow is a powerful BDD framework that has many features. You can start from below links if you want to learn more:

Integrating Git with Jenkins

1 comments

Problem

I want to check-out my automation code from Git to build in Jenkins CI environment.

Pre-Conditions

  • Jenkins is installed and running.
  • Git repository is available.
  • Git user is available.

Solution

Install Git client on CI server
Windows: Download Git installer and run through Installation Wizard

  1. Install "Git plugin" in Jenkins
  • Click "Manage Jenkins"
  • Click "Manage Plugins"
  • Click "Available" tab
  • Enter "Git plugin" in Search box
  • Select "Git plugin"
  • Click "Install without restart"
     3. Add Credentials for Git.
         There are 2 ways to authenticate to Git: via HTTPS 
         To Add HTTPS Credentials
    • In Jenkins GUI: Click "Credentials"
    • Click "Global Credentials"
    • Click "Add Credentials"
    • Select "Username with password"
    • Enter username in Firstname_Lastname@gmail.com format
    • Enter domain password
    • Click OK


    4. Create Jenkins Job.
    • In Jenkins GUI: Click "New Item"
    • Enter Job name
    • Select "Freestyle project"
    • Click OK
    • Select Git under "Source Code Management" section
    • Enter Git repository URL (can be copied from Gitlab UI)
    • Enter branches to be fetched (*/master is a good default)
    •  Add build triggers as necessary. Expand for details...
    • Click Save (we don't add build step in this recipe)

    End State

    • Git plugin is installed in Jenkins
    • Git Credentials are added
    • Jenkins job fetching the code from Git is created and the code is fetched to CI server.

    Limitations / Known issues

    • Windows: Fetching may hang depending on which version of git client (git bash vs git cmd) is in your %PATH%.
      If encountered freezing of fetching, update %PATH% environment variable to another version (I.g. update entry from %GIT_HOME%\cmd to %GIT_HOME%\bin).
    • Jenkins Git plugin is suitable for any kind of git repositories but it contains only basic functionality. If one is using Gitlab or GitHub, specific Gitlab andGitHub Jenkins plugins might be the preferable solution.
      For more documentation please see corresponding plugin pages.


    How to switch between Angular page and Iframe in Protractor

    1 comments
    To switch to an Iframe in Protractor, there is a method to serve the purpose. Use switchTo().frame() method to switch focus to Iframe. To get back the focus from Iframe to Angular paga, use switchTo().defaultContent() method.

    Below is the sample code to explain this case. Assuming there is an Iframe and need to click an element in Iframe. Once the element is clicked, Iframe disappears automatically, then swith the focus to Angular page.

    browser.driver.switchTo().frame('Frame');
    browser.driver.findElement(By.className("insert-btn")).click();
    browser.switchTo().defaultContent().then(function() {
       var elm=element.all(by.repeater('data in Data.items')).filter(function(elem, index) {
           return elem.getText().then(function(text) {
               return (text.indexOf("HTML")>-1);
    });
    }).then(function(filteredElements){
        filteredElements[0].click();
          });
    });


    How to select an item in a list in Protractor

    0 comments
    When we need to select an element in the list of element, we need to filter them out with the required criteria like visible text and then perform the action (click).

    We use filter method to get the array of required elements and then perform the action on the array of elements in a callback function.

    See the following sample code to understand better.

    var editTemplateByName = function(templateName) {
        element.all(by.repeater('template in templates')).filter(function(elem, index) {
            return elem.getText().then(function(text) {
                return (text.indexOf(templateName)>-1);
            });
        }).then(function(filteredElements){
           element(by.buttonText('or Edit')).click();
        });
    };

    call the method: editTemplateByName('HTML');

    Capybara brief introduction

    0 comments
    First of all, its open source ruby language based framework which uses selenium web-driver internally to interact with web application controls.
    It's designed specifically for testing a web application through the UI, so it has useful methods for asserting that the page is correct.
    It has the built-in mechanism for handling synchronization issues that we run into while testing web applications.

    It has a very good API support to interact with the application controls. I see there is a discussion forum to discuss the issues/solutions.

    We can integrate BDD frameworks like Cucumber and RSpec with Capybary for better acceptance test results.

    Links:
    API Support
    Discussion Forum
    Sample Code

    Default Test Frameworks in Protractor v3

    0 comments
    By default, Protractor v3 supports Jasmine and Mocha test frameworks. Cucumber is no longer included by default as of version 3.0. However, you can integrate Cucumber with Protractor with the custom framework option. For more details, see the Protractor Cucumber Framework site or the Cucumber GitHub site.

    Jasmine:

    Protractor v3 default test framework is Jasmine 2.x. If you want to upgrade to Jasmine 2.x from 1.3, follow the Jasmine upgrade guide.


    Mocha:

    If you would like to use Mocha as test framework, you need to use BDD interface and Chai assertion libraries with Chai As Promised.

    Note: Limited support for Mocha is available as of December 2013. For more information, see the Mocha documentation site.