6e) CWB for developers

This page is targeted towards developers who maintain a CWB based project. Information concerning modifications on the CWB library itself is found under 02 - development

 

How can I define my own steps in combination with the CWB libraries?

Class names have to be in the package com.foreach.cuke and in the folder test/src (under no circumstance in main!)
CWB is build on top of Spring so autowiring, etc.. is available.
To support Java classes in other packages the cucumber.xml file must be elaborated with a component scan.
And the pom.xml must be adpted with the glue parameter.

Voorbeeld basic step class
/**
 * CWB provides a base class AbstractSteps that accommodates the most common beans straight away:
 * - Browser browser
 * - CtfEvaluationContext context
 * - SpelExpressionExecutor spel
 * - ElementHandleService elementHandleService
 *
 * For simplicity, protected fields will be directly available instead of via getter.
 */
public class Examplesteps extends AbstractSteps
{
	@And("^do something$")
	public void do_something() throws Throwable {
		LOG.info( "doing something..." );	// LOG instance is created in the parent
		browser.link('somelink').click();	// browser bean is autowired in the parent
		...
	}
} 

SAHI is used abundantly so a good knowledge of SAHI is required. Please consult our tips and tricks

Which CWB beans are available?

All beans can be autowired, the most relevant ones are listed below

 

BrowserReference to the SAHI browser
CtfEvaluationContext

Offers an entrance to the properties and variabels who are used.

Fe: ${test.url} is the equivalent of context.lookup("test.url").

SpelExpressionExecutor

Used to execute EL statements, and to allow that a parameter has an EL script or lookup.

Fe: "#{id.next} ${my.variable}" can be replaced by executor.getValue("#{id.next} ${my.variable}").

ElementHandleService

SAHI ElementStub based on ElementDescriptor. Uses all registered ElementHandlers.

Fe: ElementStub stub = elementHandleService.find(descriptor);

StringMatcherCan be used to compare which is the JavaScript regex syntax allowed, as well as taking into account the string.ignore.case ctf.properties from the strings.
RichtTextTinyMCEHelper object from code to do limited adjustments on a TinyMCE component. Assumes that the html id of the component is known.

 

How can I define my own elements in a project?

In most cases, you will have a custom ElementHandler implementation to create a new element, so you can use them in several standard steps automatically.
An element is typically identified by a ElementDescriptor object, which can have optionally a Sahi ElementStub as parent . Defining own ElementHandlers is quite simple, the best way to learn is to watch. Some of the code examples

Final tests make full use of Sahi ElementStub objects. A ElementStub object can exist, but this does not mean that it also corresponds to an existing object on the page. In most cases, the different methods will always be a ElementStub rendering and should be tested with elementStub.exists () to see if it is valid, and not by comparing it to null.

 

Code voorbeelden