An advanced theme is the command interception. The basics of this technique comes from Aspect Oriented Programming.
The interceptor is basically the code which will be performed around invocation of selenium command.
This way, you can:
- execute code before/after the selenium command will be performed
- catch exceptions thrown when performing selenium command
For this purpose, you have to implement interface CommandInterceptor - it requires to implement method
where you implement the interception code.
If you will implement this method, you must additionally satisfy interceptor contract - the CommandContext#invoke() method must be invoked at least once - otherwise, after end of intercept(...) method, the CommandInterceptionException will be thrown and test will fail.
You get to disposition the CommandContext object in intercept method. On the context, you have to call the CommandContext#invoke() method. You can also obtain the current command name and it's arguments here.
The implementation of invocation nesting can be seen on CommandContext.
Registration and unregistration of interceptors to selenium commands is provided by CommandInterceptorProxy, which is the member of GrapheneSelenium.
On the CommandInterceptorProxy can be registered zero and more interceptors, which will be in sequence called to achieve desired functionality, whereas each interceptor's body is nested into the other, using CommandContext#invoke() method. The call of invoke for last of the interceptors in sequence will cause actual performing of the command.
The entry point for command execution, AjaxSelenium, contains one CommandInterceptionProxy, which triggers all the command through sequence of interceptors registered to it.
Two well-known implementation of CommandInterceptionProxy are RequestTypeGuard and AjaxAwareInterceptor.