0

I have a form that focuses a different component based on state when it loads. Is there a way with WicketTester, or some other Wicket test tool, to determine which component currently has focus?

For example, a form with username and password. When username is present then the password field gets focus and when it is not present username field gets focus.

Here is how I set focus in a temporary Behavior...

response.render(OnDomReadyHeaderItem.forScript("document.getElementById('" + component.getMarkupId() + "').focus();"));
JeredM
  • 897
  • 1
  • 14
  • 25

1 Answers1

1

since you use an headerItem to set the focus, you could test the html of the last rendered page. For example:

String responseTxt = tester.getLastResponse().getDocument();
assertTrue(responseTxt.contains("document.getElementB‌​yId('componentId').focus();"));
Andrea Del Bene
  • 2,521
  • 1
  • 15
  • 20
  • 1
    The code you wrote will tell me if the control is in the page, but not if it has focus. This did give me an idea that seems to be working well: String id = tester.getComponentFromLastRenderedPage("path:username").getMarkupId(); assertTrue(tester.getLastResponse().getDocument().contains("document.getElementById('" + id + "').focus()")); I don't know if this is what you were meaning or how stable this check will remain. Thanks for your help! – JeredM Apr 27 '17 at 15:53
  • 1
    Yes, my example was a little misleading, but I exactly meant what you did. I will edit my answer – Andrea Del Bene Apr 28 '17 at 08:40