这里我试图实现对不同角色使用相同的登录测试片段以及 BeanShell 后处理器。
问题是每次选择员工角色凭证,即使设置线程组 1_Admin 编号:1 和设置线程组 1_雇员编号:共 3 个用户的 2
这是测试设计 测试计划
设置线程组 1 - 管理员
Login_Credentials_Admin CSV Config
Login_access_Admin CSV Config
user Defined Variables: user_type : Admin
Include Controller(Login Test Fragment)
设置线程组2-员工
Login_Credentials_Employee CSV Config
Login_access_Employee CSV Config
user Defined Variables: user_type : Employee
Include Controller(Login Test Fragment)
每个角色的 BeanShell 后处理器的实际代码保存在单独的 CSV 文件中并传入以下代码
这是 BeanShell 后处理器
// Read the file path of the BeanShell script based on user type
String userType = vars.get("user_type");
String beanshellPostProcessorFile = "";
if (userType.equals("Admin")) {
beanshellPostProcessorFile = vars.get("beanshell_post_processor_file_admin");
} else if (userType.equals("Clinical_Employee")) {
beanshellPostProcessorFile = vars.get("beanshell_post_processor_file_XXXX_employee");
}
try {
// Read the contents of the BeanShell script file
String scriptCode = FileUtils.readFileToString(new File(beanshellPostProcessorFile), "UTF-8"); // Use "UTF-8" directly
// Evaluate the BeanShell code
eval(scriptCode);
// Log information
log.info("Dyanmic BeanShell PostProcessor File: " + beanshellPostProcessorFile + "; user: " + vars.get("user_type"));
} catch (IOException e) {
log.error("Error reading script file: " + e.getMessage());
}
根据用户定义变量文档:
用户定义的变量元素允许您定义一组初始变量,就像在测试计划中一样。
请注意,测试计划中的所有 UDV 元素(无论它们位于何处)都会在开始时进行处理。
所以我希望你永远能得到
Employee
家伙的价值
您需要另一种方法来定义
user_type
,例如用户参数或设置变量操作或CSV文件。
关于 Beanshell,从 JMeter 3.1 开始,建议使用 JSR223 测试元素和 Groovy 语言进行脚本编写,因此请考虑迁移。更多信息:Apache Groovy:Groovy 的用途是什么?