简体   繁体   中英

Browser#getText() returns empty string

I found a very weird behavior of the SWT Browser widget:

@Test
public void test() throws Exception {
    Shell shell = new Shell();
    Browser browser = new Browser(shell, SWT.NONE);
    browser.setText("Hello World!");
    shell.open();

    Assert.assertEquals("Hello World!", browser.getText());
}

This test fails , because Browser#getText() returns an empty string.

An other question suggest it might be because the Browser is still loading the page, but using setText doesn't trigger loading (because the HTML is already there), and the ProgressListener never gets called, either.

The JavaDoc says: Returns a string with HTML that represents the content of the current page. Nothing about returning an empty string for some reason.

How do I get the text of a browser? (I need it for a test case like the above, so "waiting" for the Browser widget is out of the question. I'm not sure that would work either way.)

The code below prints the Browser 's text:

public static void main(String[] args)
{
    final Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());
    shell.setText("StackOverflow");

    Browser browser = new Browser(shell, SWT.NONE);
    browser.setText("THIS IS A TEST");

    shell.pack();
    shell.open();

    display.asyncExec(() -> System.out.println(browser.getText()));

    while (!shell.isDisposed())
    {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

Note that the text you get back isn't the same one you set. The browser adds the <html> , <head> and <body> tags:

<html><head></head><body>THIS IS A TEST</body></html>

I've never written a test for SWT before, but shouldn't you still have the SWT event loop in your code (the while(!shell.isDisposed()) bit)?

After opening the Shell you need to call Display.readAndDispatch() to ensure that all the queued events are handled (including setting the text on the Browser ):

@Test
public void test() throws Exception {
    final Shell shell = new Shell();
    final Browser browser = new Browser(shell, SWT.WEBKIT);
    browser.setText("Hello World!");
    shell.open();
    while (shell.getDisplay().readAndDispatch()) {
        // Loop here in case there is more than one event in the queue
        // Also, it may be advised to call Display.sleep() here as well
    }
    Assert.assertEquals("Hello World!", browser.getText());
}

Note that I had to add the SWT.WEBKIT style bit for it to run on my machine - you may not need that.

I'm honestly not sure why I didn't run into issues with HTML tags as mentioned in @Baz's answer, but the above test passes. Perhaps someone else can shed some light on that as I've never had to use the Browser widget.

As an aside, writing tests around SWT components is a huge pain, and if there's any way you can refactor or redesign your code so that you don't have to test widgets directly (especially when you have to create and open a Shell ), I would STRONGLY advise it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM