Skip to content

Instantly share code, notes, and snippets.

@ralfstx
Created June 16, 2013 11:41
Show Gist options
  • Save ralfstx/5791804 to your computer and use it in GitHub Desktop.
Save ralfstx/5791804 to your computer and use it in GitHub Desktop.
A phase listener for RAP to create additional DOM attributes for UI tests.
/*******************************************************************************
* Copyright (c) 2013 EclipseSource.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Ralf Sternberg - initial API and implementation
******************************************************************************/
package org.eclipse.rap.demo.uitest;
import org.eclipse.rap.rwt.RWT;
import org.eclipse.rap.rwt.client.service.JavaScriptExecutor;
import org.eclipse.rap.rwt.lifecycle.*;
import org.eclipse.swt.internal.widgets.IDisplayAdapter;
import org.eclipse.swt.internal.widgets.WidgetTreeVisitor;
import org.eclipse.swt.internal.widgets.WidgetTreeVisitor.AllWidgetTreeVisitor;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Widget;
/**
* A phase listener that creates an additional DOM attribute for every widget. The value for this
* DOM attribute can be set using <code>Widget.setData( TEST_DATA_KEY, customId )</code>. that has
* to be done immediately after creating the widget.
*/
@SuppressWarnings( "restriction" )
public class UITestPhaseListener implements PhaseListener {
/*
* The DOM attribute on the client to set the test id to
*/
private static final String DOM_ATTRIBUTE = "testId";
/*
* The key to use for Widget.getData( key ) to read the test ID for a widget
*/
private static final String TEST_DATA_KEY = "TEST_ID";
@Override
public void beforePhase( PhaseEvent event ) {
WidgetTreeVisitor visitor = createWidgetTreeVisitor();
for( Shell shell : getAllShells() ) {
WidgetTreeVisitor.accept( shell, visitor );
}
}
@Override
public void afterPhase( PhaseEvent event ) {
}
@Override
public PhaseId getPhaseId() {
return PhaseId.RENDER;
}
private static WidgetTreeVisitor createWidgetTreeVisitor() {
WidgetTreeVisitor visitor = new AllWidgetTreeVisitor() {
@Override
public boolean doVisit( Widget widget ) {
processWidget( widget );
return true;
}
};
return visitor;
}
private static void processWidget( Widget widget ) {
String testId = ( String )widget.getData( TEST_DATA_KEY );
if( testId != null ) {
WidgetAdapter adapter = WidgetUtil.getAdapter( widget );
if( !adapter.isInitialized() ) {
applyTestId( adapter.getId(), testId );
}
}
}
private static void applyTestId( String widgetId, String testId ) {
JavaScriptExecutor executor = RWT.getClient().getService( JavaScriptExecutor.class );
executor.execute( createJsCode( widgetId, testId ) );
}
private static String createJsCode( String widgetId, String testId ) {
StringBuilder code = new StringBuilder();
code.append( "var widget = rwt.remote.ObjectRegistry.getObject('" );
code.append( widgetId );
code.append( "');" );
code.append( "widget.setHtmlAttribute('" );
code.append( DOM_ATTRIBUTE );
code.append( "', '" );
code.append( testId );
code.append( "');" );
return code.toString();
}
private static Shell[] getAllShells() {
return Display.getCurrent().getAdapter( IDisplayAdapter.class ).getShells();
}
}
@eiswind
Copy link

eiswind commented Jul 28, 2013

Hey Ralf, finally found some time to check this out. Works like a charm.
Thx!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment