Skip to content

Step template and step template config

Ivan edited this page Dec 5, 2019 · 3 revisions

Step template and step template config

Step template consists of static and dynamic parts. Dynamic part should be put inside brackets {} everything else is a static part. During execution a dynamic part will be replaced by provided method argument value if it matches with the provided pattern. To understand how to provide a correct pattern we need to observe an example.

Given 2 java classes Item with name of type String and Container with name of type String and items of type Item:

    public class Item {
	private String name;

	public Item(String name) {
		this.name = name;
	}

        @Override
	public String toString() {
		return "Item";
	}
    }

    public class Container {
	private List<Item> items;
	private String name;

	public Container(List<Item> items, String name) {
		this.items = items;
		this.name = name;
	}

        @Override
	public String toString() {
		return "Container";
	}
    } 

Also given a test:

        @Test
	public void containerTest() {
		Item item = new Item("ball");
		List<Item> items = new ArrayList<>();
		items.add(item);
		Container container = new Container(items, "box");
	}

We have Item with name='ball', Container with name='box' and provided Item in the items list.
Simple Step template example:

        @Step("Check container - {container}")
	private void checkContainerWithSimplePattern(Container container) {

	}

Dynamic part '{container}' matches with method parameter name='container' so argument value will be converted to String, Container toString() method returns 'Container' so resulted Step value will be: "Check container - Container".

If we want to get Container name we have to update our pattern with field name of required parameter:

        @Step("Check container - {container.name}")
	private void checkContainerWithNamePattern(Container container) {

	}

So now pattern matches with 'container' parameter and current pattern should be replaced by field name value of provided argument. Current Container object name="box" so resulted Step value will be:"Check container - box".

We can also convert collections:

        @Step("Check container - {container.items.name}")
	private void checkContainerWithCollectionPattern(Container container) {

	}

In this method we retrieve name value for each element of items field. We have only one Item inside items field with name="ball" so resulted Step value will be:"Check container - [ball]".

If we need an original method name in the resulted Step value we can specify predefined pattern={method}.

@StepTemplateConfig is required for resulted Step value formatting. You can set [delimiters, start symbol, end symbol] for Arrays and Collections which values should be converted to resulted Step value. To prevent collisions of method parameter with name="method" with default {method} pattern you can define your own pattern using methodNameTemplate field in the StepTemplateConfig:

        @Step(value = "My {method} explanation using test method - {m}", templateConfig = @StepTemplateConfig(methodNameTemplate = "m"))
	private void checkMethod(String method) {

	}

Resulted Step value will be:"My Bubble sorting explanation using test method - checkMethod".