1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.unitedinternet.portal.selenium.utils.logging;
18
19 import static org.fest.assertions.Assertions.assertThat;
20 import static org.junit.Assert.assertEquals;
21 import static org.junit.Assert.assertNotNull;
22 import static org.junit.Assert.assertTrue;
23
24 import java.io.Writer;
25 import java.text.MessageFormat;
26
27 import org.junit.Ignore;
28 import org.junit.Test;
29
30 public class HtmlResultFormatterTest {
31 @Ignore("This Test should pass, but research has to be made")
32 @Test
33 public void messageFormatTest() {
34 String res = MessageFormat.format("{0}", 1234);
35 assertThat(res).isEqualTo("1234");
36 }
37
38 @Test
39 public void formatHeader() {
40
41 TestMetricsBean testMetrics = new TestMetricsBean();
42 testMetrics.setUserAgent("UserAgentTestString");
43 HtmlResultFormatter testFormatter = new HtmlResultFormatter(null);
44 String header = testFormatter.formatHeader(testMetrics);
45 assertNotNull(header);
46 assertThat(header).contains("UserAgentTestString");
47 assertThat(header).excludes("'''");
48 assertThat(header).excludes("'{'");
49 assertThat(header).excludes("'}'");
50 }
51
52 @Test
53 public void formatHeaderCorrectEncoding() {
54 TestMetricsBean testMetrics = new TestMetricsBean();
55 testMetrics.setUserAgent("UserAgentTestString");
56 HtmlResultFormatter testFormatter = new HtmlResultFormatter(null, "UTF-8");
57 String header = testFormatter.formatHeader(testMetrics);
58 assertNotNull(header);
59 assertThat(header).contains("UserAgentTestString");
60 assertThat(header).contains("UTF-8");
61 }
62
63 @Test
64 public void formatScreenshotFileImgTag_RelativePathEndswithSlash() {
65 HtmlResultFormatter testFormatter = new HtmlResultFormatter(null, "UTF-8");
66 String scBaseUri = "blah/blah/";
67 String scFileName = "shotTest.png";
68 testFormatter.setScreenShotBaseUri(scBaseUri);
69
70 String result = testFormatter.formatScreenshotFileImgTag("file://foo/bar/" + scFileName);
71 assertThat(result).contains(scBaseUri + scFileName);
72 assertThat(result).excludes(scBaseUri + "/" + scFileName);
73 }
74
75 @Test
76 public void formatScreenshotFileImgTag_RelativePathEndswithNoSlash() {
77 HtmlResultFormatter testFormatter = new HtmlResultFormatter(null, "UTF-8");
78 String scBaseUri = "blah/blah";
79 String scFileName = "shotTest.png";
80 testFormatter.setScreenShotBaseUri(scBaseUri);
81
82 String result = testFormatter.formatScreenshotFileImgTag("file://foo/bar/" + scFileName);
83 assertThat(result).contains(scBaseUri + "/" + scFileName);
84 assertThat(result).excludes(scBaseUri + "//" + scFileName);
85 }
86
87 @Test
88 public void formatScreenshotFileImgTag_AbsolutePathHttpEndswithSlash() {
89 HtmlResultFormatter testFormatter = new HtmlResultFormatter(null, "UTF-8");
90 String scBaseUri = "http://my.domain.notthere/blah/blah/";
91 String scFileName = "shotTest.png";
92 testFormatter.setScreenShotBaseUri(scBaseUri);
93
94 String result = testFormatter.formatScreenshotFileImgTag("file://foo/bar/" + scFileName);
95 assertThat(result).contains(scBaseUri + scFileName);
96 assertThat(result).excludes(scBaseUri + "/" + scFileName);
97 }
98
99 @Test
100 public void formatScreenshotFileImgTag_AbsolutePathHttpEndswithNoSlash() {
101 HtmlResultFormatter testFormatter = new HtmlResultFormatter(null, "UTF-8");
102 String scBaseUri = "http://my.domain.notthere/blah/blah";
103 String scFileName = "shotTest.png";
104 testFormatter.setScreenShotBaseUri(scBaseUri);
105
106 String result = testFormatter.formatScreenshotFileImgTag("file://foo/bar/" + scFileName);
107 assertThat(result).contains(scBaseUri + "/" + scFileName);
108 assertThat(result).excludes(scBaseUri + "//" + scFileName);
109 }
110
111 @Test
112 public void formatScreenshotFileImgTag_AbsolutePathFileEndswithSlash() {
113 HtmlResultFormatter testFormatter = new HtmlResultFormatter(null, "UTF-8");
114 String scBaseUri = "file://my.domain.notthere/blah/blah/";
115 String scFileName = "shotTest.png";
116 testFormatter.setScreenShotBaseUri(scBaseUri);
117
118 String result = testFormatter.formatScreenshotFileImgTag("file://foo/bar/" + scFileName);
119 assertThat(result).contains(scBaseUri + scFileName);
120 assertThat(result).excludes(scBaseUri + "/" + scFileName);
121 }
122
123 @Test
124 public void formatScreenshotFileImgTag_AbsolutePathFileEndswithNoSlash() {
125 HtmlResultFormatter testFormatter = new HtmlResultFormatter(null, "UTF-8");
126 String scBaseUri = "file://my.domain.notthere/blah/blah";
127 String scFileName = "shotTest.png";
128 testFormatter.setScreenShotBaseUri(scBaseUri);
129
130 String result = testFormatter.formatScreenshotFileImgTag("file://foo/bar/" + scFileName);
131 assertThat(result).contains(scBaseUri + "/" + scFileName);
132 assertThat(result).excludes(scBaseUri + "//" + scFileName);
133 }
134
135 private Writer setUpMockWriter(final StringBuffer writeBuffer) {
136 return new Writer() {
137 public void flush() {
138 }
139
140 public void close() {
141 }
142
143 public void write(char[] cbuf, int off, int len) {
144 writeBuffer.append(cbuf);
145 }
146 };
147 }
148
149 @Test
150 public void commentLogEvent_twoArgs() {
151 final StringBuffer writeBuffer = new StringBuffer();
152 Writer mockWriter = setUpMockWriter(writeBuffer);
153
154 LoggingBean testBean = new LoggingBean();
155 testBean.setArgs(new String[] {"arg1", "arg2"});
156 HtmlResultFormatter testFormatter = new HtmlResultFormatter(mockWriter);
157 testFormatter.commentLogEvent(testBean);
158 assertThat(writeBuffer.toString()).startsWith(
159 "<tr class=\"title\"><td colspan=\"7\">arg1"
160 + "<span style=\"font-size:9px;font-family:arial,verdana,sans-serif;\">arg2</span>"
161 + "</td></tr>\n");
162 }
163
164 @Test
165 public void formatMetrics_failedCommands() {
166 TestMetricsBean testMetrics = new TestMetricsBean();
167 testMetrics.setFailedCommands(1);
168 HtmlResultFormatter testFormatter = new HtmlResultFormatter(null);
169 String metrics = testFormatter.formatMetrics(testMetrics);
170 assertNotNull(metrics);
171 assertThat(metrics).contains("<tr class=\"status_failed\"><td>failed commands:</td><td>1</td></tr>");
172 }
173
174 @Test
175 public void headerLogEvent() {
176
177 final StringBuffer writeBuffer = new StringBuffer();
178 Writer mockWriter = setUpMockWriter(writeBuffer);
179
180 TestMetricsBean testMetrics = new TestMetricsBean();
181
182 testMetrics.setUserAgent("UserAgentTestString");
183 HtmlResultFormatter testFormatter = new HtmlResultFormatter(mockWriter);
184 testFormatter.headerLogEvent(testMetrics);
185
186 String loggingResult = writeBuffer.toString();
187 assertThat(loggingResult).contains("UserAgentTestString");
188 assertThat(loggingResult).excludes("'''");
189 assertThat(loggingResult).excludes("'{'");
190 assertThat(loggingResult).excludes("'}'");
191 }
192
193 @Test
194 public void footerLogEvent() {
195 final StringBuffer writeBuffer = new StringBuffer();
196 Writer mockWriter = setUpMockWriter(writeBuffer);
197
198 HtmlResultFormatter testFormatter = new HtmlResultFormatter(mockWriter);
199 testFormatter.footerLogEvent();
200
201 String loggingResult = writeBuffer.toString();
202 assertTrue(loggingResult.startsWith(HtmlResultFormatter.HTML_FOOTER));
203 }
204
205 @Test
206 public void extraInformationLogEvent() {
207 HtmlResultFormatter testFormatter = new HtmlResultFormatter(null);
208 String result = testFormatter.extraInformationLogEvent(null);
209 assertThat(result).isEqualTo("");
210 }
211
212 @Test
213 public void commandLogEvent_commandFailed_noScreenshot() {
214 final StringBuffer writeBuffer = new StringBuffer();
215 Writer mockWriter = setUpMockWriter(writeBuffer);
216
217 LoggingBean testBean = new LoggingBean();
218 HtmlResultFormatter testFormatter = new HtmlResultFormatter(mockWriter);
219 testFormatter.commandLogEvent(testBean);
220
221 String result = writeBuffer.toString();
222 assertThat(result).contains("status_failed");
223 }
224
225 @Test
226 public void commandLogEvent_commandSuccess_noScreenshot() {
227 final StringBuffer writeBuffer = new StringBuffer();
228 Writer mockWriter = setUpMockWriter(writeBuffer);
229
230 LoggingBean testBean = new LoggingBean();
231 testBean.setCommandSuccessful(true);
232 HtmlResultFormatter testFormatter = new HtmlResultFormatter(mockWriter);
233 testFormatter.commandLogEvent(testBean);
234
235 String result = writeBuffer.toString();
236 assertThat(result).contains("status_done");
237 }
238
239 @Test
240 public void commandLogEvent_screenshot() {
241 final StringBuffer writeBuffer = new StringBuffer();
242 Writer mockWriter = setUpMockWriter(writeBuffer);
243
244 LoggingBean testBean = new LoggingBean();
245 testBean.setCommandName("captureScreenshot");
246 testBean.setArgs(new String[] {"arg1"});
247 HtmlResultFormatter testFormatter = new HtmlResultFormatter(mockWriter);
248 testFormatter.commandLogEvent(testBean);
249
250 String result = writeBuffer.toString();
251 assertThat(result).contains("arg1");
252 assertThat(result).contains("Selenium Screenshot");
253 }
254
255 @Test
256 public void commandLogEvent_screenshotWindowsPath() {
257 final String providedImgSrc = "E:\\somepath\\someImg.png";
258 final StringBuffer writeBuffer = new StringBuffer();
259 Writer mockWriter = setUpMockWriter(writeBuffer);
260
261 LoggingBean testBean = new LoggingBean();
262 testBean.setCommandName("captureScreenshot");
263 testBean.setArgs(new String[] {providedImgSrc});
264 HtmlResultFormatter testFormatter = new HtmlResultFormatter(mockWriter);
265 testFormatter.localFsPathSeparator = "\\";
266 testFormatter.setScreenShotBaseUri("myExtraPath/");
267 testFormatter.commandLogEvent(testBean);
268
269 String result = writeBuffer.toString();
270 assertThat(result).contains("Selenium Screenshot");
271 assertThat(result).contains("src=\"myExtraPath/someImg.png\"");
272 }
273
274 @Test
275 public void commandLogEvent_cmdExcludedFromLogging() {
276 final String cmdToBeExcluded = "getHtmlSource";
277 final String cmdArgToBeExcluded = "SHOULD-NOT-BE-IN-RESULT";
278 final StringBuffer writeBuffer = new StringBuffer();
279 Writer mockWriter = setUpMockWriter(writeBuffer);
280
281 LoggingBean testBean = new LoggingBean();
282 testBean.setCommandName(cmdToBeExcluded);
283 testBean.setExcludeFromLogging(true);
284 testBean.setArgs(new String[] {cmdArgToBeExcluded});
285 HtmlResultFormatter testFormatter = new HtmlResultFormatter(mockWriter);
286
287 testFormatter.commandLogEvent(testBean);
288
289 String result = writeBuffer.toString();
290 assertThat(result).excludes(cmdToBeExcluded);
291 assertThat(result).excludes(cmdArgToBeExcluded);
292 }
293
294 @Test
295 public void booleanCommandLogEvent_commandPass() {
296 final StringBuffer writeBuffer = new StringBuffer();
297 Writer mockWriter = setUpMockWriter(writeBuffer);
298
299 LoggingBean testBean = new LoggingBean();
300 testBean.setCommandSuccessful(true);
301 HtmlResultFormatter testFormatter = new HtmlResultFormatter(mockWriter);
302 testFormatter.booleanCommandLogEvent(testBean);
303
304 String result = writeBuffer.toString();
305 assertThat(result).contains("status_passed");
306 }
307
308 @Test
309 public void booleanCommandLogEvent_commandFail() {
310 final StringBuffer writeBuffer = new StringBuffer();
311 Writer mockWriter = setUpMockWriter(writeBuffer);
312
313 LoggingBean testBean = new LoggingBean();
314 testBean.setCommandSuccessful(false);
315 HtmlResultFormatter testFormatter = new HtmlResultFormatter(mockWriter);
316 testFormatter.booleanCommandLogEvent(testBean);
317
318 String result = writeBuffer.toString();
319 assertThat(result).contains("status_maybefailed");
320 }
321
322 @Test
323 public void generateEmptyColumns_loopZero() {
324 String result = HtmlResultFormatter.generateEmptyColumns(0);
325
326 assertThat(result).isEqualTo("");
327 }
328
329 @Test
330 public void generateEmptyColumns_loopOneTime() {
331 String result = HtmlResultFormatter.generateEmptyColumns(1);
332
333 assertEquals(HtmlResultFormatter.HTML_EMPTY_COLUMN, result);
334 }
335
336 @Test
337 public void formatCommandAsHtml_argsNotNull() {
338 LoggingBean testBean = new LoggingBean();
339 testBean.setArgs(new String[] {});
340 HtmlResultFormatter testFormatter = new HtmlResultFormatter(null);
341 String result = testFormatter.formatCommandAsHtml(testBean, "testClass", "testToolTipp");
342
343 assertThat(result).startsWith("<tr class=\"testClass\" title=\"testToolTipp\" alt=\"testToolTipp\"><td>");
344 assertThat(result).doesNotMatch(
345 "<tr class=\"testClass\" title=\"testToolTipp\" alt=\"testToolTipp\"><td></td><td>arg1</td><td> </td>");
346 }
347
348 @Test
349 public void formatCommandAsHtml_argsNotEmpty() {
350 LoggingBean testBean = new LoggingBean();
351 testBean.setArgs(new String[] {"arg1"});
352 HtmlResultFormatter testFormatter = new HtmlResultFormatter(null);
353 String result = testFormatter.formatCommandAsHtml(testBean, "testClass", "testToolTipp");
354
355 assertThat(result).startsWith(
356 "<tr class=\"testClass\" title=\"testToolTipp\" alt=\"testToolTipp\"><td></td><td>arg1</td><td> </td>");
357 assertThat(result).doesNotMatch(
358 "<tr class=\"testClass\" title=\"testToolTipp\" alt=\"testToolTipp\"><td></td><td> </td>");
359 }
360
361 @Test
362 public void formatCommandAsHtml_manyArgs() {
363 LoggingBean testBean = new LoggingBean();
364 testBean.setArgs(new String[] {"arg1", "arg2"});
365 HtmlResultFormatter testFormatter = new HtmlResultFormatter(null);
366 String result = testFormatter.formatCommandAsHtml(testBean, "testClass", "testToolTipp");
367
368 assertThat(result).startsWith(
369 "<tr class=\"testClass\" title=\"testToolTipp\" alt=\"testToolTipp\"><td></td><td>arg1</td><td>arg2</td>");
370 }
371
372 @Test
373 public void quoteHtml_commentSource() {
374 String qoutedString = HtmlResultFormatter.quoteHtml("<!-- a comment -->");
375 assertThat(qoutedString).isEqualTo("<!-- a comment -->");
376 }
377
378 @Test
379 public void quoteHtml_quotedSource() {
380 String qoutedString = HtmlResultFormatter.quoteHtml("<!-- a comment -->");
381 assertThat(qoutedString).isEqualTo("&lt;!-- a comment --&gt;");
382 }
383
384 @Test
385 public void quoteHtml_allQuoteCharacters() {
386 String qoutedString = HtmlResultFormatter.quoteHtml("<!-- a comment & some other -->");
387 assertThat(qoutedString).isEqualTo("<!-- a comment & some other -->");
388 }
389 }