You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by Alexander Elsholz <al...@widas.de> on 2009/03/30 19:05:05 UTC

TagTester and AJAX

Hi,

i try to test my manipulated html tags with tagtester. it works fine, if i
submit the form via formtester. but when i try to set value to request 
directlyand submit via ajax-button the tagtester failed - the attribute wasn't
updated.

her the code: - a simple ajax submit example where text
in real live i doesn't submit the value via button. here its a direct 
key-event. that's the reason, why i can't use the formtester in 
this case. the rest works fine.


thanks alex

java

final TextField tf1 = new TextField("tf1", new PropertyModel(this, "value")){
	@Override
	protected void onComponentTag(ComponentTag pTag) {
		super.onComponentTag(pTag);
		if (isRequired()) {
			if (getInput() == null || 
				getInput().equalsIgnoreCase("")) {
				pTag.put("class", "abc");
			}
		}
	}
        	
        };
        tf1.setRequired(true);
        final Label lbl1 = new Label("lbl1", new PropertyModel(this, "value"));
		Form lForm = new Form("form");
		final FeedbackPanel lSetOutputMarkupId = (FeedbackPanel) new
			FeedbackPanel("pnl").setOutputMarkupId(true);
		AjaxButton lChild = new AjaxButton("btn", lForm	){
			protected void onSubmit(AjaxRequestTarget pTarget, 
				Form pForm) {
				pTarget.addComponent(tf1);
				pTarget.addComponent(lbl1);
				pTarget.addComponent(lSetOutputMarkupId);
				info("doit");
			}
		};
		
		lForm.add(tf1.setOutputMarkupId(true));
		lForm.add(lbl1.setOutputMarkupId(true));

		lForm.add(lChild);
		add(lForm);
		add(lSetOutputMarkupId);
====================

html
<html>
    <head><meta name="MSSmartTagsPreventParsing" content="TRUE">
        <title>Wicket Quickstart Archetype Homepage</title>
    </head>
    <body>
        <form wicket:id="form">
        	<input type="text" wicket:id="tf1"></input><br></br><span
wicket:id="lbl1"></span>
       		<input type="submit" wicket:id="btn" value="click" ></input>
        </form>
        
        <span wicket:id="pnl"></span>
    </body>
</html>

=================
test 

private WicketTester wiTe;

	@Test
	public void testPflichtfeld() {
		
		wiTe = new WicketTester();
		wiTe.startPage(HomePage.class);
		wiTe.assertRenderedPage(HomePage.class);
		wiTe.assertNoErrorMessage();
		checkAttribute("tf1", "class", "abc");

		setValueToTextField("tf1", "");
		assertErrorMessages(1);
		checkAttribute("tf1", "class", "abc");
		
		setValueToTextField("tf1", "123");
		assertErrorMessages(1);
		checkAttribute("tf1", "class", null); //here test fails
		
		setValueToTextField("tf1", "");
		assertErrorMessages(1);
		checkAttribute("tf1", "class", "abc");
	}

	private void assertErrorMessages(int pNumberOfFailures) {
		int lActual = wiTe.getMessages(FeedbackMessage.ERROR).size();
		Assert.assertEquals(pNumberOfFailures, lActual);
	}

	private void checkAttribute(String pWicketId, String pAttr, 
		String pValue) {
		TagTester lTagByWicketId = wiTe.getTagByWicketId("tf1");
		String lClassAttribute = lTagByWicketId.getAttribute(pAttr);
		Assert.assertEquals(pValue, lClassAttribute);
	}
	
	protected final void setValueToTextField(String pWicketID, 
		String pValue) {
//		FormTester lNewFormTester = wiTe.newFormTester("form");
		
		FormComponent lComp = (FormComponent) wiTe
			.getComponentFromLastRenderedPage( "form:"+pWicketID);
	
		String lInput = lComp.getInputName();
		wiTe.getServletRequest().setParameter(lInput,pValue);


//		lNewFormTester.setValue(pWicketID, pValue);
//		lNewFormTester.submit();
		wiTe.executeAjaxEvent("form:btn", "onclick");
	}


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