2015-02-09 160 views

回答

2

JMeter不提供任何測試元素來創建隨機文件並將其刪除,因此您必須編寫相關代碼。

例如:

  1. 添加Beanshell PreProcessorBeanshell Post Processor作爲進行文件上傳
  2. 將下面的代碼放到BeanShell的預處理器 「腳本」 的要求的兒童區

    import org.apache.commons.io.FileUtils; 
    File myFile = new File("myFile.txt"); 
    FileUtils.writeStringToFile(myFile, "JMeter rocks!"); 
    

    的上面的代碼在JMeter當前的工作目錄中創建「myFile.txt」文件並寫入「JMeter rocks!」行至它

  3. 爲了刪除請求後,該文件,你可以添加以下代碼到的BeanShell PostProcessor中

    import org.apache.commons.io.FileUtils; 
    FileUtils.deleteQuietly(new File("myFile.txt")); 
    

欲瞭解更多有關在Apache的JMeter的使用BeanShell的腳本看到How to use BeanShell: JMeter's favorite built-in component指南。

+0

不錯!我可以在HTTP請求上使用我在PreProcessor上設置的路徑值的變量嗎?並在PosProcessor上恢復它? – placplacboom 2015-02-09 10:26:28

+1

你可以通過設置一個變量來捕獲上面的文件名。例如 - vars.put(「filename」,myFile.getCanonicalPath());並在後處理程序中,您可以使用FileUtils.deleteQuietly(new File(vars.get(「filename」))); – 2015-02-10 00:35:28

+2

下面是[文件上載測試計劃](https://www.redline13.com/share/testplan/11595)的完整示例,它會生成隨機大小的文件。 BeanShell預處理器代碼將爲 'import org.apache.commons.io.FileUtils; import org.apache.commons.lang3.RandomStringUtils; import org.apache.commons.lang3.RandomUtils; File myFile = File.createTempFile(「upload-」,「」); FileUtils.writeStringToFile(myFile,RandomStringUtils.random(RandomUtils.nextInt(1000,100000)),「UTF-8」); vars.put(「filename」,myFile.getCanonicalPath());' – 2015-02-10 06:06:49