September 2, 2016

Cucumber Basics

Cucumber Basics:


There are few terminologies in cucumber. Let's understand them

Feature Files

Feature files are the files which are used to write the test automation steps in Gherkin language. Normally this will be live document. The extension of these files will be .feature.

A Feature file will contain much number of Scenarios. Each scenario represents a test case. In Each Scenario there are Steps which are differentiated by keywords called (GIVEN, WHEN, THEN etc.,)

Feature:

This gives information about the high level business functionality and the purpose of Application under test. Everybody should be able to understand the intent of feature file by reading the first Feature step.

Scenario:

Basically a scenario represents a particular functionality which is under test. By seeing the scenario user should be able to understand what the test is all about. Each scenario should follow given, when and then format. This language is called as “gherkin” language.
·         Given: As we know the meaning of given, it  specifies the precondition.
·         When: This is used when some action is to be performed.
·         Then: The expected outcome or result should be placed here.
·         Background: Whenever any step is required to perform in each scenario then those steps needs to be placed in Background.
·         And: And is used to combine two or more same type of action. i.e., if you have multiple prerequisites, then we have the combine all the GIVEN statement by using "AND" keyword.
Basic template for a FEATURE file:
Feature: <Description of the Feature>

Scenario: <Scenario 1>
                Given <Prerequisite>
                When <Actions be be performed>
                Then <Expected Result>

Scenario: <Scenario 2>
                Given <Prerequisite>
                When <Actions be be performed>
                Then <Expected Result>             
EX:
Feature: Login Functionality Feature
                In order to ensure Login Functionality works,
                I want to run the cucumber test to verify it is working

Scenario: Login Functionality
                Given user navigates to www.facebook.com
                When user logs in using Username as “USER” and Password “PASSWORD”
                Then login should be successful

Scenario Outline:

Scenario outlines are used when same test has to be performed with different data set. Ex: if we want to test login functionality with multiple different set of username and password.
Let's take the same example to show the usage of Scenario Outline
Feature: Login Functionality Feature
                In order to ensure Login Functionality works,
                I want to run the cucumber test to verify it is working

Scenario Outline: Login Functionality
                Given user navigates to www.facebook.com
                When user logs in using Username as <username> and Password <password>
                Then login should be successful

Examples:
|username|password|
|Suresh|password1|
|Naresh|password12|

Observations:

  • Column names (username and password) are passed as parameters to WHEN statement.
  •  Scenario Outline is used instead of Scenario
  • EXAMPLES are used to pass different arguments in tabular format. Vertical pipes are used to separate the columns

Tags:

Cucumber by default runs all scenarios in all the feature files. In real time projects, there could be hundreds of feature file which are not required to run at all times. We can also use multiple tag names for a single feature.

For instance: Feature files related to smoke test need not run all the time. So if you mention a tag as smokeTest in each feature file which is related to smoke test and run cucumber test with @SmokeTest tag. Cucumber will run only those feature files specific to given tags. Please follow the below example. You can specify multiple tags in one feature file.
Let's take the same example to show the usage of Scenario Outline
@SmokeTest @RegressionTest
Feature: Login Functionality Feature
                In order to ensure Login Functionality works,
                I want to run the cucumber test to verify it is working

Scenario Outline: Login Functionality
                Given user navigates to www.facebook.com
                When user logs in using Username as <username> and Password <password>
                Then login should be successful

Examples:
|username|password|
|Suresh|password1|
|Naresh|password12|
Tags can be applied to Scenarios, Please see the example below
@SmokeTest @RegressionTest
Feature: Login Functionality Feature
                In order to ensure Login Functionality works,
                I want to run the cucumber test to verify it is working

Scenario Outline: Login Success Functionality
                Given user navigates to www.facebook.com
                When user logs in using Username as <username> and Password <password>
                Then login should be successful

Examples:
|username|password|
|Suresh|password1|
|Naresh|password12|

@SmokeTest
Scenario Outline: Login Failure Functionality
                Given user navigates to www.facebook.com
                When user logs in using Username as <username> and Password <password>
                Then login should be successful

Examples:
|username|password|
|Rajesh|password1|
|Mahesh|password12|

Observation: 

  • Suppose you want to execute the scenarios/features which are in Regression Suite, then the Junit Runner will execute only "Login Success Functionality" Scenario
  • Suppose you want to execute the scenarios/features which are in Smoke Suite, then the Junit Runner will execute only "Login Success Functionality" and "Login Failure Functionality" Scenarios

Junit Runner:

Cucumber Runner / JUNIT Runner Class is the class, which is used to get all the feature files in the specified location.
The Basic Template of Junit Runner is  as follows:
@RunWith(Cucumber.class)  
@CucumberOptions (
                format(deprecated)/plugin={"pretty","json:target"} ,
                features = {"src/java/features/"}
                )
public class Runner {
}

Observations:

  • Cucumber.class : Whenever we run anything as part of cucumber it iwll run using the cucumber.class file
  • CucumberOptions: it is used to provide some additional inputs like, format, feature files location and tags
Example:
@RunWith(Cucumber.class)  
@CucumberOptions (
                format(deprecated)/plugin={"pretty","json:target"} ,
                features = {"src/java/features/"},
                tags={"@SmokeTest", "@RegressionTest"}
                )
public class Runner {
}
Note: To run the specific feature file cucumber uses standard Junit Runner and specify tags in @Cucumber. Options(Deprecated)/@CucumberOptions. Multiple tags can be given by using comma separate. Here you can specify the path of the report and type of report you want to generate.

If you run this Runner file, all the Scenarios/features under SmokeTest/RegressionTest will be executed and the result be generated in JSON format.

Trouble Shootings:

If you get the Invocation Target Exception, then you have to remove Cucumber-spring.jar from the build path
If you ran the feature file but it threw an error like this:
                Exception in thread "main" cucumber.runtime.CucumberException: Unknown option: --plugin
                 at cucumber.runtime.RuntimeOptions.parse(RuntimeOptions.java:119)
                 at cucumber.runtime.RuntimeOptions.<init>(RuntimeOptions.java:50)
                 
then you have to update the jar files to latest version
If you are not able to import the "@Cucumber.Options" and it is throwing error, the use "@CucumberOptions" instead of "@Cucumber.Options" (Deprecated)
If you are getting the below exception
Exception in thread "main" Usage: java cucumber.api.cli.Main [options] [ [FILE|DIR][:LINE[:LINE]*] ]+

Then use the version 1.2.4 for cucumber-Java, Cucumber-Junit and Cucumber-core jars. user version 2.12.2 for gherkin
                change the code as below:
                                @CucumberOptions(
                                 plugin = {"pretty","jason:Target/"},
                                 features = {"src/Cucumber/"}
                                )

What is Cucumber?

Cucumber Introduction:

Before we need to discuss about CUCUMBER, we've to know about BDD.

What is BDD?

Behavior-driven development (BDD) is a software development methodology in which an application is specified and designed by describing how its behavior should appear to an outside observer and is extension of Test Driven Development.
BDD is used to test the system rather than testing the particular piece of code.

Behavior Driven Development is extension of Test Driven Development and it is used to test the system rather than testing the particular piece of code.

What is CUCUMBER?

Cucumber is a tool based on Behavior Driven Development (BDD) framework which is used to write acceptance tests for web application. It allows automation of functional validation in easily readable and understandable format (like plain English) to Business Analysts, Developers, Testers, etc.

Cucumber feature files can serve as a good document for all. Initially Cucumber was implemented in Ruby and then extended to Java framework. Both the tools support native JUnit.

Here in this blog, I'm providing some basic information about the Cucumber with Selenium Combination and it supports all the languages supported by Selenium to develope the scripts.

Impotent Information:

Required Jars: These cucmber jar are used to create feature file, interact with the selenium server

  1. Cucumber-Core.jar
  2. Cucumber-html.jar
  3. Cucumber-java.jar
  4. Cucumber-junit.jar
  5. Cucumber-jvm-deps.jar
  6. gherkin.jar
  7. import cucumber.api.junit.Cucumber;

Links:

September 1, 2016

Synchronization Techniques

Why are waits required in selenium?

In web automation waits are required as certain elements get loaded on the page, so after triggering an event a page may get loaded successfully but some of its element may still not get loaded. This causes elementNotFound exception.

In such cases we will be using Thread.sleep() i.e. a static wait that will halt the test execution for some specified time and then perform the next step. As Thread.sleep() will wait for the specified time no matter if the elements gets visible before the specified amount of time. So, using the Thread.sleep() method is never advisable for UI automation.

To avoid this, selenium provides different types of waits , in most of the cases we will use Implicit, explicit waits and pageLoadTimeout.

Implicit Waits –

An implicit wait when used is set to the WebDriver instance and is applied to all the web elements. In implicit wait the webdriver polls the DOM to check the availability of the webElement and waits till the maximum time specified before throwing NoSuchElementException.

Syntax:
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(<interger to represent the time>, TimeUnit.<UNIT>);
 - where UNIT might be SECONDS, MINUTES, HOURS, DAYS etc.,
 - TimeUnit is available from java.util.concurrent Package.

Example:
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

In the above example the value 10 specified in implicit wait method is the maximum time in seconds till which webDriver will wait before throwing NoSuchElementException while locating a webElement.

Explicit Waits –

Unlike implicit waits (which is applied on WebDriver instance without any condition), the explicit waits are applied to each and every webElement. In explicit wait certain conditions are defined for which the webDriver instance waits before locating webElements or performing actions on them. Some of the most common conditions specified in explicit waits are-
visibilityOfElementLocated, elementToBeClickable, presenceOfElementLocated etc.

Syntax:
WebDriverWait wait = new WebDriverWait(driver, <Time is Seocnds>);
wait.until(ExpectedConditions.<condition>(ElementLocator));

Example:
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.visibilityOfElementLocated(ElementLocator));

Here the webDriver instance will wait until the condition specified is met i.e. the visibility Of Element located by the Locator with the maximum wait time of 30 seconds after which if the condition is still not met than it will throw NoSuchElementException exception.


FluentWait Command :

Each FluentWait instance defines the maximum amount of time to wait for a condition, as well as the frequency with which to check the condition. Furthermore, the user may configure the wait to ignore specific types of exceptions whilst waiting, such asNoSuchElementExceptions when searching for an element on the page.

 Wait wait = new FluentWait(driver)
    .withTimeout(30, SECONDS)
    .pollingEvery(5, SECONDS)
     .ignoring(NoSuchElementException.class); 

PageLoadTimeout Command:

Sets the amount of time to wait for a page load to complete before throwing an error. If the timeout is negative, page loads can be indefinite.
driver.manage().timeouts().pageLoadTimeout(100, SECONDS);

SetScriptTimeout Command :

Sets the amount of time to wait for an asynchronous script to finish execution before throwing an error. If the timeout is negative, then the script will be allowed to run indefinitely.
driver.manage().timeouts().setScriptTimeout(100,SECONDS);