Skip to main content
Home
  • Tutorials
    • Quality Assurance
    • Software Development
    • Machine Learning
    • Data Science
  • About Us
  • Contact
programsbuzz facebook programsbuzz twitter programsbuzz linkedin
  • Log in

Main navigation

  • Tutorials
    • Quality Assurance
    • Software Development
    • Machine Learning
    • Data Science
  • About Us
  • Contact

Cucumber Optional and Alternative Text

Profile picture for user devraj
Written by devraj on 02/12/2021 - 00:22

There is no need to write a new step definition in Cucumber if your step differs by synonyms or singular plural. You can reduce lots of effort by using the Cucumber and Regular Expression technique. In this article, we will discuss two such strategies of Cucumber Expressions: Optional Text and Alternative Text.

Table of Contents

  1. Cucumber Optional Text
  2. Cucumber Alternative Text
  3. Video Tutorial

Cucumber Optional Text

Sometimes you have 1 item in your cart or more than one. It is grammatically incorrect to write "I have 1 items in cart" or "I have 2 item in cart," which we generally do to avoid creating duplicate step definitions. In such cases, you can make the plural s optional using Optional Text in Cucumber Expressions by using Round Brackets (). You can have one Step Definition for below two steps:

Then I have 1 item in cart
Then I have 2 items in cart

Step Definition

@Then("I have {int} item(s) in cart")	
public void teststep3() {
}

Cucumber Alternative Text

Consider a case when someone writes a product instead of the item in your step. Again, you don't need to create a new step definition; you can use the forward-slash (/) in your step definition to relax your language. For Example, below four steps you can map to single Step Definitions.

Then I have 1 item in cart
Then I have 1 product in cart
Then I have 2 items in cart
Then I have 2 products in cart

Step Definition

@Then("I have {int} item(s)/product(s) in cart")	
public void teststep3() {
}
Related Content
Cucumber Java Tutorial
Cucumber Expressions
Cucumber Step Definitions Regex Examples
Tags
Cucumber Java
  • Log in or register to post comments

Choose Your Technology

  1. Agile
  2. Apache Groovy
  3. Apache Hadoop
  4. Apache HBase
  5. Apache Spark
  6. Appium
  7. AutoIt
  8. AWS
  9. Behat
  10. Cucumber Java
  11. Cypress
  12. DBMS
  13. Drupal
  14. GitHub
  15. GitLab
  16. GoLang
  17. Gradle
  18. HTML
  19. ISTQB Foundation
  20. Java
  21. JavaScript
  22. JMeter
  23. JUnit
  24. Karate
  25. Kotlin
  26. LoadRunner
  27. matplotlib
  28. MongoDB
  29. MS SQL Server
  30. MySQL
  31. Nightwatch JS
  32. PactumJS
  33. PHP
  34. Playwright
  35. Playwright Java
  36. Playwright Python
  37. Postman
  38. Project Management
  39. Protractor
  40. PyDev
  41. Python
  42. Python NumPy
  43. Python Pandas
  44. Python Seaborn
  45. R Language
  46. REST Assured
  47. Ruby
  48. Selenide
© Copyright By iVagus Services Pvt. Ltd. 2023. All Rights Reserved.

Footer

  • Cookie Policy
  • Privacy Policy
  • Terms of Use