How can i get widget which has focus

WindowTester allows you to easily create and run unit tests for every GUI they build. It can also be used to generate system level tests.

Moderators: gnebling, Eric Clayberg, Dan Rubel, keertip, Phil Quitslund

How can i get widget which has focus

Postby karthik.intels12 » Thu Jul 08, 2010 8:13 am

Hi,

I have a widget in a editor. I have set focus on text box and I need to get that widget. i have used Some API to get that widget from UI which it has focus. But i want the text which is present in that text box. Is that possible to get the text from the widget. I cannot use named widgetlocator for that textbox because it has created very generic way across the tool.


I have attached the Snippets. Just go through it and give me ur valuable suggestion...

// Using mouse click key board shortcut i'm somehow setting focus on widgets

this.ui.keyClick(WT.SHIFT + WT.TAB);
this.ui.keyClick(WT.CTRL, 'A');
this.ui.keyClick(WT.DEL);
this.ui.enterText("100");


// In thread I'm trying to access that widget
Display.getDefault().syncExec(new Runnable() {
@Override
public void run() {


// here getFocusControl() returning the widget which has current focus and i'm getting that widget as text too.. but i don have any API to get text from the widget

Control focusControl = Display.getCurrent().getFocusControl();
//Shell shell = focusControl.getShell();
// String text = shell.getText();
//System.out.println(text);
}
});



Now i need that 100 to assert check... Please give me ur valuable suggestion.
Thanks and Regards,
Karthik.M.S
karthik.intels12
 
Posts: 35
Joined: Wed Mar 31, 2010 4:37 am

Re: How can i get widget which has focus

Postby Phil Quitslund » Thu Jul 08, 2010 9:27 am

To get the text of a generic control, you'll have to do some conditional testing since there is no convenient interface to cast controls with text to. In other words, something like this:

Code: Select all
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;


public class TextUtil {
   
   public static String getText(final Control c){
      final String text[] = new String[1];
      Display.getDefault().syncExec(new Runnable() {
         public void run() {
            if (c instanceof Button)
               text[0] = ((Button)c).getText();
            else if (c instanceof Group)
               text[0] = ((Group)c).getText();
            else if (c instanceof Label)
               text[0] = ((Label)c).getText();
            else if (c instanceof Text)
               text[0] = ((Text)c).getText();
            //etc
         }
      });
      return text[0];
   }
}

To make a safe assertion, you'll want to wrap the text access in a condition something like this:

Code: Select all
import org.eclipse.swt.widgets.Control;

import com.windowtester.runtime.condition.ICondition;
import com.windowtester.runtime.util.StringComparator;

public class ControlHasTextCondition implements ICondition {

   private final Control c;
   private final String expected;

   public ControlHasTextCondition(Control c, String expected) {
      this.c = c;
      this.expected = expected;
   }

   /*
    * (non-Javadoc)
    *
    * @see com.windowtester.runtime.condition.ICondition#test()
    */
   public boolean test() {
      return StringComparator.matches(TextUtil.getText(c), expected);
   }

}

Putting it all together, you'd get an assertion like this:

Code: Select all
Control focusControl = Display.getCurrent().getFocusControl();
ui.assertThat(new ControlHasTextCondition(focusControl, expectedText);

where expectedText is the text you expect the focus control to have.

For more on conditions, see this doc:

http://downloads.instantiations.com/WindowTesterDoc/integration/latest/docs/html/reference/conditions.html
--
Phil Quitslund
Software Engineer
Google, Inc.
Phil Quitslund
Moderator
 
Posts: 491
Joined: Fri Apr 28, 2006 6:26 am

Re: How can i get widget which has focus

Postby karthik.intels12 » Fri Jul 09, 2010 12:20 am

Ya its working fine... Thank you very much:)
Thanks and Regards,
Karthik.M.S
karthik.intels12
 
Posts: 35
Joined: Wed Mar 31, 2010 4:37 am

Re: How can i get widget which has focus

Postby Phil Quitslund » Fri Jul 09, 2010 8:59 am

Good deal. Thanks for following up!
--
Phil Quitslund
Software Engineer
Google, Inc.
Phil Quitslund
Moderator
 
Posts: 491
Joined: Fri Apr 28, 2006 6:26 am


Return to Window Tester

Who is online

Users browsing this forum: No registered users and 1 guest