1

I have an issue to understand how to do it correctly, as I could not find any solution on the net.

I try to test radio selection and click on a download link (bonus would be asserting the downloaded file).

My Code within the Panel is

exportXmlCartForm.add(createDownloadLink("downloadLinkId"));
final Form<Void> exportXmlCartForm = new Form<Void>("xmlCartFileExport");
final RadioGroup<String> group = new RadioGroup<String>("groupFileType", new sharedStringModel);
final Radio<String> radioPartslink = new Radio<String>("fileTypePARTSLINK", Model.of("pl"));
final Radio<String> radioPro = new Radio<String>("fileTypePRO", Model.of("pro"));
final Radio<String> radioCsv = new Radio<String>("fileTypeCSV", Model.of("csv"));
final Radio<String> radioXmlExport = new Radio<String>("fileXmlExport", Model.of("xml"));
group.add(radioPartslink, radioPro, radioCsv, radioXmlExport);
exportXmlCartForm.add(group);

Here is my test so far:

CartDTO cartDTO = new CartDTO();

IModel<CartDTO> cart = Model.of(cartDTO );
PopupPanel confirmPopup = new PopupPanel("1234");
FeedbackPanel feedbackPanel = new FeedbackPanel("12345678");
ShoppingCartFooterPanel p = new ShoppingCartFooterPanel("123", cart, confirmPopup, feedbackPanel);

testPage.setComponentToTest(p);
tester.startPage(testPage);
FormTester ft = tester.newFormTester("123:xmlCartFileExport", false);
ft.select("groupFileType", 3);

Object parstlinkSelected = p.get("xmlCartFileExport:groupFileType").getDefaultModelObject();
tester.clickLink("123:xmlCartFileExport:downloadCart");

What I do not get is

  1. How to make different selections in my test (and how to verify this)
  2. How to click the DownloadLink and verify the File which I get.

I would appreciate any help.

THX

kism3t
  • 1,343
  • 1
  • 14
  • 33

1 Answers1

1
  1. You need to FormTester.submit() after making the selection.
  2. Use clickLink(..., false) to be normal (i.e. non-Ajax) click. Then use tester.getLastResponse().get***() methods like (getBinaryContent() and getHeader(String)) to assert.
martin-g
  • 17,243
  • 2
  • 23
  • 35
  • Thanks martin this works. I think what irritates/ed me is that you have to submit the form first, which I do not have in front end, as there is no submit button. – kism3t Apr 21 '16 at 06:02
  • In that case there must be an Ajax behavior (AjaxFormChoiceComponentSubmittingBehavior) that submits the selection. – martin-g Apr 21 '16 at 06:12
  • So far it works if the selection is index 0 but if I change it to 1 or something else it does not work. I get from tester.getLastResponse another Page html. Furthermore, there is no behavior attached. The code above is everything. Previously only a RadioGroup and DownloadLink was there, but I put a Form around it, so FormTester can select Radio. – kism3t Apr 21 '16 at 07:02
  • But if there is just RadioGroup without an Ajax submitting behavior or a Form then how the value of the radio is send to the server ? – martin-g Apr 21 '16 at 07:20
  • Hey Martin, the RadioGroup has a shared Model which the DownloadLink also has access and therefore decides what format to download. I update the model in the question which is reused – kism3t Apr 21 '16 at 08:00
  • OK, but the RadioGroup model object could not be updated if there is no form submit or Ajax request. DownloadLink doesn't send the new radio value to the server. So I don't see how it works for you at runtime. – martin-g Apr 21 '16 at 12:01
  • The RadioGroup on our Page is updated when the user changes the selection. I checked it with onModelChanged() { System.out.println("+++" + getDefaultModelObjectAsString()); }. So the DownloadLink gets the Model and reads which was selected. – kism3t Apr 25 '16 at 09:27
  • Then you must be overriding `#wantOnSelectionChangedNotifications()` to return `true`. – martin-g Apr 25 '16 at 14:19
  • Hey Martin, I do not override the method. But I will try to generate a QuickStart Application. Would there be a way to share this with you? – kism3t Apr 26 '16 at 07:19
  • GitHub, BitBucket, any other similar service. Or even Dropbox. – martin-g Apr 26 '16 at 20:09
  • Ok, It was really my mistake. Our code got so big that I oversaw the AjaxFormChoiceComponentSubmittingBehavior. So your described solution works. Still wonder why the tester does not use the added Behaviour and does an auto commit as all are in the tested panel?! – kism3t Apr 28 '16 at 04:59
  • I'm glad you solved it! WicketTester doesn't do anything you didn't tell it to do. You have to execute the behavior explicitly: `tester.executeAjaxEvent(component, event)` or `tester.executeBehavior(AbstractAjaxBehavior)`. – martin-g Apr 28 '16 at 11:31