定義:處理無狀態或者上下文無關的表單提交。它用來生成表單的GET或POST請求,這些請求與Form自動生成的請求是一樣的,發送這些請求時不需要表單上下文。
函數形式:web_submit_data( const char *StepName, const char *Action, , ITEMDATA, , [ EXTRARES, ,] LAST );
說明:
-
當VuGen設為基於URL的錄制模式,或者基於HTML的錄制方式時,但 Recording Options—HTML Advanced下的A script containing explicit URLs only選項被選中時,web_submmit_data函數才會被錄制。
-
不論采用URL查詢的方式(GET),還是采用請求體發送(POST)的方式,此函數都指示出Form中的數據是如何發送到服務器的。
-
如果VuGen處於HTTP錄制模式下,此時記錄Web進程時,會產生此函數。在提交Form時,如果無法生成web_submit_form函數,VuGen也會生成web_submit_data函數。
-
在錄制選項中,Toos—Recording Option下—Recording選項中,有一個Advanced HTML選項,可以設置是否錄制非HTML資源。只有選擇了“Record within the current script step”時,List of Resource Attributes才會被錄制到。非HTML資源(如:gif和jpg等圖象文件)。
-
任何對於“EncType”的指定都會覆蓋web_add_[auto_]header函數指定的Content-Type。當省略了“EncType”時,任何一個web_add_[auto_]header函數都會起作用。如果既沒有指定EncType也沒有web_add_[auto_]header函數,且“Method=POST”,“application/x-www-form-urlencoded”會作為默認值來使用。其他情況下,不會產生Content-Type請求頭。
-
ContentType:文件類型標識符,如果“EncType”是“multipart/form-data”用來上傳文件時,需要用到“ContentType”。當在ITEMDATA中的Data子句中指定了“File=Yes”,且文件也在此子句中,ContentType才適用,此時它會作為同一個子句的值來傳遞。正常情況下,“Content-Type”根據所上傳文件的擴展名自動生成。如:
7d025e2b16b064e\r\n Content–Disposition: form–data; name="uploaded_file"; filename="D:\\temp\\a.txt"\r\n Content–Type: text/plain\r\n \r\n
-
“ContentType”如果指定了空值,那么“Content-Type”頭將不包含在文件中。如果沒有顯示的指定“ContentType”的值,當上傳的文件為空時,不管文件擴展名是什么,都默認使用“application/x-unknown-content-type”做為”ContentType”的值。VuGen不會檢查指定的ContentType是否有效。
-
通常情況下,如果錄制了web_submit_data函數,VuGen會把“name”和“value”一起錄制到ITEMDATA一節中。如果不想在腳本中以明文顯示“value”,可以對它進行加密。把“Value”改為“EncryptedValue”,然后把錄制到的值改為加密后的值。請參考web_submit_form中相關的內容。
示例1:一般的表單提交
web_submit_data("default.aspx", "Action=http://lazarus/flightnet/default.aspx", "Method=POST", "TargetFrame=", "RecContentType=text/html", "Referer=http://lazarus/flightnet/", "Snapshot=t7.inf", "Mode=HTML", ITEMDATA, "Name=grpType", "Value=radRoundtrip", ENDITEM, "Name=lstDepartingCity", "Value=DEN", ENDITEM, "Name=lstDestinationCity", "Value=LAX", ENDITEM, "Name=txtDepartureDate", "Value=8/19/2003", ENDITEM, "Name=txtReturnDate", "Value=8/19/2003", ENDITEM, "Name=txtQuantity", "Value=1", ENDITEM, "Name=radClass", "Value=1", ENDITEM, "Name=radSeat", "Value=1", ENDITEM, "Name=btnAvailableFlights", "Value=Next >", ENDITEM, LAST );
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
示例2:web_submit_data使用POST方法提交兩個文件
web_submit_data("Attachments", "Action=http://barton.cottage@.Devonshire.uk/Attachments?YY=45434", "Method=POST", "EncType=multipart/form–data", "TargetFrame=", "RecContentType=text/html", "Referer=http:///barton.cottage@.Devonshire.uk/Compose?YY=20435", "Snapshot=t5.inf", "Mode=HTML", ITEMDATA, "Name=userFile0", "Value=E:\\sense_sensibility\\Elinor.htm", "File=yes", "ContentType=text/html", // Override default "text/plain" for .txt files FilePath=Elinor.txt", "ContentTransferEncoding=html/text", ENDITEM, "Name=userFile1", "Value=E:\\sense_sensibility\\Marianne.jpg", "File=yes", ENDITEM, LAST ); 注:表單中enctype="multipart/form-data"的意思是設置表單的MIME編碼。默認情況,這個編碼格式是application/x-www-form-urlencoded,不能用於文件上傳;只有使用了multipart/form-data,才能完整的傳遞文件數據,進行下面的操作。 enctype="multipart/form-data"是上傳二進制數據; form里面的input的值以2進制的方式傳過去。
java scripts:
web.submit_data("saveFile2",
"Action=<m_host>/myLoan-mobile/action/api/saveFile",
new String[]{ //options[]
"Method=POST",
"EncType=multipart/form-data",
"RecContentType=application/josn",
"Mode=HTTP"
},
new String[]{ //data[]
"Name=token", "Value=<token>", web.ENDITEM,
"Name=pictureFile", "Value=E:\\LRWorkspace\\loginandsavephototest\\imageidcardfrontpath.jpeg",
"File=YES",
"ContentType=image/jpeg", //不加這一行,LR默認設置為ContentType=image/pjpeg格式,回放失敗。
web.ENDITEM,
web.LAST
});
傳圖片時,ie會把 jpg、jpeg翻譯成image/pjpeg,png翻譯成image/x-png 。而火狐則很標准:jpg、jpeg翻譯成image/jpeg,png翻譯成image/png。
為了避免這個問題,我在多個瀏覽器上做了測試,結果如下:
firefox image/jpeg image/bmp image/gif image/png
ie 6 image/pjpeg image/bmp image/gif image/x-png
ie 7 image/pjpeg image/bmp image/gif image/x-png
ie 8 image/pjpeg image/bmp image/gif image/x-png