{"id":1252,"date":"2022-04-29T09:28:45","date_gmt":"2022-04-29T09:28:45","guid":{"rendered":"https:\/\/www.ignitingthought.com\/?p=1252"},"modified":"2022-04-29T10:55:12","modified_gmt":"2022-04-29T10:55:12","slug":"how-to-get-started-using-cucumber-bdd-framework","status":"publish","type":"post","link":"https:\/\/stage.ignitingthought.com\/?p=1252","title":{"rendered":"How to get started using Cucumber- BDD Framework"},"content":{"rendered":"\n<p>Cucumber Framework tool supports Behavior Driven Development (BDD) and is&nbsp;used to write tests for web applications with multiple test data with minimum intervention in code. In Cucumber the executable feature files use Gherkin language, which makes the project flow easy to understand to&nbsp;interpret and execute the test scripts.&nbsp;&nbsp;&nbsp;<\/p>\n\n\n\n<p>Cucumber allows automation of functional scenario and scenario outline validation that is easily read and understood. The cucumber framework allows easier reuse of code in the tests with a time-saving mechanism in automation testing. The cucumber framework works faster and smarter with Selenium to speed up the execution of the test script.&nbsp;&nbsp;<\/p>\n\n\n\n<p>Given, \u2018When\u2019 and \u2018Then\u2019 are the main keywords to be used in the feature file, without the example keyword, Scenario outline functionality is not able to run in the runner class. Cucumber uses reporter plugins to generate reports for scenarios that are passed or failed and display them within pie charts.&nbsp;&nbsp;&nbsp;<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Test scenarios and Test Scenario Outline are written in the file, named as Feature file. It should be saved as login.feature&nbsp;&nbsp;<\/li><li>Tests code are written by focusing on user stories and project flow&nbsp;&nbsp;<\/li><li>The step definition file contains the actual code to execute the Test Scenario in the Features file, which used to be written differently in the step definitions file i.e., Java, Python&nbsp;<\/li><li>When clicking each feature line, it is navigated to the proper Step definition&nbsp;&nbsp;&nbsp;<\/li><\/ul>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"474\" height=\"367\" src=\"https:\/\/www.ignitingthought.com\/wp-content\/uploads\/2022\/04\/image.jpeg\" alt=\"\" class=\"wp-image-1254\" srcset=\"https:\/\/stage.ignitingthought.com\/wp-content\/uploads\/2022\/04\/image.jpeg 474w, https:\/\/stage.ignitingthought.com\/wp-content\/uploads\/2022\/04\/image-300x232.jpeg 300w\" sizes=\"(max-width: 474px) 100vw, 474px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Advantages of Cucumber&nbsp;&nbsp;<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>Cucumber supports languages like <a href=\"https:\/\/www.ignitingthought.com\/why-kotlin-cannot-be-missed-in-application-development\/\" target=\"_blank\" rel=\"noreferrer noopener\">Java<\/a>, Python, Ruby, and .net&nbsp;&nbsp;<\/li><li>Cucumber uses Gherkin language which is readable and can promote teamwork&nbsp;&nbsp;<\/li><li>The major advantage of cucumber is the reusability of the steps&nbsp;<\/li><li>Easy to develop and maintain with code reusability&nbsp;&nbsp;<\/li><li>Test script can be written with the knowledge of functional flow on the project&nbsp;<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Steps to create your own first Cucumber BDD with Java&nbsp;&nbsp;<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1. Feature File&nbsp;<\/strong>&nbsp;<\/h3>\n\n\n\n<p>A Feature File is\u202fan entry point to the Cucumber tests. This is a file where you will describe your tests in Gherkin language and use it as a live document at the time of testing. The extension of the feature file is \u2018example.feature\u2019. Features file contains high-level description of the Test Scenario in simple language.&nbsp;<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">&nbsp;Cucumber Feature File consists of the following components:&nbsp;<\/h4>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>Feature<\/strong>: A feature would describe the current test script which&nbsp;is to be executed&nbsp;<\/li><\/ul>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>Scenario<\/strong>: Scenario describes the steps and expected outcome for a particular test case&nbsp;<\/li><li><strong>Scenario Outline<\/strong>: Same scenario can be executed for multiple sets of data using a scenario outline. The data is provided in a tabular structure separated by (I I)&nbsp;<\/li><li><strong>Given<\/strong>: Steps are used to describe the initial context of the system&nbsp;<\/li><li><strong>When<\/strong>: Specifies the test action that is to be performed&nbsp;<\/li><li><strong>Then<\/strong>: The expected outcome of the test can be represented by \u2018Then\u2019&nbsp;<\/li><\/ul>\n\n\n\n<p><strong>Sample Feature File 1:<\/strong>&nbsp;<\/p>\n\n\n\n<p><strong>Feature<\/strong>: Login Functionality Feature&nbsp;<\/p>\n\n\n\n<p>@tagname1&nbsp;<\/p>\n\n\n\n<p><strong>Scenario<\/strong>: Login Functionality&nbsp;<\/p>\n\n\n\n<p><strong>Given<\/strong>\u202fuser navigates to SOFTWARETETINGHELP.COM&nbsp;<br><strong>when<\/strong>\u202fuser logs in using Username as \u2018USER\u2019 and Password \u2018PASSWORD\u2019&nbsp;<br><strong>Then<\/strong>\u202flogin should be successful&nbsp;<\/p>\n\n\n\n<p><strong>Sample Feature File 2 :<\/strong>&nbsp;<\/p>\n\n\n\n<p><strong>Feature<\/strong>: Login Functionality Feature&nbsp;<\/p>\n\n\n\n<p>@tagname2&nbsp;<\/p>\n\n\n\n<p><strong>Scenario Outline<\/strong>: Login Functionality&nbsp;<\/p>\n\n\n\n<p><strong>Given<\/strong>\u202fuser launches the webpage URL&nbsp;<\/p>\n\n\n\n<p><strong>When<\/strong>\u202fuser logs in using Username as &lt;<strong>username<\/strong>&gt; and Password &lt;<strong>password<\/strong>&gt;&nbsp;<br><strong>Then<\/strong>\u202flogin should be successful&nbsp;<\/p>\n\n\n\n<p><strong>Examples:<\/strong>&nbsp;<br>|username\u202f\u202f\u202f\u202f\u202f\u202f\u202f\u202f |password\u202f\u202f\u202f\u202f\u202f\u202f |&nbsp;<br>|Tom\u202f\u202f\u202f&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \u202f&nbsp; |password1\u202f\u202f\u202f\u202f |&nbsp;<br>|Harry\u202f&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \u202f |password2\u202f\u202f\u202f\u202f |&nbsp;<br>|Jerry\u202f\u202f&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |password3\u202f\u202f\u202f\u202f\u202f |&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2. Step Definition File<\/strong>&nbsp;<\/h3>\n\n\n\n<p>A step definition file in Cucumber\u202fholds the test method and code which are mapped to the Gherkin test case steps on the feature file. In the feature file each test case step once executed will have a matching step definition to execute. The extension of step Definition is mentioned as \u2018samplestepdefinition.java\u2019&nbsp;<\/p>\n\n\n\n<p>Sample Step Definition:&nbsp;<\/p>\n\n\n\n<p>@When(&#8220;user clicks {string} button&#8221;)&nbsp;<\/p>\n\n\n\n<p>public void user_clicks_button(String string) { driver.findElement(By.xpath(&#8220;\/\/button[@type=&#8217;submit&#8217;]&#8221;)).click();&nbsp;<\/p>\n\n\n\n<p>}&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3. Runner File<\/strong>&nbsp;<\/h3>\n\n\n\n<p>To start running, Cucumber needs Test Runner Class. This class uses JUnit Annotation @RunWith(). JUnit starts executing your test. In the\u202fsrc folder\u202fcreate a class called\u202f<strong>TestRunner<\/strong>. To run the specific feature file cucumber uses standard JUnit Runner and specifies tags in @Cucumber. Options. Multiple tags can be given by using comma separate. Here you can specify the path of the report and the type of report you want to generate.&nbsp;<\/p>\n\n\n\n<p>@RunWith(Cucumber.class)&nbsp;&nbsp;<\/p>\n\n\n\n<p>@CucumberOptions(&nbsp;&nbsp;<\/p>\n\n\n\n<p>Format ={ \u201cpretty\u201d,\u201djson:target\/output.json\u201d, \u201chtml:target\/html\/\u201d},&nbsp;&nbsp;<\/p>\n\n\n\n<p>Features={\u201csrc\/functional-test\/resources\u201d},&nbsp;&nbsp;<\/p>\n\n\n\n<p>Tags={\u201c@sample1\u201d,\u201d@sample2\u201d}&nbsp;&nbsp;<\/p>\n\n\n\n<p>)&nbsp;<\/p>\n\n\n\n<p>Public class CucumberRunnerTest {&nbsp;&nbsp;<\/p>\n\n\n\n<p>}&nbsp;&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Cucumber reports&nbsp;<\/strong>&nbsp;<\/h2>\n\n\n\n<p>Cucumber uses reporter plugins to produce reports that contain information about what scenarios have passed or failed. Cucumber has the capability to generate reports in the form of\u202fHTML, XML, JSON &amp; TXT.&nbsp;<\/p>\n\n\n\n<p>For HTML reports, one needs to add\u202fhtml: target\/cucumber-reports\u202fto the\u202f<em>@<\/em>CucumberOptions\u202fplugin option in runner class.&nbsp;&nbsp;<\/p>\n\n\n\n<p>plugin={\u201cpretty\u201d, \u201chtml:target\/cucumber-reports\u201d},&nbsp;<\/p>\n\n\n\n<p>This will generate an HTML report at the target location as index.html&nbsp;<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"588\" height=\"229\" src=\"https:\/\/www.ignitingthought.com\/wp-content\/uploads\/2022\/04\/image-2.png\" alt=\"\" class=\"wp-image-1253\" srcset=\"https:\/\/stage.ignitingthought.com\/wp-content\/uploads\/2022\/04\/image-2.png 588w, https:\/\/stage.ignitingthought.com\/wp-content\/uploads\/2022\/04\/image-2-300x117.png 300w\" sizes=\"(max-width: 588px) 100vw, 588px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Conclusion<\/strong>&nbsp;<\/h2>\n\n\n\n<p>&nbsp;<br>The Cucumber framework allows functional validation in an easy and understandable format in Gherkin language, that helps technical and non-technical team members to grab easily.&nbsp;Cucumber framework as greater flexibility code. It also supports report generation for each scenario.&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Cucumber Framework tool supports Behavior Driven Development (BDD) and is&nbsp;used to write tests for web applications with multiple test data with minimum intervention in code. In Cucumber the executable feature files use Gherkin language, which makes the project flow easy to understand to&nbsp;interpret and execute the test scripts.&nbsp;&nbsp;&nbsp; Cucumber allows automation of functional scenario and [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1259,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[120],"tags":[],"class_list":["post-1252","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technology-solutions","entry"],"_links":{"self":[{"href":"https:\/\/stage.ignitingthought.com\/index.php?rest_route=\/wp\/v2\/posts\/1252"}],"collection":[{"href":"https:\/\/stage.ignitingthought.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/stage.ignitingthought.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/stage.ignitingthought.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/stage.ignitingthought.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1252"}],"version-history":[{"count":4,"href":"https:\/\/stage.ignitingthought.com\/index.php?rest_route=\/wp\/v2\/posts\/1252\/revisions"}],"predecessor-version":[{"id":1260,"href":"https:\/\/stage.ignitingthought.com\/index.php?rest_route=\/wp\/v2\/posts\/1252\/revisions\/1260"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/stage.ignitingthought.com\/index.php?rest_route=\/wp\/v2\/media\/1259"}],"wp:attachment":[{"href":"https:\/\/stage.ignitingthought.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1252"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/stage.ignitingthought.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1252"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/stage.ignitingthought.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1252"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}