2013-06-21 107 views
1

我使用JMeter測試Web服務(SOAP),我正在測試的方法需要文件類型的參數。在SOAP UI中,可以通過上傳附件併爲其提供ID來完成。帶附件的JMeter SOAP請求

在SOAP請求,那麼像這樣將放在:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"  xmlns:ws="http://zk.payment.dkv.be/ws"> 
    <soapenv:Header/> 
    <soapenv:Body> 
     <ws:wsMethod> 
      <file>cid:attachementID</file> 
     </ws:wsMethod> 
    </soapenv:Body> 
</soapenv:Envelope> 

我使用的JMeter 2.9,似乎這是不可能的,任何一個有解決方法嗎?

乾杯!

回答

1

所以在寫這篇文章的時候,JMeter並沒有解決上述問題的方法。不要直接發送附件,但有一個解決方法!

因爲在最後它是所有關於標籤之間什麼:

<file>"what is here??"</file> 

這是怎麼一回事上述標籤之間,這就是其重要的爲您服務的唯一的事情。那麼解決方法:

<file><![CDATA[${yourContentJMeterVariableName}]]></file> 

「yourContentJMeterVariableName」基本上是的JMeter變量,它必須通過填寫比方說,一個BeanShell的預處理器(忽略所有的進口),像這樣:

try { 
    String path = "pathToYourContentFileOrAttachement"; 
    String content = new Scanner(new File(path)).useDelimiter("\\Z").next(); 
    byte[] encoded = Base64.encodeBase64(content.getBytes()); 
    vars.put("yourContentJMeterVariableName", new String(encoded)); 
} catch (FileNotFoundException e) { 
    log.error(e); 
} 

上面我爲了這篇文章而忽略了進口,但不太明顯的將是:

import java.util.Scanner; 
import org.apache.commons.codec.binary.Base64; 

它們應該已經包含在jmeter的lib中。就是這樣了!

+0

哪裏進口走? – ed4becky

1

我已經使用了Fico提到的類似解決方案,請嘗試下面的代碼。

File file = new File("C:\\103861\\A1940599.zip"); 
InputStream is = new FileInputStream(file); 
long length = file.length(); 
if (length > Integer.MAX_VALUE) { 
    // File is too large 
} 
byte[] bytes = new byte[(int)length]; 
int offset = 0; 
int numRead = 0; 
while (offset < bytes.length && (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) { 
    offset += numRead; 
} 
if (offset < bytes.length) { 
    throw new IOException("Could not completely read file "+file.getName()); 
} 
is.close(); 
String Attachment=Base64.encodeBase64String(bytes); 
vars.put("SoapAttachment",Attachment); 

在$ {SoapAttachment}的XML中使用SoapAttachment。

請注意 - 您可能需要刪除少量導入項,因爲我正在進行校驗和計算,所以我需要它們。

1

我會建議創建自定義肥皂取樣器,啓用附件處理。 解決方案(來源,目標文件夾中建JAR)是在github上: https://github.com/spinning10/JMeterSoapSampler

還請結算我們的測試博客: http://driven-by-tests.blogspot.be/

內置罐子(最新,爪哇1.7下編譯)正在:https://github.com/spinning10/JMeterSoapSampler/blob/master/target/CustomSoapSampler-1.3.jar

您也可以找到這個採樣器可用,如果你從網站上安裝「插件管理器」下載:http://jmeter-plugins.org/

+0

儘管這個鏈接可能回答這個問題,但最好在這裏包含答案的基本部分,並提供供參考的鏈接。如果鏈接頁面更改,則僅鏈接答案可能會失效。 - [來自評論](/ review/low-quality-posts/11048142) – Hamad

+0

我收錄了您期望的鏈接。你可以不要低估我? – Lukasz

+0

我還沒有投票呢! – Hamad