Tuesday, August 20, 2013

How to cancel an SWT key event?

KeyEvent in SWT has a doit member variable.
Setting it to false cancels any further handling of the event.

public void keyPressed(KeyEvent e) {
              handleKeyPress(e);
              e.doit = false;// cancels the event
}

Sunday, August 18, 2013

SWTUtils doesn't support invoking methods with parameter

SWTUtils.invokeMethod allows you to invoke only methods with no parameters.

Here's a customized version of the method that allows you to invokes methods on objects with parameters

    public static Object invokeMethod(final Object object, String methodName,
            final Object[] params) throws NoSuchMethodException,
            IllegalAccessException, InvocationTargetException {
        Class[] paramTypes = getParamTypes(params);
        final Method method = object.getClass().getMethod(methodName,
                paramTypes);
        Widget widget = null;
        final Object result;
        if (object instanceof Widget) {
            widget = (Widget) object;
            result = UIThreadRunnable.syncExec(widget.getDisplay(),
                    new Result<Object>() {
                        @Override
                        public Object run() {
                            try {
                                return method.invoke(object, params);
                            } catch (Exception niceTry) {
                            }
                            return null;
                        }
                    });
        } else {
            result = method.invoke(object, params);
        }

        return result;
    }

Still this method only supports Object[] parameter, it doesn't support any primitive parameter like int, boolean.



VoidResult and its use

asyncExec method of UIThreadRunnable takes a VoidResult parameter.

It's useful for running code in UIThread which involves inter thread communication.

VoidResult interface has a method run().
Put the code in run() you want to execute in UI Thread.
for example you want to call setMaximized(..) method on a Shell you can do this.

Maximize Eclipse active window from SWTBot

Here's a simple way of achieving above
   
private void maximizeActiveWindow() {
        final Shell activeShell = bot.activeShell().widget;
        VoidResult maximizeShell = new VoidResult() {
            @Override
            public void run() {
                    activeShell.setMaximized(true);
            }
        };
        syncExec(maximizeShell);
    }

There's another way of doing above here.

Tuesday, June 18, 2013

Running UI Tests from Jenkins

Usually Jenkins is run as a service while automated UI testing requires active user session.

We faced the problem when we implemented automated UI testing integration with Jenkins.

The solution we applied is installing jenkins.war in tomcat and run tomcat from a user session.

The problem isn't finished there. UI testing requires you always logged in and desktop unlocked.

We need to keep the session alive so that whenever a build starts it can interact with desktop.
We use VDI(Virtual Desktop Infrastructure) in Hyper-V which can keep a session always alive and unlocked.

But when we remotely connect to the session for some maintenance and disconnect again, the session gets locked again.
We used to manually unlock the session from the host server until we found the following solution in stackoverflow .

To keep to session alive after disconnection from Remove Desktop.
Enter following command in cmd prompt.
TSCON {sessionid | sessionname} /DEST:CONSOLE
You can get the sessionid or sessionname with following command
query session
 Example: TSCON 1 /DEST:CONSOLE
 

Tuesday, April 23, 2013

Surefire doesn't run the product/application specified

The product or application specified in configuration section of surefire are most probably on another bundle.
  1. Add the bundle as dependency to the plugin (in MANIFEST.MF) for which you have written the surefire configuration.
  2. Also add org.eclipse.equinox.ds if you have declared the product/application through extension points.

Now your test plugin should be able to access the application/product.

In case you have the extension points and tests in the same plugin/bundle you still have to do 2nd step.