NamedWidgetLocator doesn't find named widget

35 views
Skip to first unread message

Julia Körvers

unread,
Nov 24, 2014, 9:09:43 AM11/24/14
to windowte...@googlegroups.com
Hi there,

I'm testing a RCP-application and I have the problem, that a few textfields are not located by the LabeledTextLocator, but by the SWTWidgetLocator. The Problem is, that these textfields must be checked according to their user input. The SWTWidgetLocator does not implement the HasTextCondition. Therefore I tried to use the NamedWidgetLocator to locate the textfields. I named one of these textfields to check if it works. Unfortunately it doesn't work. 

The widget is declared as follows: IntegerInput veText = new IntegerInput(ekhekGroup, SWT.Border);
I named the widget as follows:      veText.setData("name", "txtVE");
In the testcode I wrote:                 ui.assertThat(new NamedWidgetLocator("txtVE").hasText("5"));

The test stops in fact of the WidgetNotFoundException: Widget NOT Found: NamedWidgetLocator("txtVE").

What am I doing wrong? Can someone help me?

Thanks in advance,
Julia 

Julia Körvers

unread,
Nov 25, 2014, 5:21:02 AM11/25/14
to windowte...@googlegroups.com
Hi everyone,

any update on this? 

Kind regards,
Julia

Fred G

unread,
Nov 25, 2014, 7:52:40 AM11/25/14
to windowte...@googlegroups.com
Hi Julia,

I guess IntegerInput is a custom class that you have implemented?
Please try to test the NamedWidgetLocator with a standard SWT/JFace class like Button, Label or Text.
That should help to isolate the problem.

Apart from that, you could use the following method to get a text from SWTWidgetLocator:

    public String getTextContent(SWTWidgetLocator textLocator) throws WidgetSearchException {
        final IWidgetReference textRef = (IWidgetReference) getUI().find(textLocator);
        final AtomicReference<String> result = new AtomicReference<String>();
        Display.getDefault().syncExec(new Runnable() {
            public void run() {
                String text = null;
                if (textRef.getWidget() instanceof StyledText) {
                    text = ((StyledText) textRef.getWidget()).getText();
                } else if (textRef.getWidget() instanceof CLabel) {
                    text = ((CLabel) textRef.getWidget()).getText();
                } else if (textRef.getWidget() instanceof Label) {
                    text = ((Label) textRef.getWidget()).getText();
                } else if (textRef.getWidget() instanceof Text) {
                    text = ((Text) textRef.getWidget()).getText();
                }
                if (text != null) {
                    result.set(text);
                } else {
                    result.set(""); //$NON-NLS-1$
                    System.err.println("Could not get text content from widget."); //$NON-NLS-1$
                }
            }
        });
        return result.get();
    }

Instead of ui.assertThat([...]) you'd have to use a normal JUnit assertion like
assertEquals("1", getTextContent(new SWTWidgetLocator(IntegerInput.class));

Hope that helps,

Fred

Julia Körvers

unread,
Nov 25, 2014, 7:59:41 AM11/25/14
to windowte...@googlegroups.com
Hi everyone,

I found the mistake! I imported the Swing-Version of the NamedWidgetLocator instead of the SWT-Version.


Greetings,
Julia

Am Montag, 24. November 2014 15:09:43 UTC+1 schrieb Julia Körvers:

Julia Körvers

unread,
Nov 25, 2014, 8:02:59 AM11/25/14
to windowte...@googlegroups.com
Hi Fred,

I just found out what went wrong. Thanks anyway for your answer.

Greetings,
Julia
Reply all
Reply to author
Forward
0 new messages