Are you familiar with Cucumber and want to test your understanding? This post contains 25 useful Cucumber multiple-choice questions (quiz) to self-test your knowledge of Cucumber.
You can find the questions for the below-mentioned answers in this post –
1) 1) Behavior Driven Development 2) 2) TDD 3) 2) BDD 4) 3) Discovery, Formulation, Automation 5) 2) Discovery, Formulation, Automation 6) 1) Common agreed understanding of the behaviour of the system 7) 2) Documented Examples 8) 1) Implemented Code + Automation Tests 9) 3) To have a different perspective from at least 3 different teams to discuss and create a common understanding 10) 3) Product Owner, Developer & Tester 11) 2) General description of the software feature or change from an end-user perspective 12) 1) Product Owner 13) 4) All of the above 14) 4) All of the above 15) 3) All of the above 16) 3) Both (1) and (2) 17) 1) False 18) 2) Cucumber and 3) Serenity 19) 4) All of the above 20) 4) Ruby
Are you familiar with Cucumber and want to test your understanding? This post contains 25 useful Cucumber multiple-choice questions (quiz) to self-test your knowledge of Cucumber.
Scenario Outline: Login to Home Page
Given user is logged in
When user clicks <link>
Then user will be logged out
@mobile
Examples:
| link |
| mobile logout |
@desktop
Examples:
| link |
| desktop logout |
Are you familiar with Cucumber and want to test your understanding? This post contains 25 useful Cucumber multiple-choice questions (quiz) to self-test your knowledge of Cucumber.
You can find the questions for the below-mentioned answers in this post –
4) 1) Describe the scenarios, behaviour, and expected result of a feature.
5) 2) It is an interface between a feature file and a step definition file, and it is used to execute the step definition file corresponding to a feature file.
6) 1) It is a Java method with an expression that links it to one or more Gherkin steps. When Cucumber executes a Gherkin statement in a scenario, it will look for a matching step definition to execute.
7) 4) .feature
8) 1) Background 4) Scenario Outline 5) Feature 6) Given 8) When
9) 1) Feature
10) 1) Given
11) 2) When
12) 1) Then
13) 2) But
14) 1) And
15) 3) Background
16) 1) A way to run the same scenario with different inputs
17) 1) A piece of code that runs before or after a scenario or feature
18) 1) A label that can be applied to a scenario or feature
19) 1) To describe the setup, action, and expected result of a scenario
Are you familiar with Cucumber and want to test your understanding? This post contains 25 useful Cucumber multiple-choice questions (quiz) to self-test your knowledge of Cucumber.
5. Which of the following is true for a TestRunner class in cucumber?
Choose one option
It is a Java file that defines or implements the logic for the scenario defined in the feature file.
It is an interface between a feature file and a step definition file, and it is used to execute the step definition file corresponding to a feature file.
Describe the scenarios, behaviour, and expected result of a feature.
6. Which of the following is true for the Step Definition in cucumber?
It is a Java method with an expression that links it to one or more Gherkin steps. When Cucumber executes a Gherkin statement in a scenario, it will look for a matching step definition to execute.
It is a Java file that is used to trigger the TestRunner class.
Describe the scenarios, behaviour, and expected result of a feature.
1) 2) BDD (Behaviour driven development) 2) 4) All of the above 3) 3) Gherkin 4) 1) Describe the scenarios, behaviour, and expected result of a feature. 5) 2) It is an interface between a feature file and a step definition file, and it is used to execute the step definition file corresponding to a feature file. 6) 1) It is a Java method with an expression that links it to one or more Gherkin steps. When Cucumber executes a Gherkin statement in a scenario, it will look for a matching step definition to execute. 7) 4) .feature 8) 1) Background 4) Scenario Outline 5) Feature 6) Given 8) When 9) 1) Feature 10) 1) Given 11) 2) When 12) 1) Then 13) 2) But 14) 1) And 15) 3) Background 16) 1) A way to run the same scenario with different inputs 17) 1) A piece of code that runs before or after a scenario or feature 18) 1) A label that can be applied to a scenario or feature 19) 1) To describe the setup, action, and expected result of a scenario 20) 1) Cucumber Reports 21) 4) All of the above 22) 1) @ 23) 3) Both 1 and 2 24) 1) ~ 25) 2) If
Tags are a great way to organize Features and Scenarios. By default, Cucumber executes all the scenarios present in a Feature File. If you want to run a specific scenario from the feature file, then a tag can be used.
Tags can be declared as below:-
@TagName
Scenario: Test the scenario
Here,
@: It is a symbol used to declare a tag. TagName: It is the name of a specific test.
In the below example, there are 2 feature files – LoginPage.feature and DashboardPage.feature. LoginPage.feature contains 4 different scenarios with different tags, whereas DashboardPage.feature contains 3 different scenarios.
LoginPage.feature
Feature: Login to HRM Application
Background:
Given User is on Login page
@ValidCredentials
Scenario: Login with valid credentials
When User enters username as "Admin"
And User enters password as "admin123"
Then User should be able to login successfully
@InValidCredentials
Scenario: Login with invalid credentials
When User enters username as "Admin123"
And User enters password as "admin123"
Then User should see an error message "Invalid credentials"
@InValidCredentials
Scenario: Login with invalid username and valid password
When User enters username as "1234"
And User enters password as "admin123"
Then User should see an error message "Invalid credentials"
@InValidCredentials @SpecialCharacters
Scenario: Login with special characters
When User enters username as "$$$$"
And User enters password as "%%%%%"
Then User should see an error message "Invalid credentials"
DashboardPage.feature
Feature: Dashboard page validation
Background:
Given User is on Login page
When User enters username as "Admin"
And User enters password as "admin123"
Then User should be able to login successfully
@ValidQuickLaunch
Scenario Outline: Login with valid credentials to check QuickLanuch options
Then there are valid QuickLaunch options '<options>'
Examples:
| options |
| Assign Leave |
| Leave List |
| Timesheets |
Running a single Cucumber Tag
import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;
import org.junit.runner.RunWith;
@RunWith(Cucumber.class)
@CucumberOptions(features= {"src/test/resources/features"}, glue= {"org.example.definitions"},
tags = ("@ValidCredentials"))
public class RunCucumberTest {
}
In the above example, only 1 scenario is tagged as @ValidCredentials in both the feature file, so it is executed.
The output of the above program is
Tags can be placed above Feature, Scenario, Scenario Outline, and Examples.
A feature or Scenario can have as many tags. However, each tag should be separated by a blank space.
How to execute Cucumber Tag that runs multiple scenarios
Let us consider a situation where a tag (@InValidCredentials) has multiple test scenarios, and you use this tag in the Runner class, then all the test scenarios associated with this tag will execute.
import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;
import org.junit.runner.RunWith;
@RunWith(Cucumber.class)
@CucumberOptions(features= {"src/test/resources/features"}, glue= {"org.example.definitions"},
tags = ("@InValidCredentials"))
public class RunCucumberTest {
}
The output of the above program is
How to execute Multiple Cucumber Tags
The testing through multiple tags can be done by using two operators:
OR operator
AND operator
OR operator
OR means scenarios that are tagged either with @BlankCredentials or @InvalidCredentials will execute. The syntax is mentioned below:
import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;
import org.junit.runner.RunWith;
@RunWith(Cucumber.class)
@CucumberOptions(features= {"src/test/resources/features"}, glue= {"org.example.definitions"},
tags = ("@ValidCredentials or @SpecialCharacters"))
public class RunCucumberTest {
}
In the above example, we have 1 scenario that is tagged as @ValidCredentials and another one tagged as @SpecialCharacters. So, atotal of 2 scenarios are executed.
The output of the above program is
AND operator
Suppose you want to test an application with multiple test cases and if the first test case is failed in the application, we do not want to test the second test case. The syntax is mentioned below:
In this tutorial, we will run the tests in the BDD format in Robot Framework.
What is BDD?
BDD is an Agile software development process in which an application is documented and designed around the behaviour that a user expects to see when interacting with it. BDD helps to avoid bloat, excessive code, unnecessary features, and lack of focus by encouraging developers to focus only on the requested behaviours of an app or program. This methodology combines, augments, and refines test-driven development (TDD) and acceptance testing practices.
The Given-When-Then syntax is a commonly used structure for writing user stories and acceptance criteria in a behaviour-driven development (BDD). It is used to describe the desired behaviour of a system in a clear, concise, and consistent manner.
The structure is broken down into three parts:
Given: This section describes the initial state or context of the system. It sets the scene for the scenario being tested.
When: This section describes the action or event that occurs. It specifies the trigger for the scenario being tested.
Then: This section describes the expected outcome or result of the scenario. It defines the acceptance criteria for the scenario being tested.
Step 1.1 – Open PyCharm and create a new project. Go to File and select New Project from the main menu.
Step 1.2 – Choose the project location. Click the “Browse” button next to the Location field and specify the directory for your project.
Deselect the Create a main.py welcome script checkbox because you will create a new Python file for this tutorial.
Click on the “Create” Button.
Step 1.3 – A new dialog appears asking to open the project using any one of the given options. I have selected New Window as I like to have separate windows for each project.
Below is the image of the new project created in PyCharms.
How to run tests in BDD format in Robot Framework?
Step 2 – Create a new directory in the new project
Right-Click on the project, select New->Directory and provide name as Tests
Below is the image of the new directory.
Right-click on the new directory and select New File and provide the name as BDD_Demo.robot as shown below:
Step 3 – Execute the tests
We are now going to write test cases. The test case details will be as follows −
*** Settings ***
Documentation Tests to login to Login Page
Library SeleniumLibrary
*** Variables ***
${valid_username} Admin
${valid_password} admin123
${invalid_password} 45678
${url} https://opensource-demo.orangehrmlive.com/web/index.php/auth/login
${browser_name} Chrome
${login_error_message} css:.oxd-alert-content--error
${dashboard_title} css:.oxd-topbar-header-breadcrumb-module
*** Test Cases ***
Validate Unsuccessful Login using invalid credentials
[Tags] SMOKE
Given I open the Browser with URL
When I fill the login form ${valid_username} ${invalid_password}
Then I verify the error message is correct
And Close Browser Session
Validate successful Login
[Tags] UAT
Given I open the Browser with URL
When I fill the login form ${valid_username} ${valid_password}
Then I verify Dashboard page opens
And Close Browser Session
*** Keywords ***
Given I open the Browser with URL
Create Webdriver ${browser_name} executable_path=/Vibha_Personal/RobotFramework_Demo/drivers/${browser_name}
Go To ${url}
Maximize Browser Window
Set Selenium Implicit Wait 5
When I fill the login form
[Arguments] ${username} ${password}
Input Text css:input[name=username] ${username}
Input Password css:input[name=password] ${password}
Click Button css:.orangehrm-login-button
Then I verify the error message is correct
Element Text Should Be ${login_error_message} Invalid credentials
Then I verify Dashboard page opens
Element Text Should Be ${dashboard_title} Dashboard
And Close Browser Session
Close Browser
All the below-mentioned keywords are derived from SeleniumLibrary except the last one. The functionality of keywords mentioned above:
1. Open Browser − The keyword opens a new browser instance to the optional URL.
2. Maximize Browser Window – This keyword maximizes the current browser window.
3. Set Selenium Implicit Wait – This keyword sets the implicit wait value used by Selenium.
4. Input Text − This keyword is used to type the given text in the specified textbox identified by the locator name:username.
5. Input Password – This keyword is used to type the given text in the specified password identified by the locator name:password.
The difference compared to Input Text is that this keyword does not log the given password on the INFO level.
6. Click button – This keyword is used to click on the button with location css:.orangehrm-login-button.
7. ${result} – This is a variable that holds the text value of the error message that is located by css:.oxd-alert-content-text
8. Element Text Should Be– This keyword is used to verify that the element locator contains exact the text expected.
To run this script, go to the command line and go to directory tests.
Step 4 – Execute the tests
We need the below command to run the Robot Framework script.
robot .
The output of the above program is
Step 5 – View Report and Log
We have the test case passed. The Robot Framework generates log.html, output.xml, and report.html by default.
Let us now see the report and log details.
Report
Right-click on report.html. Select Open In->Browser->Chrome(any browser of your wish).
The Report generated by the framework is shown below:
Log
Robot Framework has multiple log levels that control what is shown in the automatically generated log file. The default Robot Framework log level is INFO.
Right-click on log.html. Select Open In->Browser->Chrome(any browser of your wish).
That’s it! Congratulations on making it through this tutorial and hope you found it useful! Happy Learning!!
Robot Framework is a generic open-source automation framework. It can be used for test automation and robotic process automation (RPA). RPA is extensively used for Web Application Automation, API Automation, RPA, and Database Testing.
Robot Framework has an easy syntax, utilizing human-readable keywords. Its capabilities can be extended by libraries implemented with Python, Java, or many other programming languages.
The background is used in Cucumber to define a step or series of steps that are shared by all the tests in the feature file.
A Background provides context for the scenarios that follow it. It may include one or more Given steps, which are executed before each scenario but after any Before hooks.
A Background is placed at the same level of indentation as the first Scenario/Example.
Below is an example of a Background in a Feature file.
Feature: Login to HRM Application
Background:
Given User is on HRMLogin page "https://opensource-demo.orangehrmlive.com/"
@ValidCredentials
Scenario: Login with valid credentials
When User enters username as "Admin" and password as "admin123"
Then User should be able to login successfully and new page open
@MissingUsername
Scenario: Login with blank username
When User enters username as " " and password as "admin123"
Then User should be able to see a message "Required" below Username
In the above example, we have two different scenarios, where a user is successfully able to log in to the application and an unsuccessful attempt to log in. But the common step is to open the website for both scenarios. This is why we created another Scenario for opening the browser but named it Background rather than a Scenario. So that it executes for both Scenarios.
import io.cucumber.java.en.Given;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;
public class LoginPageDefinitions {
@Given("User is on HRMLogin page {string}")
public void loginTest(String url) {
System.out.println("I am in Login Page");
}
@When("User enters username as {string} and password as {string}")
public void goToHomePage(String userName, String passWord) {
System.out.println("Go to Home Page");
}
@Then("User should be able to login successfully and new page open")
public void verifyLogin() {
System.out.println("Home Page is opened");
}
@Then("User should be able to see a message {string} below Username")
public void verifyMissingUsernameMessage(String message) {
System.out.println("Login failed with an error message");
}
}
We need a runner class to execute the feature file. Below is an example of the Runner class.
import io.cucumber.testng.AbstractTestNGCucumberTests;
import io.cucumber.testng.CucumberOptions;
@CucumberOptions(tags = "", features = "src/test/resources/features/LoginPage.feature", glue = "org.example.definitions",
plugin = {})
public class CucumberRunnerTests extends AbstractTestNGCucumberTests {
}
The output of the above program is
Background with Hooks
Background can contain one or more Given steps, which are run before each scenario, but after any Before hooks.
Below is an example of the background with hooks. The feature file is the same. I’m just adding the hooks to the Step Definition class.
import io.cucumber.java.en.Given;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;
public class LoginPageDefinitions {
@Before()
public void beforeScenario(){
System.out.println("Start the browser and Clear the cookies");
}
@Given("User is on HRMLogin page {string}")
public void loginTest(String url) {
System.out.println("I am in Login Page");
}
@When("User enters username as {string} and password as {string}")
public void goToHomePage(String userName, String passWord) {
System.out.println("Go to Home Page");
}
@Then("User should be able to login successfully and new page open")
public void verifyLogin() {
System.out.println("Home Page is opened");
}
@Then("User should be able to see a message {string} below Username")
public void verifyMissingUsernameMessage(String message) {
System.out.println("Login failed with an error message");
}
@After()
public void afterScenario(){
System.out.println("Log out the user and close the browser");
}
}
The output of the above program is
Points to remember
The background should not be used to create complicated states unless the client requires that information. For example, if the client doesn’t care about the user and site names, use a higher-level step like Given I am logged in as a site owner
Keep the Background section brief. When reading the scenarios, the client must remember this information. Consider moving some of the irrelevant details into higher-level steps if the Background is longer than four lines.
Make your background section stand out Use interesting names and try to tell a story. The human brain remembers stories much better than it remembers names like “User A,” “User B,” “Site 1,” and so on.
That’s it! Congratulations on making it through this tutorial and hope you found it useful! Happy Learning!!