JMeter断言响应已成功下载

时间:2014-06-16 19:47:55

标签: jmeter response

我正在使用JMeter测试我网站上的一些功能。通过使用Save Responses to a file元素,我能够成功发出通过JMeter下载pdf的请求。但是,我很好奇是否有断言检查文件是否已经实际下载(如果可能的话,是我指定的格式!)。我知道我可以简单地查看该文件,但我希望能够使其更加自动化。我已检查过"仅保存成功回复,"但我想确保实际上已经保存了响应。

1 个答案:

答案 0 :(得分:1)

我认为您需要使用Beanshell Assertion

检查文件存在,大小和内容类型的示例代码如下:

File file = new File("/path/to/downloaded/file");

//check file existence
if (!file.exists())
{
    Failure = true;
    FailureMessage = "File " + file.getName() + " does not exist";
}

//check file size

long expectedSize = SampleResult.getBodySize();
long actualSize = file.length();

if (expectedSize != actualSize)
{
    Failure = true;
    FailureMessage = "Actual file size differs from expected. Expected: " + expectedSize + " and got: " + actualSize    ;
}

//check content type

String expectedType = SampleResult.getContentType();
String actualType = file.toURI().toURL().openConnection().getContentType();

if (!expectedType.equals(actualType))
{
    Failure = true;
    FailureMessage = "Response types are different. Expected: " + expectedType + " and got: " + actualType;
}

有关JMeter Assertions超级大国的更多信息,请参阅How to Use JMeter Assertions in 3 Easy Steps指南。