View Javadoc
1   package io.guixer.logs;
2   
3   import static com.google.common.base.Charsets.UTF_8;
4   import static com.google.common.base.Preconditions.checkNotNull;
5   import static io.guixer.logs.StepTypes.serialize;
6   import static org.apache.commons.lang3.StringUtils.splitByWholeSeparatorPreserveAllTokens;
7   
8   import java.io.File;
9   import java.io.FileOutputStream;
10  import java.io.IOException;
11  import java.io.OutputStream;
12  import java.io.OutputStreamWriter;
13  import java.io.PrintWriter;
14  import java.io.Writer;
15  
16  import javax.annotation.Nullable;
17  
18  import org.apache.commons.lang3.NotImplementedException;
19  
20  import io.guixer.logs.ElaborateLog.Attribute;
21  import io.guixer.logs.ElaborateLog.Group;
22  import io.guixer.logs.ElaborateLog.Intent;
23  import io.guixer.logs.ElaborateLog.Locator;
24  import io.guixer.logs.ElaborateLog.LogError;
25  import io.guixer.logs.ElaborateLog.Status;
26  import io.guixer.logs.ElaborateLog.Step;
27  import io.guixer.types.AttributeScope;
28  import io.guixer.types.LocatorType;
29  import io.guixer.types.ResultType;
30  import io.guixer.types.StepType;
31  
32  public class YamlLogDumper {
33  
34  	public static void generateYAML(
35  		final ElaborateLog log,
36  		final File yamlOutFile
37  	) throws IOException {
38  
39  		checkNotNull(yamlOutFile, "yamlOutFile");
40  
41  		try (OutputStream os = new FileOutputStream(yamlOutFile)) {
42  
43  			try (Writer writer = new OutputStreamWriter(os, UTF_8)) {
44  
45  				try (PrintWriter pw = new PrintWriter(writer)) {
46  
47  					generateYAML(pw, log);
48  				}
49  			}
50  		}
51  	}
52  
53  	private static void generateYAML(
54  		final PrintWriter pw,
55  		final ElaborateLog log
56  	) {
57  
58  		pw.println("guixerVersion: " + log.getGuixerVersion());
59  		pw.println("date: " + log.getDateAsString());
60  		pw.println("testClassName: " + log.getTestClassName());
61  		pw.println("testClassSimpleName: " + log.getTestClassSimpleName());
62  		pw.println("testMethodName: " + log.getTestMethodName());
63  		pw.println("timeMillis: " + log.getTimeMillis());
64  		if (log.getScenario() != null) {
65  			pw.println("scenario: " + log.getScenario());
66  		}
67  
68  		if (log.getAttributes().length != 0) {
69  			pw.println("attributes:");
70  			for (final Attribute attribute : log.getAttributes()) {
71  				pw.println("  - scope: " + attribute.getScope());
72  				pw.println("    name: " + attribute.getName());
73  				pw.println("    value: " + attribute.getValue());
74  			}
75  		}
76  
77  		if (log.getBefore() != null) {
78  
79  			pw.println("before:");
80  
81  			dumpSteps(pw, "  ", log.getBefore().getSteps());
82  		}
83  
84  		if (log.getIntents().length != 0) {
85  
86  			pw.println();
87  			pw.println("intents:");
88  
89  			dumpIntents(pw, "  ", log.getIntents());
90  		}
91  
92  		if (log.isInError()) {
93  
94  			final LogError error = log.getError();
95  
96  			pw.println();
97  
98  			pw.println("error:");
99  			pw.println("    timeMillis: " + error.getTimeMillis());
100 			pw.println("    screenshot:");
101 			pw.println("        timeMillis: " + error.getScreenshot().getTimeMillis());
102 			pw.println("        fileName: " + error.getScreenshot().getFileName());
103 			pw.println("    trace: |");
104 
105 			for (final String line : splitByWholeSeparatorPreserveAllTokens(error.getTrace(), "\n")) {
106 
107 				pw.println("        " + line);
108 			}
109 		}
110 
111 		pw.println();
112 
113 		pw.println("successCount: " + log.getSuccessCount());
114 		pw.println("failureCount: " + log.getFailureCount());
115 
116 		pw.println();
117 
118 		if (log.getDone() != null) {
119 			pw.println("done:");
120 			pw.println("  timeMillis: " + log.getDone().getTimeMillis());
121 			pw.println("  message: " + log.getDone().getMessage());
122 		}
123 
124 		/*
125 		 * 
126 		before:
127 		- timeMillis: 1691943746471
128 		type: call
129 		callable: log in
130 		- timeMillis: 1691943746471
131 		type: attribute
132 		scope: RUN
133 		name: runtime
134 		value: Davids-Mac
135 		- timeMillis: 1691943746471
136 		type: attribute
137 		scope: RUN
138 		name: client-os
139 		value: Linux
140 		- timeMillis: 1691943746471
141 		type: attribute
142 		scope: RUN
143 		name: client-browser
144 		value: Firefox 110.0
145 		
146 		 */
147 	}
148 
149 	private static void dumpIfNotNull(
150 		final PrintWriter pw,
151 		final String indent,
152 		final String propertyName,
153 		@Nullable final String value
154 	) {
155 
156 		if (value != null) {
157 
158 			pw.println(indent + propertyName + ": " + escape(value));
159 		}
160 	}
161 
162 	private static void dumpIfNotNull(
163 		final PrintWriter pw,
164 		final String indent,
165 		final String propertyName,
166 		@Nullable final Integer value
167 	) {
168 
169 		if (value != null) {
170 
171 			pw.println(indent + propertyName + ": " + value);
172 		}
173 	}
174 
175 	private static void dumpIfNotNull(
176 		final PrintWriter pw,
177 		final String indent,
178 		final String propertyName,
179 		@Nullable final Long value
180 	) {
181 
182 		if (value != null) {
183 
184 			pw.println(indent + propertyName + ": " + value);
185 		}
186 	}
187 
188 	private static void dumpIfNotNull(
189 		final PrintWriter pw,
190 		final String indent,
191 		final String propertyName,
192 		@Nullable final AttributeScope value
193 	) {
194 
195 		if (value != null) {
196 
197 			pw.println(indent + propertyName + ": " + value);
198 		}
199 	}
200 
201 	private static void dumpIfNotNull(
202 		final PrintWriter pw,
203 		final String indent,
204 		final String propertyName,
205 		@Nullable final ResultType value
206 	) {
207 
208 		if (value != null) {
209 
210 			pw.println(indent + propertyName + ": " + value);
211 		}
212 	}
213 
214 	private static void dumpIfNotNull(
215 		final PrintWriter pw,
216 		final String indent,
217 		final String propertyName,
218 		@Nullable final Locator locator
219 	) {
220 
221 		if (locator != null) {
222 
223 			pw.println(indent + "locator:");
224 
225 			final String typeAsString;
226 			if (locator.getType() == LocatorType.BY_ID) {
227 				typeAsString = "By.id";
228 			} else if (locator.getType() == LocatorType.BY_CSS_SELECTOR) {
229 				typeAsString = "By.cssSelector";
230 			} else if (locator.getType() == LocatorType.BY_XPATH) {
231 				typeAsString = "By.xpath";
232 			} else {
233 
234 				throw new NotImplementedException("locator.class: " + locator.getClass().getName());
235 			}
236 			pw.println(indent + "    type: " + typeAsString);
237 			pw.println(indent + "    value: " + escape(locator.getValue()));
238 		}
239 	}
240 
241 	private static void dumpSteps(
242 		final PrintWriter pw,
243 		final String indent,
244 		final Step[] steps
245 	) {
246 
247 		for (final Step step : steps) {
248 
249 			pw.println();
250 
251 			final StepType type = step.getType();
252 
253 			pw.println(indent + "- type: " + serialize(type));
254 
255 			dumpIfNotNull(pw, indent + "  ", "callable", step.getCallable());
256 			dumpIfNotNull(pw, indent + "  ", "fileName", step.getFileName());
257 			dumpIfNotNull(pw, indent + "  ", "label", step.getLabel());
258 			dumpIfNotNull(pw, indent + "  ", "locator", step.getLocator());
259 			dumpIfNotNull(pw, indent + "  ", "message", step.getMessage());
260 			dumpIfNotNull(pw, indent + "  ", "name", step.getName());
261 			dumpIfNotNull(pw, indent + "  ", "namespace", step.getNamespace());
262 			dumpIfNotNull(pw, indent + "  ", "result", step.getResultType());
263 			dumpIfNotNull(pw, indent + "  ", "seconds", step.getSeconds());
264 			dumpIfNotNull(pw, indent + "  ", "scope", step.getScope());
265 			dumpIfNotNull(pw, indent + "  ", "timeMillis", step.getTimeMillis());
266 			dumpIfNotNull(pw, indent + "  ", "script", step.getScript());
267 			dumpIfNotNull(pw, indent + "  ", "url", step.getUrl());
268 			dumpIfNotNull(pw, indent + "  ", "value", step.getValue());
269 		}
270 	}
271 
272 	private static String escape(
273 		final String s
274 	) {
275 
276 		if (!s.contains("\"")) {
277 			return '"' + s + '"';
278 		} else if (!s.contains("'")) {
279 			return '\'' + s + '\'';
280 		} else {
281 			throw new NotImplementedException("s: " + s);
282 		}
283 	}
284 
285 	private static void dumpIntents(
286 		final PrintWriter pw,
287 		final String indent,
288 		final Intent[] intents
289 	) {
290 
291 		for (final Intent intent : intents) {
292 
293 			pw.println();
294 
295 			final Group group = intent.getGroup();
296 			final Status status = intent.getStatus();
297 
298 			if (group != null) {
299 
300 				pw.println(indent + "- beginAtMs: " + group.getBeginAtMs());
301 				dumpIfNotNull(pw, indent + "  ", "endAtMs", group.getEndAtMs());
302 				pw.println(indent + "  group: " + group.getName());
303 
304 				if (group.getIntents().length != 0) {
305 
306 					pw.println(indent + "  intents:");
307 					dumpIntents(pw, indent + "    ", group.getIntents());
308 				}
309 
310 			} else if (status != null) {
311 
312 				pw.println(indent + "- statusAtMs: " + status.getStatusAtMs());
313 				pw.println(indent + "  status: " + status.getLabel());
314 
315 			} else {
316 
317 				pw.println(indent + "- timeMillis: " + intent.getTimeMillis());
318 				pw.println(indent + "  title: " + escape(intent.getTitle()));
319 
320 				if (intent.getSteps().length != 0) {
321 
322 					pw.println(indent + "  steps:");
323 
324 					dumpSteps(pw, indent + "  ", intent.getSteps());
325 				}
326 			}
327 		}
328 	}
329 }