I'm probably missing something obvious, but I'm having problems tracking down how to set the contents of an <input type="file"...> element. I'm currently using:
setFormElement("historyFile", new File("data/historicalFile").getCanonicalPath());
But this gives the following error:
com.meterware.httpunit.FormParameter$UnusedParameterValueException: Attempted to assign to parameter 'historyFile' the extraneous value 'C:\...\data\historicalFile'.
at com.meterware.httpunit.FormParameter.setValues(FormParameter.java:94)
at com.meterware.httpunit.WebForm.setParameter(WebForm.java:505)
at com.meterware.httpunit.WebForm.setParameter(WebForm.java:498)
at net.sourceforge.jwebunit.HttpUnitDialog.setFormParameter(Unknown Source)
at net.sourceforge.jwebunit.WebTester.setFormElement(Unknown Source)
at net.sourceforge.jwebunit.WebTestCase.setFormElement(Unknown Source)
at com.fulcrum.fulcrumapp.web.HistoricalDataTest.testUpload(HistoricalDataTest.java:52)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at junit.framework.TestCase.runTest(TestCase.java:154)
at junit.framework.TestCase.runBare(TestCase.java:127)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:392)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:276)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:167)
Any suggestions?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I suppose the form element is a <input type="file" ... >. Shouldn't you set a file rather than a file name instead?
In HttpUnit I found WebForm.setParameter(String name, UploadFileSpec[] files).
I can not find any references to that method in the HttpUnit code though. There might not be any support for this yet. In that case try to code it yourself.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I'm probably missing something obvious, but I'm having problems tracking down how to set the contents of an <input type="file"...> element. I'm currently using:
setFormElement("historyFile", new File("data/historicalFile").getCanonicalPath());
But this gives the following error:
com.meterware.httpunit.FormParameter$UnusedParameterValueException: Attempted to assign to parameter 'historyFile' the extraneous value 'C:\...\data\historicalFile'.
at com.meterware.httpunit.FormParameter.setValues(FormParameter.java:94)
at com.meterware.httpunit.WebForm.setParameter(WebForm.java:505)
at com.meterware.httpunit.WebForm.setParameter(WebForm.java:498)
at net.sourceforge.jwebunit.HttpUnitDialog.setFormParameter(Unknown Source)
at net.sourceforge.jwebunit.WebTester.setFormElement(Unknown Source)
at net.sourceforge.jwebunit.WebTestCase.setFormElement(Unknown Source)
at com.fulcrum.fulcrumapp.web.HistoricalDataTest.testUpload(HistoricalDataTest.java:52)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at junit.framework.TestCase.runTest(TestCase.java:154)
at junit.framework.TestCase.runBare(TestCase.java:127)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:392)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:276)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:167)
Any suggestions?
Any suggestions other than, for example, "Use the right forum, monkey-boy!" I can make that one myself.
<sigh> All these Sourceforge forums look alike to me on a Monday morning. :(
I suppose the form element is a <input type="file" ... >. Shouldn't you set a file rather than a file name instead?
In HttpUnit I found WebForm.setParameter(String name, UploadFileSpec[] files).
I can not find any references to that method in the HttpUnit code though. There might not be any support for this yet. In that case try to code it yourself.
This works for me:
upload = new WebConversation();
try {
serverResponse = upload.getResponse(applicationURL);
WebForm uploadForm = serverResponse.getForms()[0];
WebRequest request = uploadForm.getRequest();
File file = new File(fileName);
UploadFileSpec[] filesArray = new UploadFileSpec[1];
filesArray[0] = new UploadFileSpec( file );
request.setParameter(fileFieldName, filesArray);
activeResponse = upload.getResponse(request);
} catch (SAXException e) {
System.err.println( "SAXException:" + e.getMessage() );
} catch ( MalformedURLException e){
System.err.println("MalformedURLException: " + e.getMessage());
} catch (IOException e){
System.err.println("IOException: " + e.getMessage());
e.printStackTrace();
}