You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by Hans-Heinrich Braun <ha...@yahoo.de.INVALID> on 2017/03/13 07:07:21 UTC

Problem with testing AbstractAjaxBehavior

In my Application i have a DropDownSelect which changes a Label onUpdate by  AjaxFormComponentUpdatingBehaviorThen there is a Button which tries access at the selected item. In the real Webapp it works fine but when i test itwhen i click the Button the selected Item is null.What I am doing wrong. Here the HomePage:
public class HomePage extends WebPage { private static final long serialVersionUID = 1L; private static final List<String> SEARCH_ENGINES = Arrays.asList(new String[] { "Google", "Bing", "Baidu" }); //make Google selected by default private String selected = "Google";
    Model<String> model = new Model<String>() {                    public String getObject() {              return getSelected();          }      }; 
      private Label chosen=new Label("chosen", model);   public HomePage(final PageParameters parameters) { super(parameters);  FeedbackPanel feedback=new FeedbackPanel("feedback"); add(feedback);  feedback.setVisible(true); // other pages will set this to visible         feedback.setEscapeModelStrings(false); add(new Label("version", getApplication().getFrameworkSettings().getVersion()));  DropDownChoice<String> listSites = new DropDownChoice<String>( "sites", new PropertyModel<String>(this, "selected"), SEARCH_ENGINES);
 Form<?> form = new Form<Void>("form");  final   Button detailButton=new Button("detailButton") { @Override public void onSubmit() {info(getSelected()); } };  detailButton.setOutputMarkupId(true); chosen.setOutputMarkupId(true);   listSites.add(new AjaxFormComponentUpdatingBehavior("onchange")        {            @Override            protected void onUpdate(AjaxRequestTarget target)            {                              target.add(chosen);            target.add(detailButton);                      }        });    form.add(chosen);  add(form); form.add(listSites); form.add(detailButton);    } 
 public String getSelected() { return selected; }
 public void setSelected(String selected) { this.selected = selected; }}

and here the Test :
public class TestHomePage{ private WicketTester tester;
 @Before public void setUp() { tester = new WicketTester(new WicketApplication()); }
 @Test public void homepageRendersSuccessfully() { //start and render the test page tester.startPage(HomePage.class);
 //assert rendered page class tester.assertRenderedPage(HomePage.class); FormTester  formTester = tester.newFormTester("form"); formTester.select("sites", 1);  tester.executeBehavior((AbstractAjaxBehavior)tester.getComponentFromLastRenderedPage("form:sites").getBehaviors().get(0));  tester.assertLabel("form:chosen", "Bing");   formTester.submit("detailButton");  tester.assertInfoMessages("feedback","Bing"); }}
i am using wicket 6.19.0

Re: WG: Problem with testing AbstractAjaxBehavior

Posted by Sven Meier <sv...@meiers.net>.
Hi,

try with an additional formTester:

     tester.startPage(HomePage.class);
     FormTester firstRequest = tester.newFormTester("form");
     firstRequest.select("sites", 1);
     tester.executeBehavior((AbstractAjaxBehavior)tester.getComponentFromLastRenderedPage("form:sites").getBehaviors().get(0));
     tester.assertLabel("form:chosen","Bing");

     FormTester secondRequest = tester.newFormTester("form");
     secondRequest.submit("detailButton");
     tester.assertInfoMessages("feedback","Bing");

Note that *when FormTester is created*, it pushes all values of a form 
into the current request.

With your setup the value of the choice is missing in the second request.

Have fun
Sven



On 14.03.2017 10:16, Hans-Heinrich Braun wrote:
> In my Application i have a DropDownSelect which changes a Label onUpdate
>
>
>   by  AjaxFormComponentUpdatingBehaviorThen there is a Button which tries
> access at the selected item. In the real Webapp it works fine but when i
>   test itwhen i click the Button the selected Item is null.What I am doing
>   wrong.
> ----------------------------------Here the HomePage:
>
> public class HomePage extends WebPage {
> private static final longserialVersionUID = 1L;
> private static final List<String> SEARCH_ENGINES = Arrays.asList(new String[] { "Google", "Bing", "Baidu" }); //
> private String selected = "Google";
> Model<String> model = new Model<String>() {                    public String getObject() {              return getSelected();          }      };
>
> private Label chosen=new Label("chosen", model);
>   public HomePage(final PageParameters parameters) { super(parameters);
>
> FeedbackPanel feedback=new FeedbackPanel("feedback"); add(feedback);
>
> feedback.setVisible(true); // other pages will set this to visible
> feedback.setEscapeModelStrings(false); add(new
> Label("version",getApplication().getFrameworkSettings().getVersion()));
>
> DropDownChoice<String> listSites = new DropDownChoice<String>( "sites", new
>
> PropertyModel<String>(this, "selected"), SEARCH_ENGINES);
>
> Form<?> form = new Form<Void>("form");
> final  Button detailButton=new Button("detailButton") {
> @Override public void onSubmit(){info(getSelected()); } };
> detailButton.setOutputMarkupId(true);
>
> chosen.setOutputMarkupId(true);  listSites.add(new  AjaxFormComponentUpdatingBehavior("onchange")        {
> @Override            protected void onUpdate(AjaxRequestTarget target)
>         {                              target.add(chosen);
>   target.add(detailButton);                      }        });
>
> form.add(chosen);  add(form); form.add(listSites); form.add(detailButton);
>     }
>
>    public String getSelected() { return selected; }
> public void setSelected(String selected) { this.selected = selected; }}
>
> ----------------------------------------and here the Test :
>
> public class TestHomePage{ private WicketTester tester;
>
> @Before public void setUp() {
> tester = new WicketTester(new WicketApplication()); }
>
> @Test public void homepageRendersSuccessfully() {
> tester.startPage(HomePage.class);
>   FormTester  formTester = tester.newFormTester("form");
>   formTester.select("sites", 1);  tester.executeBehavior((
> AbstractAjaxBehavior)tester.getComponentFromLastRenderedPage("form:sites").getBehaviors().get(0));
> tester.assertLabel("form:chosen","Bing");
>   formTester.submit("detailButton");
> tester.assertInfoMessages("feedback","Bing");
> }}
>   I am using wicket 6.26.0
>
>
>     


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


WG: Problem with testing AbstractAjaxBehavior

Posted by Hans-Heinrich Braun <ha...@yahoo.de.INVALID>.
In my Application i have a DropDownSelect which changes a Label onUpdate


 by  AjaxFormComponentUpdatingBehaviorThen there is a Button which tries
access at the selected item. In the real Webapp it works fine but when i
 test itwhen i click the Button the selected Item is null.What I am doing
 wrong. 
----------------------------------Here the HomePage:

public class HomePage extends WebPage { 
private static final longserialVersionUID = 1L; 
private static final List<String> SEARCH_ENGINES = Arrays.asList(new String[] { "Google", "Bing", "Baidu" }); //
private String selected = "Google";
Model<String> model = new Model<String>() {                    public String getObject() {              return getSelected();          }      };

private Label chosen=new Label("chosen", model);  
 public HomePage(final PageParameters parameters) { super(parameters);

FeedbackPanel feedback=new FeedbackPanel("feedback"); add(feedback);

feedback.setVisible(true); // other pages will set this to visible
feedback.setEscapeModelStrings(false); add(new 
Label("version",getApplication().getFrameworkSettings().getVersion()));

DropDownChoice<String> listSites = new DropDownChoice<String>( "sites", new

PropertyModel<String>(this, "selected"), SEARCH_ENGINES);

Form<?> form = new Form<Void>("form");  
final  Button detailButton=new Button("detailButton") { 
@Override public void onSubmit(){info(getSelected()); } };  
detailButton.setOutputMarkupId(true);

chosen.setOutputMarkupId(true);  listSites.add(new  AjaxFormComponentUpdatingBehavior("onchange")        {
@Override            protected void onUpdate(AjaxRequestTarget target)
       {                              target.add(chosen);
 target.add(detailButton);                      }        });

form.add(chosen);  add(form); form.add(listSites); form.add(detailButton);
   }

  public String getSelected() { return selected; }
public void setSelected(String selected) { this.selected = selected; }}

----------------------------------------and here the Test :

public class TestHomePage{ private WicketTester tester;

@Before public void setUp() { 
tester = new WicketTester(new WicketApplication()); }

@Test public void homepageRendersSuccessfully() { 
tester.startPage(HomePage.class);
 FormTester  formTester = tester.newFormTester("form");
 formTester.select("sites", 1);  tester.executeBehavior((
AbstractAjaxBehavior)tester.getComponentFromLastRenderedPage("form:sites").getBehaviors().get(0));  
tester.assertLabel("form:chosen","Bing");  
 formTester.submit("detailButton");  
tester.assertInfoMessages("feedback","Bing"); 
}}
 I am using wicket 6.26.0


   

Re: Problem with testing AbstractAjaxBehavior

Posted by Martin Grigorov <mg...@apache.org>.
Hi,

Your code is not formatted and it is quite hard to read and follow it.
Could you please re-send it ?

Also is it possible to try with 6.26.0 ?

Martin Grigorov
Wicket Training and Consulting
https://twitter.com/mtgrigorov

On Mon, Mar 13, 2017 at 8:07 AM, Hans-Heinrich Braun <
hansheinrichbraun@yahoo.de.invalid> wrote:

> In my Application i have a DropDownSelect which changes a Label onUpdate
> by  AjaxFormComponentUpdatingBehaviorThen there is a Button which tries
> access at the selected item. In the real Webapp it works fine but when i
> test itwhen i click the Button the selected Item is null.What I am doing
> wrong. Here the HomePage:
> public class HomePage extends WebPage { private static final long
> serialVersionUID = 1L; private static final List<String> SEARCH_ENGINES =
> Arrays.asList(new String[] { "Google", "Bing", "Baidu" }); //make Google
> selected by default private String selected = "Google";
>     Model<String> model = new Model<String>() {                    public
> String getObject() {              return getSelected();          }      };
>       private Label chosen=new Label("chosen", model);   public
> HomePage(final PageParameters parameters) { super(parameters);
> FeedbackPanel feedback=new FeedbackPanel("feedback"); add(feedback);
> feedback.setVisible(true); // other pages will set this to visible
>  feedback.setEscapeModelStrings(false); add(new Label("version",
> getApplication().getFrameworkSettings().getVersion()));
> DropDownChoice<String> listSites = new DropDownChoice<String>( "sites", new
> PropertyModel<String>(this, "selected"), SEARCH_ENGINES);
>  Form<?> form = new Form<Void>("form");  final   Button detailButton=new
> Button("detailButton") { @Override public void onSubmit()
> {info(getSelected()); } };  detailButton.setOutputMarkupId(true);
> chosen.setOutputMarkupId(true);   listSites.add(new
> AjaxFormComponentUpdatingBehavior("onchange")        {
> @Override            protected void onUpdate(AjaxRequestTarget target)
>       {                              target.add(chosen);
> target.add(detailButton);                      }        });
> form.add(chosen);  add(form); form.add(listSites); form.add(detailButton);
>   }
>  public String getSelected() { return selected; }
>  public void setSelected(String selected) { this.selected = selected; }}
>
> and here the Test :
> public class TestHomePage{ private WicketTester tester;
>  @Before public void setUp() { tester = new WicketTester(new
> WicketApplication()); }
>  @Test public void homepageRendersSuccessfully() { //start and render the
> test page tester.startPage(HomePage.class);
>  //assert rendered page class tester.assertRenderedPage(HomePage.class);
> FormTester  formTester = tester.newFormTester("form");
> formTester.select("sites", 1);  tester.executeBehavior((
> AbstractAjaxBehavior)tester.getComponentFromLastRenderedPa
> ge("form:sites").getBehaviors().get(0));  tester.assertLabel("form:chosen",
> "Bing");   formTester.submit("detailButton");  tester.assertInfoMessages("feedback","Bing");
> }}
> i am using wicket 6.19.0