{"id":13540,"date":"2017-03-16T00:00:00","date_gmt":"2017-03-16T05:00:00","guid":{"rendered":"https:\/\/centricconsulting.com\/post\/using-parameters-in-bdd-gherkin-script_sqat-columbus\/"},"modified":"2022-03-06T18:42:25","modified_gmt":"2022-03-06T23:42:25","slug":"using-parameters-in-bdd-gherkin-script_sqat-columbus","status":"publish","type":"post","link":"https:\/\/centricconsulting.com\/blog\/using-parameters-in-bdd-gherkin-script_sqat-columbus\/","title":{"rendered":"Using Parameters in Your BDD Gherkin Script"},"content":{"rendered":"

Learn how to use Parameters in our How-To-Guide on applying Cucumber\u00a0to lead your agile testing team\u00a0down the path to\u00a0automation.<\/h2>\n

Blog six\u00a0of a series<\/a>.<\/em><\/p>\n

As the number of Scenarios in your project increases, so does the need to make them easy to maintain.<\/p>\n

Brittle Scenarios, those which have hardcoded values, require multiple steps to accomplish the same task. Fortunately, Cucumber has provided an elegant solution for agile testing teams.<\/p>\n

How to use Parameters in BDD Gherkin Script<\/h2>\n

Let\u2019s work with the following BDD Gherkin Script:<\/p>\n

\"BDD<\/p>\n

To demonstrate how parameters work, I am going to write this script out in an imperative format rather than declarative. This will expose the individual steps:<\/p>\n

\"BDD<\/p>\n

You will notice the steps are highlighted in yellow. If you recall, that is Cucumber telling us it cannot find corresponding step definitions for each. So we need to create the corresponding step definitions (you can add these to the existing step definitions file).<\/p>\n

If you ran the .feature file and copy and pasted the four new steps into the step definition file, you will notice a lot of red:<\/p>\n

\"BDD<\/p>\n

Hovering the cursor over the red tells you that \u201cseveral step definitions with the same name were found.\u201d And if you look at each step, you will see that Cucumber is right. Each step is identical. So it doesn\u2019t know which one to use.<\/p>\n

Working With Step Definitions and Parameters<\/h2>\n

As your Cucumber framework grows, these kind of error messages can be frustrating because the matching step definition might be hard to find. Fortunately for us, our Cucumber framework is still small. The solution is to simply remove three of the four duplicate steps.<\/p>\n

Your step definition has two parameters:<\/p>\n

    \n
  • arg1, which takes the text entered<\/li>\n
  • arg2, which takes the field you wish to enter the text into.<\/li>\n<\/ul>\n

    The values of each are handled by Regular Expressions. We\u2019re not going to get into Regular Expressions here because they are far too complicated.<\/p>\n

    Just know that when we use the parameter (.*?) it will allow any value to be passed to the step definition. The value being passed down will be treated as a String variable by Cucumber regardless of what you pass.<\/p>\n

    The values captured in arg1 and arg2 can now be manipulated and used by WebDriver. That means arg1 can be directly passed to WebDriver; arg2, however, needs some manipulation. To do this, I will use a simple if-then-else statement:<\/p>\n

    \"BDD<\/p>\n

    Go ahead and run your .feature file. It should be all green. You have just created a step definition that can be used to populate ANY text field you come across on the web.<\/p>\n

    Parameters can be an extremely powerful and useful tool for agile testing teams. They enable you to write similar steps in a specific way so the step definition can be reused. As a result, scripts can be generated very fast and automated equally as fast.<\/p><\/blockquote>\n

    I would strongly encourage you to read more on Regular Expressions (regex.com) and read our blog on Advanced Cucumber Parameters.<\/p>\n

    In our next installment of this series, learn\u00a0how to run Cucumber using Rake.<\/em><\/h3>\n","protected":false},"excerpt":{"rendered":"

    Learn how to use Parameters in your BDD Gherkin Script in our How-To-Guide on applying Cucumber to automate testing.<\/p>\n","protected":false},"author":63,"featured_media":16463,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_oasis_is_in_workflow":0,"_oasis_original":0,"_oasis_task_priority":"","_relevanssi_hide_post":"","_relevanssi_hide_content":"","_relevanssi_pin_for_all":"","_relevanssi_pin_keywords":"","_relevanssi_unpin_keywords":"","_relevanssi_related_keywords":"","_relevanssi_related_include_ids":"","_relevanssi_related_exclude_ids":"","_relevanssi_related_no_append":"","_relevanssi_related_not_related":"","_relevanssi_related_posts":"","_relevanssi_noindex_reason":"","footnotes":""},"categories":[1],"tags":[],"coauthors":[15012],"acf":[],"publishpress_future_action":{"enabled":false,"date":"2024-07-21 22:39:45","action":"change-status","newStatus":"draft","terms":[],"taxonomy":"category"},"_links":{"self":[{"href":"https:\/\/centricconsulting.com\/wp-json\/wp\/v2\/posts\/13540"}],"collection":[{"href":"https:\/\/centricconsulting.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/centricconsulting.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/centricconsulting.com\/wp-json\/wp\/v2\/users\/63"}],"replies":[{"embeddable":true,"href":"https:\/\/centricconsulting.com\/wp-json\/wp\/v2\/comments?post=13540"}],"version-history":[{"count":0,"href":"https:\/\/centricconsulting.com\/wp-json\/wp\/v2\/posts\/13540\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/centricconsulting.com\/wp-json\/wp\/v2\/media\/16463"}],"wp:attachment":[{"href":"https:\/\/centricconsulting.com\/wp-json\/wp\/v2\/media?parent=13540"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/centricconsulting.com\/wp-json\/wp\/v2\/categories?post=13540"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/centricconsulting.com\/wp-json\/wp\/v2\/tags?post=13540"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/centricconsulting.com\/wp-json\/wp\/v2\/coauthors?post=13540"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}