View Javadoc
1   package net.avcompris.guixer.core;
2   
3   import static com.google.common.base.Preconditions.checkNotNull;
4   import static net.avcompris.guixer.core.LoggerUtils.escape;
5   import static net.avcompris.guixer.core.LoggerUtils.formatKeys;
6   
7   import java.io.IOException;
8   
9   import javax.annotation.Nullable;
10  
11  import org.openqa.selenium.TimeoutException;
12  import org.openqa.selenium.WebDriverException;
13  
14  abstract class AbstractCommandLoggerImpl extends CommandDecorator implements Command {
15  
16  	@Nullable
17  	private final String actionShortDescription;
18  
19  	protected final Logger logger;
20  
21  	protected AbstractCommandLoggerImpl( //
22  			final Command delegate, //
23  			final Context context, //
24  			final Logger logger, //
25  			@Nullable final String actionShortDescription //
26  	) throws IOException {
27  
28  		super(delegate);
29  
30  		this.actionShortDescription = actionShortDescription;
31  
32  		this.logger = checkNotNull(logger, "logger");
33  
34  		// context.getDumperLogger();
35  
36  		logger.startCommand(actionShortDescription);
37  	}
38  
39  	protected AbstractCommandLoggerImpl( //
40  			final Command delegate, //
41  			final Context context, //
42  			final Logger logger //
43  	) throws IOException {
44  
45  		this(delegate, context, logger, null);
46  	}
47  
48  	@Override
49  	public final Command get() throws IOException {
50  
51  		logger.startStep("get()");
52  
53  		delegate.get();
54  
55  		logger.endStep();
56  
57  		return this;
58  	}
59  
60  	@Override
61  	public final Command takeScreenshot() throws IOException {
62  
63  		logger.startStep("takeScreenshot()");
64  
65  		delegate.takeScreenshot();
66  
67  		logger.endStep();
68  
69  		return this;
70  	}
71  
72  	@Override
73  	public final Command takeScreenshot(final String label) throws IOException {
74  
75  		logger.startStep("takeScreenshot(" + escape(label) + ")");
76  
77  		delegate.takeScreenshot(label);
78  
79  		logger.endStep();
80  
81  		return this;
82  	}
83  
84  	@Override
85  	public final Command sleep(final int ms) throws IOException {
86  
87  		logger.startStep("sleep(" + ms + ")");
88  
89  		delegate.sleep(ms);
90  
91  		logger.endStep();
92  
93  		return this;
94  	}
95  
96  	@Override
97  	public final Command waitFor(final String locator) throws IOException {
98  
99  		logger.startStep("waitFor(" + escape(locator) + ")");
100 
101 		try {
102 
103 			delegate.waitFor(locator);
104 
105 		} catch (final TimeoutException e) {
106 
107 			handleError(e);
108 		}
109 
110 		logger.endStep();
111 
112 		return this;
113 	}
114 
115 	@Override
116 	public final Command waitFor(final String locator, final int timeOutSeconds) throws IOException {
117 
118 		logger.startStep("waitFor(" + escape(locator) + ", " + timeOutSeconds + ")");
119 
120 		try {
121 
122 			delegate.waitFor(locator, timeOutSeconds);
123 
124 		} catch (final TimeoutException e) {
125 
126 			handleError(e);
127 		}
128 
129 		logger.endStep();
130 
131 		return this;
132 	}
133 
134 	@Override
135 	public final Command waitForVisible(final String locator) throws IOException {
136 
137 		logger.startStep("waitForVisible(" + escape(locator) + ")");
138 
139 		try {
140 
141 			delegate.waitForVisible(locator);
142 
143 		} catch (final TimeoutException e) {
144 
145 			handleError(e);
146 		}
147 
148 		logger.endStep();
149 
150 		return this;
151 	}
152 
153 	@Override
154 	public final Command waitForVisible(final String locator, final int timeOutSeconds) throws IOException {
155 
156 		logger.startStep("waitForVisible(" + escape(locator) + ", " + timeOutSeconds + ")");
157 
158 		try {
159 
160 			delegate.waitForVisible(locator, timeOutSeconds);
161 
162 		} catch (final TimeoutException e) {
163 
164 			handleError(e);
165 		}
166 
167 		logger.endStep();
168 
169 		return this;
170 	}
171 
172 	@Override
173 	public final SwitchTo switchTo() throws IOException {
174 
175 		return new SwitchToLoggerImpl( //
176 				delegate.switchTo(), //
177 				this, //
178 				logger);
179 	}
180 
181 	@Override
182 	public final Command log(@Nullable final String text) throws IOException {
183 
184 		logger.startStep("log(" + escape(text) + ")");
185 
186 		delegate.log(text);
187 
188 		logger.endStep();
189 
190 		return this;
191 	}
192 
193 	@Override
194 	public final Command clear(final String locator) throws IOException {
195 
196 		logger.startStep("clear(" + escape(locator) + ")");
197 
198 		try {
199 
200 			delegate.clear(locator);
201 
202 		} catch (final WebDriverException e) {
203 
204 			handleError(e);
205 		}
206 
207 		logger.endStep();
208 
209 		return this;
210 	}
211 
212 	@Override
213 	public final Command click(final String locator) throws IOException {
214 
215 		logger.startStep("click(" + escape(locator) + ")");
216 
217 		try {
218 
219 			delegate.click(locator);
220 
221 		} catch (final WebDriverException e) {
222 
223 			handleError(e);
224 		}
225 
226 		logger.endStep();
227 
228 		return this;
229 	}
230 
231 	@Override
232 	public final Command submit(final String locator) throws IOException {
233 
234 		logger.startStep("submit(" + escape(locator) + ")");
235 
236 		try {
237 
238 			delegate.submit(locator);
239 
240 		} catch (final WebDriverException e) {
241 
242 			handleError(e);
243 		}
244 
245 		logger.endStep();
246 
247 		return this;
248 	}
249 
250 	@Override
251 	public final Command select(final String locator, final int value) throws IOException {
252 
253 		logger.startStep("select(" + escape(locator) + ", " + value + ")");
254 
255 		try {
256 
257 			delegate.select(locator, value);
258 
259 		} catch (final WebDriverException e) {
260 
261 			handleError(e);
262 		}
263 
264 		logger.endStep();
265 
266 		return this;
267 	}
268 
269 	@Override
270 	public final Command select(final String locator, final String value) throws IOException {
271 
272 		logger.startStep("select(" + escape(locator) + ", " + escape(value) + ")");
273 
274 		try {
275 
276 			delegate.select(locator, value);
277 
278 		} catch (final WebDriverException e) {
279 
280 			handleError(e);
281 		}
282 
283 		logger.endStep();
284 
285 		return this;
286 	}
287 
288 	protected abstract void handleError(WebDriverException e) throws IOException;
289 
290 	@Override
291 	public final Command assertHasClass(final String locator, final String className) throws IOException {
292 
293 		logger.startStep("assertHasClass(" + escape(locator) + ", " + escape(className) + ")");
294 
295 		try {
296 
297 			delegate.assertHasClass(locator, className);
298 
299 		} catch (final WebDriverException e) {
300 
301 			handleError(e);
302 		}
303 
304 		logger.endStep();
305 
306 		return this;
307 	}
308 
309 	@Override
310 	public final Command assertDoesntHaveClass(final String locator, final String className) throws IOException {
311 
312 		logger.startStep("assertDoesntHaveClass(" + escape(locator) + ", " + escape(className) + ")");
313 
314 		try {
315 
316 			delegate.assertDoesntHaveClass(locator, className);
317 
318 		} catch (final WebDriverException e) {
319 
320 			handleError(e);
321 		}
322 
323 		logger.endStep();
324 
325 		return this;
326 	}
327 
328 	@Override
329 	public final Command sendKeys(final String locator, final CharSequence... keys) throws IOException {
330 
331 		logger.startStep("sendKeys(" + escape(locator) + "" + formatKeys(keys) + ")");
332 
333 		try {
334 
335 			delegate.sendKeys(locator, keys);
336 
337 		} catch (final WebDriverException e) {
338 
339 			handleError(e);
340 		}
341 
342 		logger.endStep();
343 
344 		return this;
345 	}
346 
347 	@Override
348 	public final Command sendKeysSecret(final String locator, final String secret) throws IOException {
349 
350 		logger.startStep("sendKeysSecret(" + escape(locator) + ", ***)");
351 
352 		try {
353 
354 			delegate.sendKeysSecret(locator, secret);
355 
356 		} catch (final WebDriverException e) {
357 
358 			handleError(e);
359 		}
360 
361 		logger.endStep();
362 
363 		return this;
364 	}
365 
366 	@Override
367 	public final Command skip() throws IOException {
368 
369 		final Command skip = new Skip(this, logger);
370 
371 		return skip;
372 	}
373 
374 	private final Command applyWhen(final boolean condition) throws IOException {
375 
376 		return condition ? this : skip();
377 	}
378 
379 	@Override
380 	public final Command when(final String label, boolean condition) throws IOException {
381 
382 		logger.startStep("when(" + escape(label) + "," + condition + ")");
383 
384 		final Command when = applyWhen(condition);
385 
386 		logger.endStep();
387 
388 		return when;
389 	}
390 
391 	@Override
392 	public final Command when(final boolean condition) throws IOException {
393 
394 		logger.startStep("when(" + condition + ")");
395 
396 		final Command when = applyWhen(condition);
397 
398 		logger.endStep();
399 
400 		return when;
401 	}
402 }