Parameterization (regular expressions (regex))(.+)

Regular Expressions: 

Regex is a sequence of characters that forms a search pattern. In Cucumber, regex allows you to capture variable values from the Gherkin steps, making your tests more versatile.


#Regular Expressions:


parameterization.feature


Feature: Application Login for different users

@RegularExp

Scenario: Login with parameters

When user logs in with username "sunny" and password "sunny1234"

And user logs in with username "taraka" and password "taraka1234"



parameterizationSteps.java


package stepDefinitions;


import java.util.Map;


import io.cucumber.java.en.Given;

import io.cucumber.java.en.Then;

import io.cucumber.java.en.When;


public class parameterizationSteps {

@When("^user logs in with username \"([^\"]*)\" and password \"([^\"]*)\"$")

public void user_logs_in_with_username_and_password(String username, String password) {

System.out.println("Username: " + username + ", Password: " + password);

}

}



parameterizationTestNGRunner.java


package cucumberOptions;

import io.cucumber.testng.AbstractTestNGCucumberTests;

import io.cucumber.testng.CucumberOptions;


@CucumberOptions(features = "src/test/java/features", glue="stepDefinitions",tags="@RegularExp", monochrome=true)

public class parameterizationTestNGRunner extends AbstractTestNGCucumberTests{

}




Output:


Username: sunny, Password: sunny1234

Username: munny, Password: sunny1234

PASSED: io.cucumber.testng.AbstractTestNGCucumberTests.runScenario("Login with parameters", "Application Login for different users")

Runs Cucumber Scenarios


===============================================

Default suite

Total tests run: 1, Passes: 1, Failures: 0, Skips: 0

===============================================



Step Definition Explanation

The step definition uses a regex pattern to match the Gherkin step.

The pattern ^user logs in with username \"([^\"]*)\" and password \"([^\"]*)\"$ breaks down as follows:


  • ^ asserts the start of the string.
  • user logs in with username is a literal match.
  • \"([^\"]*)\" captures the username, allowing any characters except for a double quote.
  • and password is another literal match.
  • \"([^\"]*)\" captures the password in the same manner.
  • $ asserts the end of the string.
  • When this step is executed, the captured values for username and password are passed as arguments to the method user_logs_in_with_username_and_password. The method then prints these values to the console.


=====================================================================


Ex: regex (.+)


When user login into application with "admin" and password "1234"

@When("user is login with {string} and password {string} combination")

it will accept only string data type values only


(.+) --- it will accept any datatype

When user login into application with admin and password 1234



Feature: Application Login


@Adminlogin

Scenario: Admin Page default Login

Given user is on NetBanking Landing Page

When user login into application with admin4567 and password 1234

Then homepage is displayed

And cards are displayed



@When("user login into application with (.+) and password (.+)$")

public void user_login_into_application_with_and_password(String username, String password) {

System.out.println(username + " and password in " +password );

}


Output:


User landed on netbanking page

admin4567 and password in 1234

Home page is displayed

Cards are displayed


===============================================

Default suite

Total tests run: 1, Passes: 1, Failures: 0, Skips: 0

===============================================


Explanation Annotation @When: This annotation indicates that the following method will execute when a specific condition is met in the BDD scenario (usually defined in a .feature file).

In this case, it’s triggered when a user attempts to log in with given credentials.

String Argument Matching: The string user login into application with (.+) and password (.+)$ is a regular expression that matches a specific format. (.+) captures one or more characters for the username and password.

The structure suggests that this line will be called with values that fit this format.\



Method Definition: public void user_login_into_application_with_and_password(String username, String password) { System.out.println(username + " and password in " + password); }

This method takes two parameters: username and password, both of type String. When this method is executed, it prints a message to the console showing the username and the password (although displaying passwords like this in a real application would be a security risk).