Search code examples
cucumberbddgherkin

Random string in cucumber scenarios


I am testing a GUI using cucumber. I need to test CRUD operations of the GUI.

When I write a scenario to create a new entity in GUI, I am unable to run multiple times, since the second time scenario fails because the ID I specified for the entity already exists (created in the first run) in the system the second time I run the test.

The system I am testing doesn't allow deleting entities. System needs to be started in a special mode to delete entities, so deleting the entity created after the test is not an option.

It would be great if I could use a random number for the entity id. For an example:

when user creates a new Branch with following values:
|Branch ID|<random_string_1>|
|Address|1, abc, def.|
|Telephone|01111111111|
And user searches for a branch by "Branch ID" = "<random_string_1>"
Then branch details should be as following
|Branch ID|<random_string_1>|
|Address|1, abc, def.|
|Telephone|01111111111|

Is there an option in cucumber to do something like this? Or, is there any other way I can achieve this?


Solution

  • In the end, I've added RandomStringTransformer class to test suite:

        public class RandomStringTransformer extends Transformer<String> {
            private static final Map<String, String> RANDOM_STRINGS = new HashMap<>();   //Key -> random string
            public static final RandomStringTransformer INSTANCE = new RandomStringTransformer();
    
            @Override
            public String transform(String input) {
                return transformString(input);
            }
    
            public DataTable transform(DataTable dataTable) {
                dataTable.getGherkinRows().forEach(dataTableRow -> dataTableRow.getCells().replaceAll(this::transformString));
                return dataTable;
            }
    
            private String transformString(String input) {
                final String[] inputCopy = {input};
                Map<String, String> replacements = new HashMap<>();
                Matcher matcher = Pattern.compile("(<random_string_[^>]*>)").matcher(input);
                while (matcher.find()) {
                    String group = matcher.group(0);
                    replacements.put(group, RANDOM_STRINGS.computeIfAbsent(group, key -> Utilities.getNextUniqueString()));
                }
                replacements.forEach((key, value) -> inputCopy[0] = inputCopy[0].replace(key, value));
                return inputCopy[0];
            }
        }
    

    And used the transformer in step definition:

        @When("^user creates a branch of name "([^"]*)" with following values$")
        public void branchIsCreatedWithDetails(@Transform(RandomStringTransformer.class) String branchName, DataTable fieldValues) {
            fieldValues = RandomStringTransformer.INSTANCE.transform(fieldValues);
            //Now, fieldValues table values and branchName are replaced with random values if they were in format <random_string_SOMETHING>
        }