Example: Custom steps with a table

In this example, a step is defined which  is going to look for a particular element in the parent container.In this case a span is which contains a specific error message. Makes maximum reuse of ElementDescriptor.

Use

 

Then the following fields in div "Levering" should have an error:
      | type    | locator  |
      | textbox | Postcode |
      | select  | Cadeau   |
And the following fields in list 1 should have an error:
      | type    | locator  | value 							|
      | textbox | Postcode | specific error message 		|
      | select  | Cadeau   | other specific error message	|

Java Code

	@Then("^the following fields in " + CommonActions.ELEMENT_REGEX + " should have an error:$")
	public void the_following_fields_in_should_have_an_error( String parentType,
	                                                          String parentLocator,
	                                                          Integer parentPosition,
	                                                          List<ElementDescriptor> descriptors ) throws Throwable {
		ElementStub parent = find( parentType, parentLocator, parentPosition );

		for ( ElementDescriptor descriptor : descriptors ) {
			descriptor.setParent( parent );
			ElementStub element = elementHandleService.find( descriptor );
			if ( element.exists() ) {
				ElementStub container = element.parentNode();
				ElementStub message = browser.span( "m-form__dialog m-form__dialog--error" ).in( container );

				if ( message.exists() ) {
					String expected = descriptor.getValue();
					if ( StringUtils.isEmpty( expected ) ) {
						expected = "Dit veld is verplicht";
					}
					assertTrue( "Field error does not match expected text",
					            stringMatcher.contains( message.getText(), expected ) );
				}
				else {
					fail( "Did not find error message for: " + descriptor );
				}
			}
			else {
				fail( "Could not find field: " + descriptor );
			}
		}
	}