在 SoapUI 中,我想在执行测试运行程序时向用户显示一个对话框,以便从项目中选择一个环境。
我添加了一个事件监听器ProjectRunListener.beforeRun
。目前,当该事件触发时,我有一个自定义库,它执行以下给定projectRunner
提供的内容:
public static void SetEndpoints(ProjectRunner projectRunner, Logger log = null){
if (projectRunner == null) {
_Log(log, "Parameter 'projectRunner' cannot be null.");
throw new Exception();
}
if (_RunOnce(projectRunner, "SetEndpoints")){
_Log(log, "Begin - Util.SetEndpoints (Project)");
def project = (Project)projectRunner.getProject();
_SetEndpointsForTestSuites(project.getTestSuiteList(), log);
_Log(log, "End - Util.SetEndpoints (Project - ${project.name})");
}
}
public static void SetEndpoints(TestSuiteRunner testSuiteRunner, Logger log = null){
if (testSuiteRunner == null) {
_Log(log, "Parameter 'testSuiteRunner' cannot be null.");
throw new Exception();
}
if (_RunOnce(testSuiteRunner, "SetEndpoints")){
_Log(log, "Begin - Util.SetEndpoints (TestSuite)");
def testSuite = (TestSuite)testSuiteRunner.getTestSuite();
_SetEndpointsForTestSuites([testSuite], log);
_Log(log, "End - Util.SetEndpoints (TestSuite - ${testSuite.name})");
}
}
public static void SetEndpoints(TestCaseRunner testCaseRunner, Logger log = null){
if (testCaseRunner == null) {
_Log(log, "Parameter 'testCaseRunner' cannot be null.");
throw new Exception();
}
if (_RunOnce(testCaseRunner, "SetEndpoints")){
_Log(log, "Begin - Util.SetEndpoints (TestCase)");
def testCase = (TestCase)testCaseRunner.getTestCase();
_SetEndpointsForTestCases([testCase], log);
_Log(log, "End - Util.SetEndpoints (TestCase - ${testCase.name})");
}
}
private static void _SetEndpointsForProject(Project project, Logger log = null){
_SetEndpointsForTestCases(project.getTestSuiteList(), log);
}
private static void _SetEndpointsForTestSuites(List<TestSuite> testSuites, Logger log = null){
def testCases = [] as List<TestCase>;
testSuites.each{
testCases.addAll(it.getTestCaseList() as List<TestCase>);
};
_SetEndpointsForTestCases(testCases, log);
}
private static void _SetEndpointsForTestCases(List<TestCase> testCases, Logger log = null){
// Get all test request steps
//def testSteps = testCase.getTestStepList();
def testSteps = [] as List<HttpRequestTestStep>;
testCases.each {
testSteps.addAll(it.getTestStepsOfType(RestTestRequestStep.class) as List<HttpRequestTestStep>);
}
// Loop across test request steps
def stepMap = [:]
testSteps.each {
Interface iface = it.getInterface();
if (stepMap[iface.name] == null){
List<String> endpointList = Arrays.asList(iface.getEndpoints()) as List<String>;
if ((endpointList != null) && (endpointList.size() > 0)) {
def selection = JOptionPane.showInputDialog(null, "Select an endpoint:",
iface.name, JOptionPane.QUESTION_MESSAGE, null,
endpointList as String[], endpointList[0]).toString();
if (selection == null){
stepMap.put(iface.name, "IGNORE");
_Log(log, "User cancelled endpoint selection for interface ${iface.name}");
}
else {
stepMap.put(iface.name, selection);
_Log(log, "User selected endpoint '${selection}' for interface ${iface.name}");
}
}
}
// Set endpoint
if (stepMap[iface.name] != "IGNORE"){
it.getHttpRequest().setEndpoint(stepMap[iface.name]);
}
}
}
private static boolean _RunOnce(TestRunner runner, String methodName){
def context = runner.getRunContext();
if (context.getProperty(methodName) != null)
return false;
context.setProperty(methodName, methodName);
return true;
}
private static void _Log(Logger log, Object message){
if (log != null){
log.info message;
}
}
该脚本,当给定来自项目、TestSuite 或 TestCase 的 TestRunner 时,它将查找要执行的每个测试请求步骤的所有接口,并询问用户选择哪个端点用于该测试用例。它只会为每个接口查询用户一次,并且不会查询用户是否已经执行(它可以通过在它检查的运行上下文中放置一个标志来确定)。
在事件中:ProjectRunListener.beforeRun
我称之为:
Util.SetEndpoints(projectRunner, log);
在事件中:TestSuiteRunListener.beforeRun
,TestRunListener.beforeRun
我称之为:
Util.SetEndpoints(testRunner, log);
因此,无论用户启动测试运行器的哪个级别,他/她仍将看到对话框以选择每个使用的接口的端点。
我相信这可以使用环境选择以更好的方式完成。我在 API 中看到我能够在项目级别设置环境,这可能会将所有接口端点设置为给定活动环境的适当值。但是,我看不到一种方法可以获取所有可用环境的列表。API 中的 Project 对象有.setActiveEnvironment()
,但没有提到任何关于获取环境列表的内容。
给定一个 Project 对象,是否有人知道获取所有已配置环境列表的方法?