当我从csv文件中获取一些名称(如汽车,电话,blabla)时,我使用小beanshell脚本进行测试,我必须在之前的测试步骤中检查html中的这些名称。每次检查的结果我都要写在其他文件中。我的问题是如果在html中找不到至少一个名称,则将此步骤标记为红色。代码在这里:
String Response = prev.getResponseDataAsString();
try {
File file = new File(vars.get("pathtocsv"));
FileReader fr = new FileReader(file);
BufferedReader reader = new BufferedReader(fr);
String line = reader.readLine();
if (line != null) {
String[] parts = line.split(",");
try{
FileWriter fw = new FileWriter(vars.get("pathtoresults"), true);
BufferedWriter bw = new BufferedWriter(fw);
PrintWriter pw = new PrintWriter(bw);
for(String i : parts) {
String utf8String= new String(i.getBytes("windows-1251"), "UTF-8");
if(Response.contains(utf8String)){
pw.println("Response contain element: " + i);
}
else{
pw.println("!!! Response doesn't contain element: " + i);
Failure=true;
FailureMessage = "!!! Response doesn't contain element: " + i;
log.warn( "!!! Response doesn't contain element " + utf8String);
prev.setResponseCode("400");
}
}
pw.close();
}
catch (IOException e){
e.printStackTrace();
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
答案 0 :(得分:1)
prev
是SampleResult类实例的简写,因此您可以使用prev.setSuccessful(false)方法从PostProcessor有条件地将采样器标记为失败。
请注意Beanshell是not the recommended scripting option,从JMeter 3.1开始,强烈建议用户切换到JSR223 Test Elements和Groovy语言,因为Groovy更符合Java,{{3}并且表现得更好。有关详细信息,请参阅a lot of JDK enhancements。
答案 1 :(得分:0)
您可以通过将成功标记为false来使父采样器失败:
prev.setSuccessful(false);