GWT TestCase: Simulating clicking a button on my page

3.6k views Asked by At

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

3

There are 3 answers

0
Thomas Broyer On BEST ANSWER

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.

1
Gael On

gwt-test-utils seems to be the perfect framework to answer your need. Instead of inheriting from GWTTestCase, extend the gwt-test-utils GwtTest class and implement your click test with the Browser class, like shown in the getting starting guide :

  @Test
public void checkClickOnSendMoreThan4chars() {
   // Arrange
   Browser.fillText(app.nameField, "World");

   // Act
   Browser.click(app.sendButton);

   // Assert
   assertTrue(app.dialogBox.isShowing());
   assertEquals("", app.errorLabel.getText());
   assertEquals("Hello, World!", app.serverResponseLabel.getHTML());
   assertEquals("Remote Procedure Call", app.dialogBox.getText());
}

If you want to keep your button private, you'd be able to retrieve it by introspection. But my advice is to make you view's widgets package protected and to write your unit test in the same package so it could access them. It's more convinent and refactoring-friendly.

gwt-test-utils provide introspection convinence. For example, to retrieve the "dialogBox" field which could have been private, you could have do this :

 DialogBox dialogBox = GwtReflectionUtils.getPrivateFieldValue(app, "dialogBox");

But note that using GwtReflectionUtils is not mandatory. gwt-test-utils allows you to use ANY java classes in GWT client side tests, without restriction :)

0
Saeed Zarinfam On

You can do it like this:

YourComposite view = new YourComposite();
RootPanel.get().add(view);

view.getSubmitButton.getElement().<ButtonElement>cast().click();