eBook .NET 4 L1 BDD With Selenium and SpecFlow
eBook .NET 4 L1 BDD With Selenium and SpecFlow
Joe has been assigned a task where he needs to use LINQ to generate
some data.
In this lesson, we will learn how to solve this real-world scenario to help
Joe complete his task effectively and quickly.
Learning Objectives
C# is an object-oriented programming language that combines the features of Java and C++.
Structured
Component Rich
oriented library
C#
Simple Features Interoperable
Scalable Upgradeable
Fast
Different Tools for Automation
Cake Build Allows to compile codes and run unit tests by invoking MSuild and Xunit
Final Builder Provides visual debugging without editing XML or writing scripts
SpecFlow Uses the Gherkin syntax to build readable tests written in plain english
01
Supports various operating systems, browsers, and languages
02
Supports parallel test execution
03
Works with external frameworks such as TestNG, Ant, and Maven
04
Offers simpler commands as compared to other tools
05
Performs web-based automated testing
Benefits of Selenium Testing
Offers multi browser support, such as Google Allows integration with JUnit and TestNG that
Chrome, Internet Explorer, Mozilla Firefox, or aids in generating reports and managing test
Safari cases
Automation Regression
testing testing
Performance
Browser testing
testing
Smartphone app
testing
Selenium Setup
01 Install Java 02 03
Install Eclipse IDE for Install Selenium Java
Development Kit
Java Developers Client Driver
(JDK)
06 05 04
Add a new package Create a new project Configure Eclipse
and its details and add its details IDE with WebDriver
07 08
Create a new class
Install Driver Server
and add its details
Basics of Selenium IDE
Selenium IDE
Selenium IDE (Integrated Development Environment) is a record-and-run tool to quickly create test
cases. It is implemented as a Firefox extension.
Selenium Remote Control (RC) is a test tool to write automated web application user interface tests in any
programming language.
Web Server Web Server Web Server Web Server Web Server
Java/Ruby/Python/.Net
Selenium Grid
Selenium Grid distributes the tests on multiple machines, allowing simultaneous testing across
machines. It reduces testing time and gives quick feedback to users.
● Takes instructions from the WebDriver client and executes them remotely on the parallel
Hub nodes
● A central server with only one hub in the grid
Duration: 20 min.
Problem Statement:
Create an application to set up Selenium with Visual Studio.
Assisted Practice: Guidelines to Set Up Selenium with Visual Studio
Selenium WebDriver architecture controls the browser from the operating system level. It requires only the
programming language IDE and a browser.
HTTP over HTTP Server
XPath, or XML Path, is a query language used to navigate through XML documents. It is used to find a
web element in a web page.
XPath query is used to navigate through XML documents to select individual elements and attributes.
● Can search an element in the middle of the HTML Document Object Model (DOM)
Relative structure
● Begins with a double slash (//) to search anywhere in the web page
Handling Complex and Dynamic Web Elements
XPath functions are used to identify multiple elements having same names or buttons having same IDs and
names.
Basic XPath Finds node or set of nodes based on attributes such as ID and name
XPath Axes methods are used to find the complex or dynamic web elements on the web page.
Following are some of the most commonly used XPath Axes methods:
Following-Sibling Selects all elements that are in the same level as the current node
The Wait method informs Selenium that the program would like to wait for a certain amount of time
before throwing an exception when it cannot find an element in a web page.
Find element.
Tests Web Driver Wait and loop for Browser
given time.
Types of Wait
Selenium wait helps resolve issues that may occur due to loading of different elements on the web page
at different time intervals.
Explicit Wait Waits for certain conditions or maximum time gets exceeded
Locating techniques are used to find elements. It informs the Selenium IDE about the GUI element that
needs to be located, using the keyword findElement.
Types of Locators:
findElement(By.tagName("Tag")) Tag name locator is used to search an element by its tag name
Link text locator is used to search an element by using <a> tag in the
findElement(By.linkText("LinkText")) HTML links
Duration: 40 min.
Problem Statement:
Create an application to locate web elements.
Assisted Practice: Guidelines to Locate Web Elements
Iframe, or inline frame, is an HTML page embedded in another HTML page. It may be content from
another source, such as an advertisement.
By Index The browser will switch to the frame with the specified index number
By Name or ID The browser will switch to the frame with the specified name or ID
By Web Element The browser will switch to the frame with the specified web element
Alerts
Alert is a message or a notification that displays on a small window over the current window to provide
information, to seek confirmation, or to warn.
Types of alerts:
Simple Alert Displays an information or warning on the screen
A Pop-up is a window that displays on screen when the alerts are triggered.
Alerts that come up with options for users to go ahead, cancel, or input
Web-based
something.
Changing Windows
Selenium WebDriver lets users switch browser windows, or open new windows by clicking on links or buttons. It
keeps track of all the opened windows by their unique IDs, called window handles.
Syntax:
Screenshots are used to analyze test case failures and in bug analysis. Selenium automatically takes snapshots
during test execution.
Syntax:
Duration: 40 min.
Problem Statement:
Demonstrate the use of Iframes in a web page.
Assisted Practice: Guidelines to Use Iframes in a Web Page
Selenium Grid allows running multiple tests on different browsers and operating systems in parallel. it
consists of a single Hub as a server and multiple nodes as test machines.
Node
Node
Node
Node
Node
Hub
Advanced User Interactions and Cross Browser Testing
Advanced User Interactions
Advanced User Interactions API describes complex actions that a user can do on a web page, such as
drag and drop, clicking of multiple options while holding control key.
ButtonReleaseAction ClickAction
DoubleClickAction ClickAndHoldAction
MoveToOffsetAction KeyDownAction
SendKeysAction KeyUpAction
MoveMouseAction ContextClickAction
Advanced User Interaction Methods
Advanced User Interactions API handles special keyboard and mouse events using actions and action
classes.
Method Action
doubleClick() Double-click at the current mouse location
contextClick() Right-click at the current mouse location
clickAndHold() Click without releasing at the current mouse location
dragAndDrop(source, target) Click and hold at source element, move to the target, release the mouse
dragAndDropBy(source, x-offset, Click and hold at the source element, move by a given offset, release mouse
y-offset)
keyDown(modifier_key) Press Modifier key without releasing it
keyUp(modifier _key) Release Modifier key
release() Release the depressed left mouse button at the current mouse location
sendKeys(onElement, Send a series of keystrokes onto the element
charsequence)
Cross Browser Testing
Cross Browser Testing is a technique in Selenium that allows users to test web applications in different
browsers. Selenium integrates with TestNG to perform multi-browser testing.
Google Chrome supports many browser extensions that can be tested through the Selenium
automation capability.
Maven is a build management and automation tool that is used to make builds consistent with other projects.
NOTE: Maven downloads all dependency jars into the local repository. If
not done automatically, jars can be populated manually.
Page Object Model Design
Page Object Model (POM) is an XML file containing default values, project information, and configuration
details that Maven uses to build the project.
Page object
Page Factory is a class that is an extension of the POM design pattern. It is used to initialize page object
elements and instantiate the objects.
Page
Duration: 90 min.
Problem Statement:
Create an application using Maven.
Assisted Practice: Guidelines to Create an Application Using Maven
Testing refers to the process of executing a program or running a software application with an
intent of identifying bugs, errors, or missing technical requirements in the program.
Functional
Testing
Responsive
UI Testing
Testing
Regression
Smoke Testing Testing
Testing
SpecFlow is an open source testing framework supporting Behavior Driven Development (BDD)
practices in .NET. It defines application behavior in plain text using Gherkin language.
Features:
SpecFlow scenarios have a name, and can consist of multiple scenario steps
Generates executable unit tests from the scenarios that represent acceptance criteria
Generates a unit test method for each scenario, with the method name derived from the scenario
name
Generates a unit test class for the feature element, with the class name derived from the feature name
Generates row-based tests from scenario outlines if the testing framework supports it
Behavior Driven Development (BDD)
Behavioral Driven Development (BDD) is a set of software development practices developed using simple
scripting language.
BDD Practices:
BDD specifications are written in plain english, defined by Gherkin language, and supported by the Cucumber test
framework.
Gherkin is a domain-specific and line oriented language to describe business behavior without going into the details
of implementation. Each line or statement in a Gherkin script starts with a special keyword.
Simple to understand
Gherkin uses indentation define structure. Gherkin text acts as the documentation and
skeleton of automated tests.
Gherkin syntax:
NOTE: Comments can be added anywhere in the Gherkin scripts by prefixing the comment
statement with #.
Gherkin
This script can be used to redirect the user to their dashboard if the specified conditions
(statements starting with and keyword in the script) are met.
Gherkin Feature Keyword
Feature keyword is used to describe a software feature and group the related scenarios.
Elements in a feature:
An optional description
Gherkin Step Definitions
Step Definition is a Java or C# method in a class with an annotation attached to it. The annotation with a
pattern is used to link Step Definition to all the matching steps that are executed.
[Given(@“Userid is (.*)")]
public void GivenUserIDAs(string name)
{
// ...
}
Alternate Syntax:
Step 1:
Given (/^ I am on google.com$/) do
Browser.goto "http://google.com"
end
Step 2:
When (/^ click on gmail menu$/) do
Browser.text (:name, " google mail" ).click
end
Step 3:
Then (/^ I should see gmail page$/) do
Browser.goto "https://mail.google.com"
end
SpecFlow Setup
Duration: 20 min.
Problem Statement:
You are given a project to set up SpecFlow.
Assisted Practice: Guidelines to Set Up SpecFlow
Duration: 20 min.
Problem Statement:
Create an application to work with multiple data using tables.
Assisted Practice: Guidelines to Work with Multiple Data Using Tables
Scenario Context is a class to store data relative to the current scenario state. It is mainly used to pass values
between steps.
// step1.cs
[Given(@"an employee exists by the name of Xxyy Aabb")]
public void s1(){
var emp = new Employee{ Name = "Xxyy Aabb"};
ScenarioContext.Current["AEmployee"] = emp;
}
// step2.cs
[Then(@"his name should be Xxyy Aabb"]
public void s2(){
var emp = ScenarioContext.Current["AEmployee"];
emp.Name.ShouldEqual("Xxyy Aabb");
}
SpecFlow.Assist.Dynamics Package
[Binding]
public class StepsUsingStaticType Syntax with Assist.Dynamic:
{ private Person _person;
[Then(@"Name property on Person should equal '(.*)'")] [Given(@"Create an instance from this table")]
public void PersonNameShouldBe(string expectedValue) public void c(dynamic instance) { _instance = instance; }
{ Assert.AreEqual(expectedValue, _person.Name);
} [Then(@"Name property should equal '(.*)'")]
} public void NameShouldBe(string expectedValue)
{ Assert.AreEqual(expectedValue, _instance.Name); }
public class Person }
{ public string Name { get; set; }
public int Age { get; set; }
public DateTime BirthDate { get; set; }
public double LengthInMeters { get; set; }
}
Scenario Context
Duration: 15 min.
Problem Statement:
Develop an application to create scenarios.
Assisted Practice: Guidelines to Create Scenarios
Duration: 15 min.
Problem Statement:
Develop an application to create SpecFlow bindings.
Assisted Practice: Guidelines to Create SpecFlow Bindings
Duration: 15 min.
Problem Statement:
Create an application to use SpecFlow context injections.
Assisted Practice: Guidelines to Use SpecFlow Context Injections
Duration: 15 min.
Problem Statement:
Create an application to use scoped bindings.
Assisted Practice: Guidelines to Use Scoped Bindings
Duration: 15 min.
Problem Statement:
Demonstrate the use of step argument transformation.
Assisted Practice: Guidelines to Use Step Argument Transformation
Dynamic Table Conversion (DTC) is the automatic conversion between the table class and a collection of
user-defined classes by using SpecFlow.Assist.Dynamic.
Create binding
Duration: 20 min.
Problem Statement:
Demonstrate the conversion between table class and user-defined class.
Assisted Practice: Guidelines to Convert Between Table Class and User-Defined Class
SpecFlow + Excel is used to define requirements and example sets in MS Excel, similar to the plain text files in
Gherkin.
Steps to install and configure SpecFlow + Excel:
Bind the test to application code and generate the step definitions
Build the application and run test from the test explorer
Step Definition class is a Java class to store step definition methods. The methods are linked to scenario steps in
the feature file.
Add the path of the step definition class in the test runner
Duration: 15 min.
Problem Statement:
Create an application for getting feature, step, and scenario info (BDD and SpecFlow series).
Assisted Practice: Guidelines to Get Feature, Step, and Scenario Info (BDD and SpecFlow Series)
1. Create a unit test project to get feature, step, and scenario info.
2. Install Specflow and related libraries.
3. Create a sample feature file.
4. Create a sample steps file.
5. Build the project.
6. Run all the tests in test explorer.
7. Push the code to your GitHub repositories.
Calling Step Definitions from One Project to Another
Project in Specflow
Duration: 20 min.
Problem Statement:
Create an application for calling step definitions from one project to another project in SpecFlow.
Assisted Practice: Guidelines to Call Step Definitions of One Project to Another in SpecFlow
1. Create a unit test project to set up a sample feature and step file.
2. Install Specflow and related libraries.
3. Create a sample feature file.
4. Create a sample steps file.
5. Create a second unit test project to call the step definitions from the first project.
6. Install Specflow and related libraries.
7. Create a sample steps file.
8. Build the project.
9. Run all the tests in test explorer.
10.Push the code to your GitHub repositories.
SpecFlow Parallel Execution
Introduction
Parallel testing, along with remote testing, reduces the duration of an automated test run.
With Memory (AppDomain) Isolation When the application architecture depends on a static state
Without Memory Isolation When the application architecture does not depend on a static state
Running SpecFlow Test in Parallel with SpecFlow 2.0 and
NUnit 3
Duration: 20 min.
Problem Statement:
Demonstrate running of SpecFlow test in parallel with SpecFlow 2.0 and NUnit 3.
Assisted Practice: Guidelines to Run SpecFlow Test
1. Create a unit test project to show parallel execution in SpecFlow and NUnit.
2. Install SpecFlow, NUnit, and related libraries.
3. Create a feature file for login.
4. Create a steps file for login.
5. Create a feature file for UserForm.
6. Create a steps file for UserForm.
7. Change AssemblyInfo.cs.
8. Build the project.
9. Run all the tests in test explorer.
10.Push the code to your GitHub repositories.
Parallel Execution with SpecFlow 2.0 + Selenium + NUnit
Duration: 20 min.
Problem Statement:
Demonstrate parallel execution with SpecFlow 2.0 + Selenium + NUnit.
Assisted Practice: Guidelines for Parallel Execution with SpecFlow 2.0 + Selenium + NUnit
1. Create a unit test project to show parallel execution in SpecFlow and NUnit.
2. Install SpecFlow, NUnit, and related libraries.
3. Create a feature file for Yahoo.
4. Create a steps file for Yahoo.
5. Create a feature file for Google.
6. Create a steps file for Google.
8. Change AssemblyInfo.cs.
Extent Reports are used to create HTML reports from tests. It is an Application Program Interface (API)
written for Java and .Net.
Duration: 15 min.
Problem Statement:
Demonstrate how to generate a report dynamically with extent reporting.
Assisted Practice: Guidelines to Generate a Report Dynamically with Extent Reporting
Duration: 15 min.
Problem Statement:
Demonstrate how to generate a report with SpecFlow steps.
Assisted Practice: Guidelines to Generate a Report with SpecFlow Steps
Duration: 15 min.
Problem Statement:
Demonstrate historical extent report using KLOV reporting.
Assisted Practice: Guidelines for Historical Extent Report Using KLOV Reporting
Install Selenium WebDriver libraries and Selenium WaitHelpers to add ExpectedCondition waits
Create folders for features and step definitions to store their respective implementation files
Setting Stage Ready for BDD with Selenium and SpecFlow
Duration: 20 min.
Problem Statement:
Set the stage ready for BDD with Selenium and SpecFlow.
Assisted Practice: Guidelines to Set Stage Ready for BDD with Selenium and SpecFlow
1. Create a unit test project to use Selenium with SpecFlow.
2. Install SpecFlow, Selenium, and related libraries.
3. Create a feature file.
4. Create a steps file.
5. Create a Hook file to set up Selenium browser object.
6. Build the project.
7. Run all the tests in test explorer.
8. View the reports.
9. Push the code to your GitHub repositories.
Writing Simple BDD Code with Selenium
Duration: 20 min.
Problem Statement:
Write simple BDD code with Selenium.
Assisted Practice: Guidelines to Write Simple BDD Code with Selenium
1. Create a unit test project to use Selenium with SpecFlow for BDD.
2. Install SpecFlow, Selenium, and related libraries.
3. Create a feature file.
4. Create a steps file.
5. Create a Hook file to set up Selenium browser object.
6. Build the project.
7. Run all the tests in test explorer.
8. View the reports.
9. Push the code to your GitHub repositories.
BDD with Selenium POM and SpecFlow
Duration: 20 min.
Problem Statement:
Demonstrate BDD with Selenium POM and SpecFlow.
Assisted Practice: Guidelines to Demonstrate BDD with Selenium POM and SpecFlow
1. Create a unit test project to use Selenium with SpecFlow using POM.
2. Install SpecFlow, Selenium, and related libraries.
3. Create a POM file.
4. Create a test feature file.
5. Create a test steps file.
6. Create a Hook file to set up Selenium browser object.
7. Build the project.
8. Run all the tests in test explorer.
9. View the reports.
10.Push the code to your GitHub repositories.
BDD with Selenium POM (Page Navigation) and SpecFlow
Duration: 20 min.
Problem Statement:
Demonstrate BDD with Selenium POM (Page Navigation) and SpecFlow.
Assisted Practice: Guidelines to Demonstrate BDD with Selenium POM and SpecFlow
1. Create a unit test project to use Selenium with SpecFlow using POM for page navigation.
2. Install SpecFlow, Selenium, and related libraries.
3. Create a home POM file.
4. Create a search POM file.
5. Create a test feature file.
6. Create a test steps file.
7. Create a Hook file to set up Selenium browser object.
8. Build the project.
9. Run all the tests in test explorer.
10.View the reports.
11.Push the code to your GitHub repositories.
Using Generics for Pages in BDD with Selenium and SpecFlow
Duration: 20 min.
Problem Statement:
Use Generics for Pages in BDD with Selenium and SpecFlow.
Assisted Practice: Guidelines to Use Generics for Pages in BDD
1. Create a unit test project to use Selenium with SpecFlow using POM and page generics.
2. Install SpecFlow, Selenium, and related libraries.
3. Create a POM file.
4. Create a BasePage generic class.
5. Create a PropsCollection class to use the generic class.
6. Create a test feature file.
7. Create a test steps file.
8. Create a Hook file to set up Selenium browser object.
9. Build the project.
10.Run all the tests in test explorer.
11.View the reports.
12.Push the code to your GitHub repositories.
Key Takeaways