Search code examples
gwtjunitgwttestcase

GWT TestCase: Simulating clicking a button on my page


I'm using GWT 2.4 with JUnit 4.8.1. When writing my class that extends GWTTestCase, I want to simulate clicking on a button on the page. Currently, in my onModuleLoad method, this button is only a local field ...

public void onModuleLoad() {
    final Button submitButton = Button.wrap(Document.get().getElementById(SUBMIT_BUTTON_ID));
    ...     

    // Add a handler to send the name to the server
    GetHtmlHandler handler = new GetHtmlHandler();
    submitButton.addClickHandler(handler);

How do I simulate clicking on this button from the GWTTestCase? Do I have to expose this button as a public member accessor is there a more elegant way to access it? Here is what I have in my test case so far ...

public class GetHtmlTest extends GWTTestCase {

    // Entry point class of the GWT application being tested.
    private Productplus_gwt productPlusModule;

    @Override
    public String getModuleName() {
        return "com.myco.clearing.productplus.Productplus_gwt";
    }

    @Before
    public void prepareTests() { 
        productPlusModule = new Productplus_gwt();
        productPlusModule.onModuleLoad();
    }   // setUp

    @Test
    public void testSuccessEvent() { 
        // TODO:  Simulate clicking on button
    }   // testSuccessEvent

}

Thanks, - Dave


Solution

  • It can be as easy as buttonElement.click() (or ButtonElement.as(buttonWidget.getElement()).click(), or ButtonElement.as(Document.get().getElementById(SUBMIT_BUTTON_ID)).click())

    But remember that a GWTTestCase doesn't run in your own HTML host page, but an empty one, so you'll first have to insert your button within the page before simulating your module's load.