Custom Javascript

Injecting Custom javascript

CWB provides the ability to inject custom javascript into SAHI js. 

To do so, you should register your custom javascript resources in the customJavascriptInjector bean:

    • Session-scoped javascript will be available on all webpages in the entire session. This is the default scope. 
    • Scenario-scoped javascript is only available in the given scenario. It is loaded by tagging your scenario with an appriopriate tag: "@IncludeCustomJs:<comma-separated resourcekeys>". 

 

Registering custom javascript in CwbSahiConfiguration
@Value("classpath:/com/foreach/cuke/sahi/util/cwb.js")
private Resource javascriptCwb;
 
@PostConstruct
public void registerCustomJavascript(){
	customJavascriptInjector.register( "cwb", javascriptCwb );
	customJavascriptInjector.register( "tinymce", javascriptTinyMce, CustomJavascriptInjector.Scope.SCENARIO );
}
Loading scenario-scoped javascript
@IncludeCustomJs:TinyMce
Feature: Test TinyMCE 3 functionality

 

For an example of a specific use case, read this blog post.

Breaking change: TinyMCE support

This feature is breaking with regard to TinyMCE. If you already wrote some tests that depend on CWB supporting TinyMCE , you should either add the tag '@IncludeCustomJs:TinyMce' to all your features or scenario's that make use of TinyMCE, or, in order to make TinyMCE support available in all scenario's, you could include the following snippet in your application's Spring configuration class:

 

Loading scenario-scoped javascript
@Value("classpath:/com/foreach/cuke/sahi/util/tinymce.js")
private Resource javascriptTinyMce;
 
@Autowired
private CustomJavascriptInjector customJavascriptInjector;
 
@PostConstruct
public void overrideDefaultTinyMceJavascriptConfiguration() {
 customJavascriptInjector.register( "tinymce", javascriptTinyMce );
}