[Webtest] multipart form

Jeff Nielsen webtest@lists.canoo.com
Tue, 15 Apr 2003 08:09:35 -0400


This is a multi-part message in MIME format.

------=_NextPart_000_026B_01C30326.5A91A1C0
Content-Type: text/plain;
	charset="Windows-1252"
Content-Transfer-Encoding: 7bit

I had to write a couple of my own WebTest tags to be able to test multi-part
forms and other kinds of situations.  I'm attaching the code in case anyone
is interested.

Using the attached classes, I can write tests like this and have them work.

<invoke . . ./>
<setjsformfield form="myMultipartForm" name="submit-name" value="The File
Name"/>
<setjsfilefield form="myMultipartForm" name="files" value="fileName"/>
<clickbutton . . ./>

Jeff Nielsen
Digital Focus
www.digitalfocus.com


----- Original Message -----
From: "paul ng" <ng_paul@hotmail.com>
To: <webtest@lists.canoo.com>
Sent: Monday, April 14, 2003 8:59 PM
Subject: [Webtest] multipart form


> Hi all,
>
> Have anyone been successfully in processing a multipart form using
WebTest?
> I've encountered error whenever i have enctype="mulipart/form-data" in my
> HTML FORM tag.  Here is an example.  The FORM works only when I remove the
> enctype and INPUT tag with type file.
>
>
> <FORM action="http://server.com/cgi/handle"
>        enctype="multipart/form-data"
>        method="post">
>    <P>
>    What is your name? <INPUT type="text" name="submit-name"><BR>
>    What files are you sending? <INPUT type="file" name="files"><BR>
>    <INPUT type="submit" value="Send"> <INPUT type="reset">
> </FORM>
>
> Regards,
> Paul Ng
>
>
> _________________________________________________________________
> Take a break! Find destinations on MSN Travel.
http://www.msn.com.sg/travel/
>
> _______________________________________________
> WebTest mailing list
> WebTest@lists.canoo.com
> http://lists.canoo.com/mailman/listinfo/webtest

------=_NextPart_000_026B_01C30326.5A91A1C0
Content-Type: application/octet-stream;
	name="SetJsFormField.java"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: attachment;
	filename="SetJsFormField.java"

package com.canoo.webtest.steps;

import java.util.HashMap;

import org.xml.sax.SAXException;

import com.canoo.webtest.engine.Context;
import com.canoo.webtest.engine.StepExecutionException;
import com.canoo.webtest.engine.StepFailedException;
import com.meterware.httpunit.WebForm;

/**
 * @author  Jeff Nielsen
 */
public class SetJsFormField extends Step {
   =20
    private String form;
    private String name;
    private String value;

    public SetJsFormField() {
        super();
    }

    public SetJsFormField(String form, String name, String value) {
        this.form =3D form;
        this.name =3D name;
        this.value =3D value;
    }

    public void doExecute(Context context) {
        verifyParameters();
        try {
            WebForm theForm =3D =
context.getLastResponse().getFormWithName(form);
            theForm.setParameter(name, value);
        } catch (SAXException e) {
            throw new StepFailedException("Cannot find form with name " =
+ form,=20
                                          this);
        }
    }

    public String getForm() {
        return form;
    }

    public String getName() {
        return name;
    }

    public String getValue() {
        return value;
    }

    public void setForm(String form) {
        this.form =3D form;
    }

    public void setName(String name) {
        this.name =3D name;
    }

    public void setValue(String value) {
        this.value =3D value;
    }

    public HashMap getParameterDictionary() {
        HashMap map =3D super.getParameterDictionary();
        map.put("form", form);
        map.put("name", name);
        map.put("value", value);
        return map;
    }

    public void expandProperties() {
        super.expandProperties();
        form =3D expandDynamicProperties(form);
        name =3D expandDynamicProperties(name);
        value =3D expandDynamicProperties(value);
    }

    protected void verifyParameters() {
        if (form =3D=3D null) {
            throw new StepExecutionException("Required parameter =
\"form\" "=20
                                             + "not set!");
        }
        if (name =3D=3D null) {
            throw new StepExecutionException("Required parameter =
\"name\" "=0D                                              + "not =
set!");
        }
        if (value =3D=3D null) {
            throw new StepExecutionException("Required parameter =
\"value\" "=0D                                             + "not =
set!");
        }
    }



}

------=_NextPart_000_026B_01C30326.5A91A1C0
Content-Type: application/octet-stream;
	name="SetJsFormFieldTest.java"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: attachment;
	filename="SetJsFormFieldTest.java"

package com.canoo.webtest.steps;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;

import com.canoo.webtest.engine.Context;
import com.canoo.webtest.engine.StepExecutionException;
import com.canoo.webtest.engine.WebTest;
import com.canoo.webtest.self.ContextStub;
import com.meterware.httpunit.IllegalRequestParameterException;
import com.meterware.httpunit.WebResponse;


public class SetJsFormFieldTest extends StepTest {

    private SetJsFormField step;

    public SetJsFormFieldTest(String name) {
        super(name);
    }
   =20
    public void setUp() {
        step =3D new SetJsFormField();
        step.setStepId("myStep");
        step.setStepType("setjsformfield");
        step.setForm("loginForm");
        step.setName("userName");
        step.setValue("mhlink");
    }

    public void testConstruction() {
        assertEquals("loginForm", step.getForm());
        assertEquals("userName", step.getName());
        assertEquals("mhlink", step.getValue());
    }

    public void testGetParameterDictionary() {
        HashMap expected =3D new HashMap();
        expected.put("stepType", "setjsformfield");
        expected.put("stepId", "myStep");
        expected.put("form", "loginForm");
        expected.put("name", "userName");
        expected.put("value", "mhlink");
        assertEquals(expected, step.getParameterDictionary());       =20
    }
   =20
    public void testVerifyingParameter() throws Exception {
        step =3D new SetJsFormField();

        try {
            step.doExecute(new ContextStub());
            fail("should have raised StepExecutionException since no "=20
                 + "parameters are set");
        } catch (StepExecutionException expected) {}

        step.setForm("XXXXX");
        try {
            step.doExecute(new ContextStub());
            fail("should have raised StepExecutionException since only =
form"=20
                 + " is set");
        } catch (StepExecutionException ex) {}

        step.setName("XXXXX");
        try {
            step.doExecute(new ContextStub());
            fail("should have raised StepExecutionException since value" =

                 + " is not set");
        } catch (StepExecutionException ex) {}
       =20
        step =3D new SetJsFormField();
        step.setValue("XXXXX");
        try {
            step.doExecute(new ContextStub());
            fail("should have raised StepExecutionException since only =
value "
                 + "is set");
        } catch (StepExecutionException ex) {}

    }

    public void testExecute() throws Exception {
        Context ctx =3D createContext();
        step.doExecute(ctx);
        String savedValue =3D =
ctx.getLastResponse().getFormWithName("loginForm")
                                             =
.getParameterValue("userName");
        assertEquals("mhlink", savedValue);
    }

    public void testExecuteWithBadFormName() throws Exception {
        Context ctx =3D createContext();
        step.setName("formThatDoesNotExist");
        try {
            step.doExecute(ctx);
            fail("Should not work with bad form");
        } catch (IllegalRequestParameterException correct) {}
    }
   =20
    private Context createContext() throws IOException, =
MalformedURLException {
        final WebResponse sampleResponse =3D=20
            WebResponse.newResponse(createUrlConnection());
       =20
        Context ctx =3D new Context(new WebTest()) {
            public WebResponse getLastResponse() {
                return sampleResponse;
            }
        };
        return ctx;
    }

    private URLConnection createUrlConnection() throws IOException {
        String htmlString =3D ""
            + "<html><head><title>Fake Login Page</title></head>"
            + "<body><form name=3DloginForm>"
            + "<input type=3Dtext name=3DuserName>"
            + "<input type=3Dpassword name=3Dpassword>"
            + "</form></body></html>";
        File htmlFile =3D File.createTempFile("test", ".html");
        FileWriter writer =3D new FileWriter(htmlFile);
        writer.write(htmlString);
        writer.close();
       =20
        URL url =3D new URL("file://localhost/" + htmlFile.getPath());
        URLConnection conn =3D url.openConnection();
        return conn;
    }
   =20

    /* Overridden from StepTest =
--------------------------------------------- */

    protected String[] getAttributes() {
        return new String[]{ "Form", "Name", "Value" };
    }

    protected Step getStep() {
        return new SetJsFormField();
    }

}

------=_NextPart_000_026B_01C30326.5A91A1C0
Content-Type: application/octet-stream;
	name="SetJsFileField.java"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
	filename="SetJsFileField.java"

package com.canoo.webtest.steps;

import java.io.File;

import org.xml.sax.SAXException;

import com.canoo.webtest.engine.Context;
import com.canoo.webtest.engine.StepFailedException;
import com.meterware.httpunit.UploadFileSpec;
import com.meterware.httpunit.WebForm;

/**
 * @author Jeff Nielsen
 */
public class SetJsFileField extends SetJsFormField {

    public SetJsFileField() {
        super();
    }

    public SetJsFileField(String form, String name, String value) {
        super(form, name, value);
    }

    public void doExecute(Context context) {
        verifyParameters();
        try {
            WebForm theForm = 
                context.getLastResponse().getFormWithName(getForm());
            UploadFileSpec file = new UploadFileSpec(new File(getValue()));
            theForm.setParameter(getName(), new UploadFileSpec[] { file });
        } catch (SAXException e) {
            throw new StepFailedException("Cannot find form with name " 
                                          + getForm(), this);
        }
    }

}

------=_NextPart_000_026B_01C30326.5A91A1C0--