Re: How to validate radio buttons using HttpUnit

Wolfgang Fahl - BITPlan <[email protected]> Thu, 06 Nov 2008 12:38:36 +1300
Newsgroups gmane.comp.web.httpunit.devel
Message-ID <[email protected]>
Sandy,

> For some reason, form.getOptions(radioGroupName) returns empty string. How do I decide which radio option is selected?

I had the problem the otherway round in 2004 and that's why the
RadioButtonFormControl is now accessible. Please find my example code for
setting a radiobutton below. Along the same lines getting the control's
value should be possible.

Also the testcases in

HTMLFormTest might give you some hints

/**

*

set

a

value

of

a

form

radio

button

and

or

checkbox

attribute

*

@param

var

-

the

variable

name

*

@param

value

-

the

value

to

set

*

@throws

Throwable

if

setformvalue

fails

*/

public

void

setradioformvalue(String var, String value)

throws

Throwable {

try

{

tester().assertFormPresent(getForm());

//com.meterware.httpunit.WebForm form=tester().getDialog().getResponse().getFormWithName(FORM);

//form.setParameter( var+value, value );

Object buttonlist[]=tester().getDialog().getResponse().getElementsWithName(var);

if

(buttonlist.

length

==0)

throw

new

Exception(

"radio button with name "

+var+

" not found!"

);

boolean

found=

false

;

for

(

int

i=0;i<buttonlist.

length

;i++) {

Object o=buttonlist[i];

if

(o

instanceof

RadioButtonFormControl) {

RadioButtonFormControl button=(RadioButtonFormControl)o;

if

(button.getQueryValue().equals(value)) {

System.

out

.println(

"checking button "

+button.getID());

button.setChecked(

true

);

found=

true

;

}

}

else

if

(o

instanceof

FormControl) {

FormControl button=(FormControl)o;

button.setState(value.toLowerCase().equals(

"true"

));

found=

true

;

}

else

{

throw

new

Exception(

"element with id "

+var+

"has invalid type "

+o.getClass().getName()+

" expected was FormControl"

);

}

}

// for

if

(!found) {

throw

new

Exception(

"radio button with name "

+var+

" and value "

+value+

" not found!"

);

}

}

catch

(Throwable t) {

watcher().snapShotNow();

throw

(t);

}

}

// setradioformvalue

yours

Wolfgang

Date sent: Fri, 31 Oct 2008 16:12:27 -0700 (PDT)

From: Sandhya Kollath <[email protected]>

To: [email protected]

Subject: [Httpunit-develop] How to validate radio buttons using HttpUnit

Send reply to: [email protected],

Discussion of use and development of HttpUnit

<[email protected]>

<httpunit-develop.lists.sourceforge.net>

<mailto:[email protected]?subject=unsubscribe>

<mailto:[email protected]?subject=subscribe>

> Hi,

> I am trying to write a test case to simulate a radio option using HttpUnit.

>

> My form looks like this:

>

> <form method="post">

> <table>

> <tr>

> <td>

> <input type="radio" name="phoneList" value="false"/>Existing Mobile Number

> Select a number

> <input type="hidden" name="wlw-select_key:{actionForm.selectedNumber}OldValue" value="true" />

> <select name="wlw-select_key:{actionForm.selectedNumber}">

> <option value="1231231234">1231231234</option>

> <option value="2342342345">2342342345</option></select>

> </td>

>

> <td>

> <input type="radio" name="phoneList" value="true" checked="checked"/>New Mobile Number

> Number

> <input type="text" name="{actionForm.areaCode}" maxlength="3" size="3" title="Area code" onkeyup="return autoTab(this, 3, event);" />

> <input type="text" name="{actionForm.firstThree}" maxlength="3" size="3" title="First 3" onkeyup="return autoTab(this, 3, event);" />

> <input type="text" name="{actionForm.lastFour}" maxlength="4" size="4" title="Last 4" onkeyup="return autoTab(this, 4, event);" />

> </td>

> </tr>

> </table>

> ...

>

> </form>

>

> and my test case looks like this;

>

> WebForm[] forms = response.getForms();

> WebForm form = forms[0];

>

> String radioGroupName = "phoneList";

>

> String[] options = form.getOptions(radioGroupName); //this returns the labels of the radio button

> String[] optionsValues = form.getOptionValues(radioGroupName); //this returns true and false as per HttpUnit API

>

> for (int i = 0; i < options.length; i++) {

> if (options[i].equals("Existing Mobile Number")) {

> System.out.println("Existing");

> //do something

> } else {

> System.out.println("New");

> //do something

> }

> }

>

> For some reason, form.getOptions(radioGroupName) returns empty string. How do I decide which radio option is selected?

>

> Any advise is highly appreciated.

>

> Thanks.

> Sandy

>

>

>

>

-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/

_______________________________________________
Httpunit-develop mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/httpunit-develop