-
Notifications
You must be signed in to change notification settings - Fork 8
/
CucumberRecipeExecutor.java
188 lines (152 loc) · 6.45 KB
/
CucumberRecipeExecutor.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package com.smartbear.readyapi.testserver.cucumber;
import com.smartbear.readyapi.client.ExecutionListener;
import com.smartbear.readyapi.client.TestRecipe;
import com.smartbear.readyapi.client.execution.Execution;
import com.smartbear.readyapi.client.execution.RecipeExecutor;
import com.smartbear.readyapi.client.execution.TestServerClient;
import com.smartbear.readyapi.client.model.TestCase;
import cucumber.api.Scenario;
import io.swagger.util.Json;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.FileWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Map;
/**
* Executes a TestServer Recipe class using the TestServer endpoint specified
* in the testserver.endpoint system property (defaults to the public
* TestServer instance).
*/
public class CucumberRecipeExecutor {
private final static Logger LOG = LoggerFactory.getLogger(CucumberRecipeExecutor.class);
private static final String TESTSERVER_ENDPOINT = "testserver.endpoint";
private static final String TESTSERVER_USER = "testserver.user";
private static final String TESTSERVER_PASSWORD = "testserver.password";
private static final String DEFAULT_TESTSERVER_ENDPOINT = "http://testserver.readyapi.io:8080";
private static final String DEFAULT_TESTSERVER_USER = "demoUser";
private static final String DEFAULT_TESTSERVER_PASSWORD = "demoPassword";
private RecipeExecutor executor;
private boolean async = false;
public CucumberRecipeExecutor() throws MalformedURLException {
Map<String, String> env = System.getenv();
URL url = new URL(env.getOrDefault(TESTSERVER_ENDPOINT,
System.getProperty(TESTSERVER_ENDPOINT, DEFAULT_TESTSERVER_ENDPOINT)));
TestServerClient testServerClient = TestServerClient.fromUrl( url.toString() );
String user = env.getOrDefault(TESTSERVER_USER,
System.getProperty(TESTSERVER_USER, DEFAULT_TESTSERVER_USER));
String password = env.getOrDefault(TESTSERVER_PASSWORD,
System.getProperty(TESTSERVER_PASSWORD, DEFAULT_TESTSERVER_PASSWORD));
testServerClient.setCredentials(user, password);
executor = testServerClient.createRecipeExecutor();
}
/**
* Executes the specified TestCase and returns the Execution. If a scenario
* is specified and the testserver.cucumber.logfolder system property is set,
* the generated recipe will be written to the specified folder.
*
* It is possible to temporarily "bypass" recipe execution by specifying
* a testserver.cucumber.silent property - in which case testcases will not be
* submitted to the server, but still logged to the above folder.
*
* @param testCase the TestCase to execute
* @param scenario the Cucumber scenario used to generate the specified Recipe
* @return the TestServer Execution for the executed TestCase
* @throws com.smartbear.readyapi.client.execution.ApiException if recipe execution failes
*/
public Execution runTestCase(TestCase testCase, Scenario scenario) {
TestRecipe testRecipe = new TestRecipe(testCase);
if (LOG.isDebugEnabled()) {
LOG.debug(testRecipe.toString());
}
String logFolder = System.getProperty( "testserver.cucumber.logfolder", null );
if( scenario != null && logFolder != null ){
logScenarioToFile(testRecipe, scenario, logFolder);
}
return async ? executor.submitRecipe( testRecipe ) : executor.executeRecipe(testRecipe);
}
/**
* Writes the specified testRecipe to a folder/file name deducted from the
* specified scenario
*
* @param testRecipe the test recipe to log
* @param scenario the associated Cucumber scenario
* @param logFolder the root folder for generated folders and files
*/
protected void logScenarioToFile(TestRecipe testRecipe, Scenario scenario, String logFolder) {
try {
File folder = new File( logFolder );
if( !folder.exists() || !folder.isDirectory()){
folder.mkdirs();
}
String[] pathSegments = scenario.getId().split(";");
File scenarioFolder = folder;
int fileIndex = 0;
if( pathSegments.length > 1 ) {
scenarioFolder = new File(folder, pathSegments[0]);
if (scenarioFolder.exists() || !scenarioFolder.isDirectory()) {
scenarioFolder.mkdirs();
}
fileIndex = 1;
}
String filename = pathSegments[fileIndex];
for( int c = fileIndex+1; c < pathSegments.length; c++ ){
String segment = pathSegments[c].trim();
if( !StringUtils.isBlank( segment )){
filename += "_" + segment;
}
}
filename += ".json";
File scenarioFile = new File( scenarioFolder, filename );
FileWriter writer = new FileWriter( scenarioFile );
LOG.info("Writing recipe to " + folder.getName() + File.separatorChar + scenarioFolder.getName() +
File.separatorChar + scenarioFile.getName());
writer.write( Json.pretty(testRecipe) );
writer.close();
} catch (Exception e) {
LOG.error("Failed to write recipe to logFolder [" + logFolder + "]", e );
}
}
/**
* Adds a listener for test execution events
*
* @param listener the listener to add
*/
public void addExecutionListener(ExecutionListener listener) {
executor.addExecutionListener(listener);
}
/**
* Removes a previously added listener for test execution events
*
* @param listener the listener to remove
*/
public void removeExecutionListener(ExecutionListener listener) {
executor.removeExecutionListener(listener);
}
/**
* Get the underlying RecipeExecutor used to execute the generated recipes.
*
* @return the underlying RecipeExecutor
*/
public RecipeExecutor getExecutor() {
return executor;
}
/**
* Tells is execution of recipes will be async
*
* @return execution mode
*/
public boolean isAsync() {
return async;
}
/**
* Sets if recipe execution will be async
*
* @param async execution mode
*/
public void setAsync(boolean async) {
this.async = async;
}
}