[927] | 1 | // Copyright 2012 Georg-August-Universität Göttingen, Germany |
---|
| 2 | // |
---|
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
---|
| 4 | // you may not use this file except in compliance with the License. |
---|
| 5 | // You may obtain a copy of the License at |
---|
| 6 | // |
---|
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
---|
| 8 | // |
---|
| 9 | // Unless required by applicable law or agreed to in writing, software |
---|
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
---|
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
| 12 | // See the License for the specific language governing permissions and |
---|
| 13 | // limitations under the License. |
---|
| 14 | |
---|
[864] | 15 | package de.ugoe.cs.autoquest.htmlmonitor; |
---|
| 16 | |
---|
| 17 | import static org.junit.Assert.*; |
---|
| 18 | |
---|
| 19 | import java.io.File; |
---|
[1069] | 20 | import java.util.Collection; |
---|
[1911] | 21 | import java.util.HashMap; |
---|
[1069] | 22 | import java.util.Iterator; |
---|
[864] | 23 | import java.util.List; |
---|
[1911] | 24 | import java.util.Map; |
---|
[864] | 25 | |
---|
| 26 | import org.apache.http.HttpEntity; |
---|
| 27 | import org.apache.http.HttpResponse; |
---|
| 28 | import org.apache.http.client.methods.HttpPost; |
---|
| 29 | import org.apache.http.entity.ContentType; |
---|
| 30 | import org.apache.http.entity.StringEntity; |
---|
| 31 | import org.apache.http.impl.client.DefaultHttpClient; |
---|
| 32 | import org.junit.After; |
---|
| 33 | import org.junit.Before; |
---|
| 34 | import org.junit.Test; |
---|
| 35 | |
---|
[1069] | 36 | import de.ugoe.cs.autoquest.eventcore.Event; |
---|
| 37 | import de.ugoe.cs.autoquest.eventcore.IEventType; |
---|
| 38 | import de.ugoe.cs.autoquest.eventcore.gui.KeyboardFocusChange; |
---|
| 39 | import de.ugoe.cs.autoquest.eventcore.gui.MouseButtonInteraction; |
---|
| 40 | import de.ugoe.cs.autoquest.eventcore.gui.MouseClick; |
---|
| 41 | import de.ugoe.cs.autoquest.eventcore.gui.MouseDoubleClick; |
---|
| 42 | import de.ugoe.cs.autoquest.eventcore.gui.Scroll; |
---|
| 43 | import de.ugoe.cs.autoquest.eventcore.guimodel.GUIModel; |
---|
| 44 | import de.ugoe.cs.autoquest.eventcore.guimodel.IGUIElement; |
---|
| 45 | import de.ugoe.cs.autoquest.plugin.html.HTMLLogParser; |
---|
| 46 | import de.ugoe.cs.autoquest.plugin.html.guimodel.HTMLDocument; |
---|
| 47 | import de.ugoe.cs.autoquest.plugin.html.guimodel.HTMLPageElement; |
---|
| 48 | import de.ugoe.cs.autoquest.plugin.html.guimodel.HTMLServer; |
---|
[864] | 49 | import de.ugoe.cs.util.console.TextConsole; |
---|
| 50 | |
---|
| 51 | /** |
---|
[1078] | 52 | * |
---|
[864] | 53 | * @author Patrick Harms |
---|
| 54 | */ |
---|
| 55 | public class HtmlMonitorTest { |
---|
| 56 | |
---|
| 57 | /** |
---|
| 58 | * |
---|
| 59 | */ |
---|
| 60 | public static final TextConsole CONSOLE = new TextConsole(); |
---|
| 61 | |
---|
| 62 | /** |
---|
| 63 | * |
---|
| 64 | */ |
---|
| 65 | private final static String LOG_FILE_DIR = "target/tmp/logfiles/"; |
---|
| 66 | |
---|
| 67 | /** |
---|
| 68 | * |
---|
| 69 | */ |
---|
| 70 | private static final int PORT = 19098; |
---|
| 71 | |
---|
| 72 | /** |
---|
| 73 | * |
---|
| 74 | */ |
---|
| 75 | private HtmlMonitor htmlMonitor; |
---|
| 76 | |
---|
| 77 | /** |
---|
| 78 | * |
---|
| 79 | */ |
---|
| 80 | @Before |
---|
| 81 | public void setUp() throws Exception { |
---|
| 82 | htmlMonitor = new HtmlMonitor(new String[] { LOG_FILE_DIR, Integer.toString(PORT) }); |
---|
| 83 | htmlMonitor.init(); |
---|
| 84 | htmlMonitor.start(); |
---|
| 85 | } |
---|
| 86 | |
---|
| 87 | /** |
---|
| 88 | * |
---|
| 89 | */ |
---|
| 90 | @After |
---|
| 91 | public void tearDown() throws Exception { |
---|
| 92 | if (htmlMonitor != null) { |
---|
| 93 | try { |
---|
| 94 | htmlMonitor.stop(); |
---|
| 95 | } |
---|
| 96 | finally { |
---|
| 97 | htmlMonitor = null; |
---|
| 98 | } |
---|
| 99 | } |
---|
| 100 | |
---|
| 101 | deleteFiles(new File(LOG_FILE_DIR)); |
---|
| 102 | } |
---|
| 103 | |
---|
| 104 | /** |
---|
| 105 | * |
---|
| 106 | */ |
---|
| 107 | @Test |
---|
| 108 | public void testOneSimpleMessage() throws Exception { |
---|
| 109 | String clientId = "123"; |
---|
| 110 | |
---|
| 111 | String message = |
---|
| 112 | "{" + |
---|
| 113 | " \"message\": {" + |
---|
| 114 | " \"clientInfos\": {" + |
---|
| 115 | " \"clientId\":\"" + clientId + "\"," + |
---|
| 116 | " \"userAgent\":\"Agent\"," + |
---|
| 117 | " \"title\":\"Title\"," + |
---|
| 118 | " \"url\":\"http://host/path\"" + |
---|
| 119 | " }," + |
---|
[1069] | 120 | " \"guiModel\": {" + |
---|
| 121 | " \"tagName\":\"html\"," + |
---|
| 122 | " \"index\":\"0\"," + |
---|
| 123 | " \"children\":" + |
---|
| 124 | " [ {" + |
---|
| 125 | " \"tagName\":\"head\"," + |
---|
| 126 | " \"index\":\"0\"," + |
---|
| 127 | " }," + |
---|
| 128 | " {" + |
---|
| 129 | " \"tagName\":\"body\"," + |
---|
| 130 | " \"htmlId\":\"gsr\"," + |
---|
| 131 | " }" + |
---|
| 132 | " ]" + |
---|
| 133 | " }," + |
---|
[864] | 134 | " \"events\":" + |
---|
| 135 | " [ {" + |
---|
| 136 | " \"time\":\"12345\"," + |
---|
[1069] | 137 | " \"path\":\"/html[0]/body(htmlId=gsr)\"," + |
---|
| 138 | " \"eventType\":\"onclick\"" + |
---|
| 139 | " \"coordinates\": [\"194\", \"7\"]" + |
---|
[864] | 140 | " }" + |
---|
| 141 | " ]" + |
---|
| 142 | " }" + |
---|
| 143 | "}"; |
---|
| 144 | |
---|
| 145 | sendMessageAndAssertResponse(message); |
---|
| 146 | |
---|
[1069] | 147 | htmlMonitor.stop(); |
---|
| 148 | htmlMonitor = null; |
---|
| 149 | |
---|
[1229] | 150 | File logFile = new File(LOG_FILE_DIR + File.separator + "host" + File.separator + |
---|
| 151 | clientId + File.separator + "htmlmonitor_" + clientId + "_000.log"); |
---|
[864] | 152 | |
---|
| 153 | assertTrue(logFile.exists()); |
---|
| 154 | |
---|
[1496] | 155 | HTMLLogParser parser = new HTMLLogParser(null); |
---|
[864] | 156 | |
---|
[1069] | 157 | parser.parseFile(logFile); |
---|
| 158 | |
---|
| 159 | // check the GUI model |
---|
| 160 | GUIModel guiModel = parser.getGuiModel(); |
---|
| 161 | assertNotNull(guiModel); |
---|
| 162 | |
---|
| 163 | List<IGUIElement> nodes = guiModel.getRootElements(); |
---|
| 164 | assertNotNull(nodes); |
---|
| 165 | assertEquals(1, nodes.size()); |
---|
| 166 | |
---|
| 167 | // get server node |
---|
| 168 | IGUIElement node = nodes.get(0); |
---|
| 169 | assertNotNull(node); |
---|
| 170 | assertTrue(node instanceof HTMLServer); |
---|
| 171 | assertEquals("HTML", node.getPlatform()); |
---|
| 172 | assertFalse(node.isUsed()); |
---|
| 173 | |
---|
| 174 | nodes = guiModel.getChildren(node); |
---|
| 175 | assertNotNull(nodes); |
---|
| 176 | assertEquals(1, nodes.size()); |
---|
| 177 | |
---|
| 178 | // get document node |
---|
| 179 | node = nodes.get(0); |
---|
| 180 | assertNotNull(node); |
---|
| 181 | assertTrue(node instanceof HTMLDocument); |
---|
| 182 | assertEquals("HTML", node.getPlatform()); |
---|
| 183 | assertFalse(node.isUsed()); |
---|
| 184 | |
---|
| 185 | nodes = guiModel.getChildren(node); |
---|
| 186 | assertNotNull(nodes); |
---|
| 187 | assertEquals(1, nodes.size()); |
---|
| 188 | |
---|
| 189 | // get html node |
---|
| 190 | node = nodes.get(0); |
---|
| 191 | assertNotNull(node); |
---|
| 192 | assertTrue(node instanceof HTMLPageElement); |
---|
| 193 | assertEquals("HTML", node.getPlatform()); |
---|
| 194 | assertFalse(node.isUsed()); |
---|
| 195 | |
---|
| 196 | nodes = guiModel.getChildren(node); |
---|
| 197 | assertNotNull(nodes); |
---|
| 198 | assertEquals(1, nodes.size()); // only one child as the head tag should have been ignored |
---|
| 199 | |
---|
| 200 | // get body node |
---|
| 201 | node = nodes.get(0); |
---|
| 202 | assertNotNull(node); |
---|
| 203 | assertTrue(node instanceof HTMLPageElement); |
---|
| 204 | assertEquals("HTML", node.getPlatform()); |
---|
| 205 | assertTrue(node.isUsed()); |
---|
| 206 | |
---|
| 207 | nodes = guiModel.getChildren(node); |
---|
| 208 | assertNotNull(nodes); |
---|
| 209 | assertEquals(0, nodes.size()); |
---|
| 210 | |
---|
| 211 | // check the sequences |
---|
| 212 | Collection<List<Event>> sequences = parser.getSequences(); |
---|
| 213 | |
---|
| 214 | assertNotNull(sequences); |
---|
| 215 | |
---|
| 216 | Iterator<List<Event>> iterator = sequences.iterator(); |
---|
| 217 | assertTrue(iterator.hasNext()); |
---|
| 218 | |
---|
| 219 | List<Event> sequence = iterator.next(); |
---|
| 220 | assertFalse(iterator.hasNext()); |
---|
| 221 | |
---|
| 222 | assertNotNull(sequence); |
---|
| 223 | assertEquals(1, sequence.size()); |
---|
| 224 | |
---|
| 225 | assertEvent(sequence.get(0), 12345, MouseClick.class, node, 194, 7); |
---|
[864] | 226 | } |
---|
| 227 | |
---|
| 228 | /** |
---|
| 229 | * |
---|
| 230 | */ |
---|
| 231 | @Test |
---|
| 232 | public void testSeveralMessagesInOneSession() throws Exception { |
---|
| 233 | String clientId = "123"; |
---|
| 234 | |
---|
| 235 | String message = |
---|
| 236 | "{" + |
---|
| 237 | " \"message\": {" + |
---|
| 238 | " \"clientInfos\": {" + |
---|
| 239 | " \"clientId\":\"" + clientId + "\"," + |
---|
| 240 | " \"userAgent\":\"Agent\"," + |
---|
| 241 | " \"title\":\"Title\"," + |
---|
| 242 | " \"url\":\"http://host/path\"" + |
---|
| 243 | " }," + |
---|
[1069] | 244 | " \"guiModel\": {" + |
---|
| 245 | " \"tagName\":\"html\"," + |
---|
| 246 | " \"index\":\"0\"," + |
---|
| 247 | " \"children\":" + |
---|
| 248 | " [ {" + |
---|
| 249 | " \"tagName\":\"head\"," + |
---|
| 250 | " \"index\":\"0\"," + |
---|
| 251 | " }," + |
---|
| 252 | " {" + |
---|
| 253 | " \"tagName\":\"body\"," + |
---|
| 254 | " \"htmlId\":\"gsr\"," + |
---|
| 255 | " \"children\":" + |
---|
| 256 | " [ {" + |
---|
| 257 | " \"tagName\":\"input_button\"," + |
---|
| 258 | " \"htmlId\":\"input1\"," + |
---|
| 259 | " }," + |
---|
| 260 | " {" + |
---|
| 261 | " \"tagName\":\"input_button\"," + |
---|
| 262 | " \"htmlId\":\"input2\"," + |
---|
| 263 | " }," + |
---|
| 264 | " {" + |
---|
| 265 | " \"tagName\":\"input_button\"," + |
---|
| 266 | " \"htmlId\":\"input3\"," + |
---|
| 267 | " }," + |
---|
| 268 | " {" + |
---|
| 269 | " \"tagName\":\"input_button\"," + |
---|
| 270 | " \"htmlId\":\"input4\"," + |
---|
| 271 | " }," + |
---|
| 272 | " {" + |
---|
| 273 | " \"tagName\":\"input_button\"," + |
---|
| 274 | " \"htmlId\":\"input5\"," + |
---|
| 275 | " }," + |
---|
| 276 | " {" + |
---|
| 277 | " \"tagName\":\"input_button\"," + |
---|
| 278 | " \"htmlId\":\"input6\"," + |
---|
| 279 | " }," + |
---|
| 280 | " {" + |
---|
| 281 | " \"tagName\":\"input_button\"," + |
---|
| 282 | " \"htmlId\":\"input7\"," + |
---|
| 283 | " }," + |
---|
| 284 | " {" + |
---|
| 285 | " \"tagName\":\"input_button\"," + |
---|
| 286 | " \"htmlId\":\"input8\"," + |
---|
| 287 | " }," + |
---|
| 288 | " {" + |
---|
| 289 | " \"tagName\":\"input_button\"," + |
---|
| 290 | " \"htmlId\":\"input9\"," + |
---|
| 291 | " }," + |
---|
| 292 | " {" + |
---|
| 293 | " \"tagName\":\"input_button\"," + |
---|
| 294 | " \"htmlId\":\"input10\"," + |
---|
| 295 | " }," + |
---|
| 296 | " ]" + |
---|
| 297 | " }" + |
---|
| 298 | " ]" + |
---|
| 299 | " }," + |
---|
[864] | 300 | " \"events\":" + |
---|
[1069] | 301 | " [ {" + |
---|
[864] | 302 | " \"time\":\"1\"," + |
---|
[1069] | 303 | " \"path\":\"/html[0]/body(htmlId=gsr)/input_button(htmlId=input1)\"," + |
---|
[864] | 304 | " \"eventType\":\"onclick\"," + |
---|
| 305 | " \"coordinates\": [\"194\", \"7\"]" + |
---|
| 306 | " }," + |
---|
| 307 | " {" + |
---|
| 308 | " \"time\":\"2\"," + |
---|
[1069] | 309 | " \"path\":\"/html[0]/body(htmlId=gsr)/input_button(htmlId=input2)\"," + |
---|
[864] | 310 | " \"eventType\":\"ondblclick\"," + |
---|
| 311 | " \"coordinates\": [\"194\", \"7\"]" + |
---|
| 312 | " }," + |
---|
| 313 | " {" + |
---|
| 314 | " \"time\":\"3\"," + |
---|
[1069] | 315 | " \"path\":\"/html[0]/body(htmlId=gsr)/input_button(htmlId=input3)\"," + |
---|
[864] | 316 | " \"eventType\":\"onfocus\"" + |
---|
| 317 | " }," + |
---|
| 318 | " {" + |
---|
| 319 | " \"time\":\"4\"," + |
---|
[1069] | 320 | " \"path\":\"/html[0]/body(htmlId=gsr)/input_button(htmlId=input4)\"," + |
---|
[864] | 321 | " \"eventType\":\"onclick\"," + |
---|
| 322 | " \"coordinates\": [\"125\", \"14\"]" + |
---|
| 323 | " }," + |
---|
| 324 | " {" + |
---|
| 325 | " \"time\":\"5\"," + |
---|
[1069] | 326 | " \"path\":\"/html[0]/body(htmlId=gsr)/input_button(htmlId=input5)\"," + |
---|
[864] | 327 | " \"eventType\":\"onfocus\"" + |
---|
| 328 | " }," + |
---|
| 329 | " {" + |
---|
| 330 | " \"time\":\"6\"," + |
---|
[1069] | 331 | " \"path\":\"/html[0]/body(htmlId=gsr)/input_button(htmlId=input6)\"," + |
---|
[864] | 332 | " \"eventType\":\"onfocus\"" + |
---|
| 333 | " }," + |
---|
| 334 | " {" + |
---|
| 335 | " \"time\":\"7\"," + |
---|
[1069] | 336 | " \"path\":\"/html[0]/body(htmlId=gsr)/input_button(htmlId=input7)\"," + |
---|
[864] | 337 | " \"eventType\":\"onfocus\"" + |
---|
| 338 | " }," + |
---|
| 339 | " {" + |
---|
| 340 | " \"time\":\"8\"," + |
---|
[1069] | 341 | " \"path\":\"/html[0]/body(htmlId=gsr)/input_button(htmlId=input8)\"," + |
---|
[864] | 342 | " \"eventType\":\"onclick\"," + |
---|
| 343 | " \"coordinates\": [\"255\", \"4\"]" + |
---|
| 344 | " }," + |
---|
| 345 | " {" + |
---|
| 346 | " \"time\":\"9\"," + |
---|
[1069] | 347 | " \"path\":\"/html[0]/body(htmlId=gsr)\"," + |
---|
[864] | 348 | " \"eventType\":\"onscroll\"," + |
---|
[943] | 349 | " \"scrollPosition\": [\"23\", \"567\"]" + |
---|
[864] | 350 | " }," + |
---|
| 351 | " {" + |
---|
| 352 | " \"time\":\"10\"," + |
---|
[1069] | 353 | " \"path\":\"/html[0]/body(htmlId=gsr)/input_button(htmlId=input10)\"," + |
---|
[864] | 354 | " \"eventType\":\"onclick\"," + |
---|
| 355 | " \"coordinates\": [\"516\", \"154\"]" + |
---|
| 356 | " }" + |
---|
| 357 | " ]" + |
---|
| 358 | " }" + |
---|
| 359 | "}"; |
---|
| 360 | |
---|
| 361 | sendMessageAndAssertResponse(message); |
---|
| 362 | |
---|
[1069] | 363 | htmlMonitor.stop(); |
---|
| 364 | htmlMonitor = null; |
---|
| 365 | |
---|
[1229] | 366 | File logFile = new File(LOG_FILE_DIR + File.separator + "host" + File.separator + |
---|
| 367 | clientId + File.separator + "htmlmonitor_" + clientId + "_000.log"); |
---|
[864] | 368 | |
---|
| 369 | assertTrue(logFile.exists()); |
---|
| 370 | |
---|
[1496] | 371 | HTMLLogParser parser = new HTMLLogParser(null); |
---|
[864] | 372 | |
---|
[1069] | 373 | parser.parseFile(logFile); |
---|
| 374 | |
---|
| 375 | // check the GUI model |
---|
| 376 | GUIModel guiModel = parser.getGuiModel(); |
---|
| 377 | assertNotNull(guiModel); |
---|
| 378 | |
---|
| 379 | List<IGUIElement> nodes = guiModel.getRootElements(); |
---|
| 380 | assertNotNull(nodes); |
---|
| 381 | assertEquals(1, nodes.size()); |
---|
| 382 | |
---|
| 383 | // get server node |
---|
| 384 | IGUIElement node = nodes.get(0); |
---|
| 385 | assertNotNull(node); |
---|
| 386 | assertTrue(node instanceof HTMLServer); |
---|
| 387 | assertEquals("HTML", node.getPlatform()); |
---|
| 388 | assertFalse(node.isUsed()); |
---|
| 389 | |
---|
| 390 | nodes = guiModel.getChildren(node); |
---|
| 391 | assertNotNull(nodes); |
---|
| 392 | assertEquals(1, nodes.size()); |
---|
| 393 | |
---|
| 394 | // get document node |
---|
| 395 | node = nodes.get(0); |
---|
| 396 | assertNotNull(node); |
---|
| 397 | assertTrue(node instanceof HTMLDocument); |
---|
| 398 | assertEquals("HTML", node.getPlatform()); |
---|
| 399 | assertFalse(node.isUsed()); |
---|
| 400 | |
---|
| 401 | nodes = guiModel.getChildren(node); |
---|
| 402 | assertNotNull(nodes); |
---|
| 403 | assertEquals(1, nodes.size()); |
---|
| 404 | |
---|
| 405 | // get html node |
---|
| 406 | node = nodes.get(0); |
---|
| 407 | assertNotNull(node); |
---|
| 408 | assertTrue(node instanceof HTMLPageElement); |
---|
| 409 | assertEquals("HTML", node.getPlatform()); |
---|
| 410 | assertFalse(node.isUsed()); |
---|
| 411 | |
---|
| 412 | nodes = guiModel.getChildren(node); |
---|
| 413 | assertNotNull(nodes); |
---|
| 414 | assertEquals(1, nodes.size()); // only one child as the head tag should have been ignored |
---|
| 415 | |
---|
| 416 | // get body node |
---|
| 417 | IGUIElement body = nodes.get(0); |
---|
| 418 | assertNotNull(body); |
---|
| 419 | assertTrue(body instanceof HTMLPageElement); |
---|
| 420 | assertEquals("HTML", body.getPlatform()); |
---|
| 421 | assertTrue(body.isUsed()); |
---|
| 422 | |
---|
| 423 | nodes = guiModel.getChildren(body); |
---|
| 424 | assertNotNull(nodes); |
---|
| 425 | |
---|
[1429] | 426 | // wait for all 10 GUI elements on the same page to be logged although only 9 are used |
---|
| 427 | assertEquals(10, nodes.size()); |
---|
[1315] | 428 | |
---|
[1911] | 429 | Map<String, IGUIElement> inputs = new HashMap<>(); |
---|
| 430 | |
---|
[1069] | 431 | // get input nodes |
---|
| 432 | for (int i = 0; i < nodes.size(); i++) { |
---|
| 433 | node = nodes.get(i); |
---|
| 434 | assertNotNull(node); |
---|
| 435 | assertTrue(node instanceof HTMLPageElement); |
---|
| 436 | assertEquals("HTML", node.getPlatform()); |
---|
[1429] | 437 | |
---|
| 438 | if (!"input9".equals(((HTMLPageElement) node).getHtmlId())) { |
---|
| 439 | assertTrue(node.isUsed()); |
---|
| 440 | } |
---|
| 441 | else { |
---|
| 442 | assertFalse(node.isUsed()); |
---|
| 443 | } |
---|
[1911] | 444 | |
---|
| 445 | inputs.put(((HTMLPageElement) node).getHtmlId(), node); |
---|
[1069] | 446 | |
---|
| 447 | assertNotNull(guiModel.getChildren(node)); |
---|
| 448 | assertEquals(0, guiModel.getChildren(node).size()); |
---|
| 449 | } |
---|
| 450 | |
---|
| 451 | // check the sequences |
---|
| 452 | Collection<List<Event>> sequences = parser.getSequences(); |
---|
| 453 | |
---|
| 454 | assertNotNull(sequences); |
---|
| 455 | |
---|
| 456 | Iterator<List<Event>> iterator = sequences.iterator(); |
---|
| 457 | assertTrue(iterator.hasNext()); |
---|
| 458 | |
---|
| 459 | List<Event> sequence = iterator.next(); |
---|
| 460 | assertFalse(iterator.hasNext()); |
---|
| 461 | |
---|
| 462 | assertNotNull(sequence); |
---|
| 463 | assertEquals(10, sequence.size()); |
---|
| 464 | |
---|
[1911] | 465 | assertEvent(sequence.get(0), 1, MouseClick.class, inputs.get("input1"), 194, 7); |
---|
| 466 | assertEvent(sequence.get(1), 2, MouseDoubleClick.class, inputs.get("input2"), 194, 7); |
---|
| 467 | assertEvent(sequence.get(2), 3, KeyboardFocusChange.class, inputs.get("input3"), 0, 0); |
---|
| 468 | assertEvent(sequence.get(3), 4, MouseClick.class, inputs.get("input4"), 125, 14); |
---|
| 469 | assertEvent(sequence.get(4), 5, KeyboardFocusChange.class, inputs.get("input5"), 0, 0); |
---|
| 470 | assertEvent(sequence.get(5), 6, KeyboardFocusChange.class, inputs.get("input6"), 0, 0); |
---|
| 471 | assertEvent(sequence.get(6), 7, KeyboardFocusChange.class, inputs.get("input7"), 0, 0); |
---|
| 472 | assertEvent(sequence.get(7), 8, MouseClick.class, inputs.get("input8"), 255, 4); |
---|
[1069] | 473 | assertEvent(sequence.get(8), 9, Scroll.class, body, 0, 0); |
---|
[1911] | 474 | assertEvent(sequence.get(9), 10, MouseClick.class, inputs.get("input10"), 516, 154); |
---|
[1069] | 475 | |
---|
[864] | 476 | } |
---|
| 477 | |
---|
| 478 | /** |
---|
| 479 | * |
---|
| 480 | */ |
---|
| 481 | @Test |
---|
| 482 | public void testSeveralSessions() throws Exception { |
---|
| 483 | String clientId = "123"; |
---|
| 484 | |
---|
| 485 | String message = |
---|
| 486 | "{" + |
---|
| 487 | " \"message\": {" + |
---|
| 488 | " \"clientInfos\": {" + |
---|
| 489 | " \"clientId\":\"" + clientId + "\"," + |
---|
| 490 | " \"userAgent\":\"Agent\"," + |
---|
| 491 | " \"title\":\"Title\"," + |
---|
| 492 | " \"url\":\"http://host/path\"" + |
---|
| 493 | " }," + |
---|
[1069] | 494 | " \"guiModel\": {" + |
---|
| 495 | " \"tagName\":\"html\"," + |
---|
| 496 | " \"index\":\"0\"," + |
---|
| 497 | " \"children\":" + |
---|
| 498 | " [ {" + |
---|
| 499 | " \"tagName\":\"head\"," + |
---|
| 500 | " \"index\":\"0\"," + |
---|
| 501 | " }," + |
---|
| 502 | " {" + |
---|
| 503 | " \"tagName\":\"body\"," + |
---|
| 504 | " \"htmlId\":\"gsr\"," + |
---|
| 505 | " }" + |
---|
| 506 | " ]" + |
---|
| 507 | " }," + |
---|
[864] | 508 | " \"events\":" + |
---|
| 509 | " [ {" + |
---|
| 510 | " \"time\":\"12345\"," + |
---|
[1069] | 511 | " \"path\":\"/html[0]/body(htmlId=gsr)\"," + |
---|
| 512 | " \"eventType\":\"onclick\"" + |
---|
| 513 | " \"coordinates\": [\"194\", \"7\"]" + |
---|
[864] | 514 | " }" + |
---|
| 515 | " ]" + |
---|
| 516 | " }" + |
---|
| 517 | "}"; |
---|
| 518 | |
---|
| 519 | sendMessageAndAssertResponse(message); |
---|
| 520 | |
---|
| 521 | int numberOfSessions = 10; |
---|
| 522 | for (int i = 0; i < numberOfSessions; i++) { |
---|
| 523 | htmlMonitor.stop(); |
---|
| 524 | htmlMonitor = new HtmlMonitor(new String[] { LOG_FILE_DIR, Integer.toString(PORT) }); |
---|
| 525 | htmlMonitor.init(); |
---|
| 526 | htmlMonitor.start(); |
---|
| 527 | sendMessageAndAssertResponse(message); |
---|
| 528 | } |
---|
| 529 | |
---|
[1069] | 530 | htmlMonitor.stop(); |
---|
| 531 | htmlMonitor = null; |
---|
| 532 | |
---|
[1496] | 533 | HTMLLogParser parser = new HTMLLogParser(null); |
---|
[1069] | 534 | |
---|
[864] | 535 | // assert 9 already rotated log files |
---|
[1069] | 536 | for (int i = 0; i < numberOfSessions; i++) { |
---|
[1229] | 537 | File logFile = new File(LOG_FILE_DIR + File.separator + "host" + File.separator + |
---|
| 538 | clientId + File.separator + "htmlmonitor_" + clientId + "_00" + |
---|
| 539 | i + ".log"); |
---|
[864] | 540 | |
---|
| 541 | assertTrue(logFile.exists()); |
---|
| 542 | |
---|
[1069] | 543 | parser.parseFile(logFile); |
---|
| 544 | } |
---|
| 545 | |
---|
| 546 | // check the GUI model |
---|
| 547 | GUIModel guiModel = parser.getGuiModel(); |
---|
| 548 | assertNotNull(guiModel); |
---|
| 549 | |
---|
| 550 | List<IGUIElement> nodes = guiModel.getRootElements(); |
---|
| 551 | assertNotNull(nodes); |
---|
| 552 | assertEquals(1, nodes.size()); |
---|
| 553 | |
---|
| 554 | // get server node |
---|
| 555 | IGUIElement node = nodes.get(0); |
---|
| 556 | assertNotNull(node); |
---|
| 557 | assertTrue(node instanceof HTMLServer); |
---|
| 558 | assertEquals("HTML", node.getPlatform()); |
---|
| 559 | assertFalse(node.isUsed()); |
---|
| 560 | |
---|
| 561 | nodes = guiModel.getChildren(node); |
---|
| 562 | assertNotNull(nodes); |
---|
| 563 | assertEquals(1, nodes.size()); |
---|
| 564 | |
---|
| 565 | // get document node |
---|
| 566 | node = nodes.get(0); |
---|
| 567 | assertNotNull(node); |
---|
| 568 | assertTrue(node instanceof HTMLDocument); |
---|
| 569 | assertEquals("HTML", node.getPlatform()); |
---|
| 570 | assertFalse(node.isUsed()); |
---|
| 571 | |
---|
| 572 | nodes = guiModel.getChildren(node); |
---|
| 573 | assertNotNull(nodes); |
---|
| 574 | assertEquals(1, nodes.size()); |
---|
| 575 | |
---|
| 576 | // get html node |
---|
| 577 | node = nodes.get(0); |
---|
| 578 | assertNotNull(node); |
---|
| 579 | assertTrue(node instanceof HTMLPageElement); |
---|
| 580 | assertEquals("HTML", node.getPlatform()); |
---|
| 581 | assertFalse(node.isUsed()); |
---|
| 582 | |
---|
| 583 | nodes = guiModel.getChildren(node); |
---|
| 584 | assertNotNull(nodes); |
---|
| 585 | assertEquals(1, nodes.size()); // only one child as the head tag should have been ignored |
---|
| 586 | |
---|
| 587 | // get body node |
---|
| 588 | node = nodes.get(0); |
---|
| 589 | assertNotNull(node); |
---|
| 590 | assertTrue(node instanceof HTMLPageElement); |
---|
| 591 | assertEquals("HTML", node.getPlatform()); |
---|
| 592 | assertTrue(node.isUsed()); |
---|
| 593 | |
---|
| 594 | nodes = guiModel.getChildren(node); |
---|
| 595 | assertNotNull(nodes); |
---|
| 596 | assertEquals(0, nodes.size()); |
---|
| 597 | |
---|
| 598 | // check the sequences |
---|
| 599 | Collection<List<Event>> sequences = parser.getSequences(); |
---|
| 600 | |
---|
| 601 | assertNotNull(sequences); |
---|
| 602 | assertEquals(numberOfSessions, sequences.size()); |
---|
| 603 | |
---|
| 604 | Iterator<List<Event>> iterator = sequences.iterator(); |
---|
| 605 | |
---|
| 606 | while (iterator.hasNext()) { |
---|
| 607 | List<Event> sequence = iterator.next(); |
---|
| 608 | |
---|
| 609 | assertNotNull(sequence); |
---|
| 610 | assertEquals(1, sequence.size()); |
---|
[864] | 611 | |
---|
[1069] | 612 | assertEvent(sequence.get(0), 12345, MouseClick.class, node, 194, 7); |
---|
[864] | 613 | } |
---|
| 614 | } |
---|
| 615 | |
---|
| 616 | /** |
---|
| 617 | * |
---|
| 618 | */ |
---|
[1229] | 619 | @Test |
---|
| 620 | public void testRevertOfOldFiles() throws Exception { |
---|
| 621 | String clientId = "123"; |
---|
| 622 | |
---|
| 623 | String message = |
---|
| 624 | "{" + |
---|
| 625 | " \"message\": {" + |
---|
| 626 | " \"clientInfos\": {" + |
---|
| 627 | " \"clientId\":\"" + clientId + "\"," + |
---|
| 628 | " \"userAgent\":\"Agent\"," + |
---|
| 629 | " \"title\":\"Title\"," + |
---|
| 630 | " \"url\":\"http://host/path\"" + |
---|
| 631 | " }," + |
---|
| 632 | " \"guiModel\": {" + |
---|
| 633 | " \"tagName\":\"html\"," + |
---|
| 634 | " \"index\":\"0\"," + |
---|
| 635 | " \"children\":" + |
---|
| 636 | " [ {" + |
---|
| 637 | " \"tagName\":\"head\"," + |
---|
| 638 | " \"index\":\"0\"," + |
---|
| 639 | " }," + |
---|
| 640 | " {" + |
---|
| 641 | " \"tagName\":\"body\"," + |
---|
| 642 | " \"htmlId\":\"gsr\"," + |
---|
| 643 | " }" + |
---|
| 644 | " ]" + |
---|
| 645 | " }," + |
---|
| 646 | " \"events\":" + |
---|
| 647 | " [ {" + |
---|
| 648 | " \"time\":\"12345\"," + |
---|
| 649 | " \"path\":\"/html[0]/body(htmlId=gsr)\"," + |
---|
| 650 | " \"eventType\":\"onclick\"" + |
---|
| 651 | " \"coordinates\": [\"194\", \"7\"]" + |
---|
| 652 | " }" + |
---|
| 653 | " ]" + |
---|
| 654 | " }" + |
---|
| 655 | "}"; |
---|
| 656 | |
---|
| 657 | sendMessageAndAssertResponse(message); |
---|
| 658 | |
---|
| 659 | htmlMonitor.stop(); |
---|
| 660 | htmlMonitor = null; |
---|
| 661 | |
---|
| 662 | File logFile1 = new File(LOG_FILE_DIR + File.separator + "host" + File.separator + |
---|
| 663 | clientId + File.separator + "htmlmonitor_" + clientId + "_000.log"); |
---|
| 664 | |
---|
| 665 | assertTrue(logFile1.exists()); |
---|
| 666 | |
---|
| 667 | // Move file to a the directory in which it would resist in the old structure and then |
---|
| 668 | // restart the server, resend the message and ensure two separate log files in the new |
---|
| 669 | // structure |
---|
| 670 | File oldLogFile = new File(LOG_FILE_DIR + File.separator + clientId + File.separator + |
---|
| 671 | "htmlmonitor_" + clientId + "_000.log"); |
---|
| 672 | |
---|
| 673 | assertTrue(oldLogFile.getParentFile().mkdirs()); |
---|
| 674 | assertTrue(logFile1.renameTo(oldLogFile)); |
---|
| 675 | |
---|
| 676 | htmlMonitor = new HtmlMonitor(new String[] { LOG_FILE_DIR, Integer.toString(PORT) }); |
---|
| 677 | htmlMonitor.init(); |
---|
| 678 | htmlMonitor.start(); |
---|
| 679 | |
---|
| 680 | sendMessageAndAssertResponse(message); |
---|
| 681 | |
---|
| 682 | htmlMonitor.stop(); |
---|
| 683 | htmlMonitor = null; |
---|
| 684 | |
---|
| 685 | assertTrue(logFile1.exists()); |
---|
| 686 | |
---|
| 687 | File logFile2 = new File(LOG_FILE_DIR + File.separator + "host" + File.separator + |
---|
| 688 | clientId + File.separator + "htmlmonitor_" + clientId + "_001.log"); |
---|
| 689 | |
---|
| 690 | assertTrue(logFile2.exists()); |
---|
| 691 | |
---|
[1496] | 692 | HTMLLogParser parser = new HTMLLogParser(null); |
---|
[1229] | 693 | |
---|
| 694 | parser.parseFile(logFile1); |
---|
| 695 | parser.parseFile(logFile2); |
---|
| 696 | |
---|
| 697 | // check the GUI model |
---|
| 698 | GUIModel guiModel = parser.getGuiModel(); |
---|
| 699 | assertNotNull(guiModel); |
---|
| 700 | |
---|
| 701 | List<IGUIElement> nodes = guiModel.getRootElements(); |
---|
| 702 | assertNotNull(nodes); |
---|
| 703 | assertEquals(1, nodes.size()); |
---|
| 704 | |
---|
| 705 | // get server node |
---|
| 706 | IGUIElement node = nodes.get(0); |
---|
| 707 | assertNotNull(node); |
---|
| 708 | assertTrue(node instanceof HTMLServer); |
---|
| 709 | assertEquals("HTML", node.getPlatform()); |
---|
| 710 | assertFalse(node.isUsed()); |
---|
| 711 | |
---|
| 712 | nodes = guiModel.getChildren(node); |
---|
| 713 | assertNotNull(nodes); |
---|
| 714 | assertEquals(1, nodes.size()); |
---|
| 715 | |
---|
| 716 | // get document node |
---|
| 717 | node = nodes.get(0); |
---|
| 718 | assertNotNull(node); |
---|
| 719 | assertTrue(node instanceof HTMLDocument); |
---|
| 720 | assertEquals("HTML", node.getPlatform()); |
---|
| 721 | assertFalse(node.isUsed()); |
---|
| 722 | |
---|
| 723 | nodes = guiModel.getChildren(node); |
---|
| 724 | assertNotNull(nodes); |
---|
| 725 | assertEquals(1, nodes.size()); |
---|
| 726 | |
---|
| 727 | // get html node |
---|
| 728 | node = nodes.get(0); |
---|
| 729 | assertNotNull(node); |
---|
| 730 | assertTrue(node instanceof HTMLPageElement); |
---|
| 731 | assertEquals("HTML", node.getPlatform()); |
---|
| 732 | assertFalse(node.isUsed()); |
---|
| 733 | |
---|
| 734 | nodes = guiModel.getChildren(node); |
---|
| 735 | assertNotNull(nodes); |
---|
| 736 | assertEquals(1, nodes.size()); // only one child as the head tag should have been ignored |
---|
| 737 | |
---|
| 738 | // get body node |
---|
| 739 | node = nodes.get(0); |
---|
| 740 | assertNotNull(node); |
---|
| 741 | assertTrue(node instanceof HTMLPageElement); |
---|
| 742 | assertEquals("HTML", node.getPlatform()); |
---|
| 743 | assertTrue(node.isUsed()); |
---|
| 744 | |
---|
| 745 | nodes = guiModel.getChildren(node); |
---|
| 746 | assertNotNull(nodes); |
---|
| 747 | assertEquals(0, nodes.size()); |
---|
| 748 | |
---|
| 749 | // check the sequences |
---|
| 750 | Collection<List<Event>> sequences = parser.getSequences(); |
---|
| 751 | |
---|
| 752 | assertNotNull(sequences); |
---|
| 753 | |
---|
| 754 | Iterator<List<Event>> iterator = sequences.iterator(); |
---|
| 755 | assertTrue(iterator.hasNext()); |
---|
| 756 | |
---|
| 757 | List<Event> sequence = iterator.next(); |
---|
| 758 | |
---|
| 759 | assertNotNull(sequence); |
---|
| 760 | assertEquals(1, sequence.size()); |
---|
| 761 | |
---|
| 762 | assertEvent(sequence.get(0), 12345, MouseClick.class, node, 194, 7); |
---|
| 763 | |
---|
| 764 | assertTrue(iterator.hasNext()); |
---|
| 765 | |
---|
| 766 | sequence = iterator.next(); |
---|
| 767 | assertFalse(iterator.hasNext()); |
---|
| 768 | |
---|
| 769 | assertNotNull(sequence); |
---|
| 770 | assertEquals(1, sequence.size()); |
---|
| 771 | |
---|
| 772 | assertEvent(sequence.get(0), 12345, MouseClick.class, node, 194, 7); |
---|
| 773 | } |
---|
| 774 | |
---|
| 775 | /** |
---|
| 776 | * |
---|
| 777 | */ |
---|
[864] | 778 | private void sendMessageAndAssertResponse(String message) throws Exception { |
---|
| 779 | DefaultHttpClient httpclient = new DefaultHttpClient(); |
---|
| 780 | HttpPost httpPost = new HttpPost("http://localhost:" + PORT + "/"); |
---|
| 781 | HttpEntity entity = new StringEntity(message, ContentType.APPLICATION_JSON); |
---|
| 782 | httpPost.setEntity(entity); |
---|
| 783 | |
---|
| 784 | try { |
---|
| 785 | HttpResponse response = httpclient.execute(httpPost); |
---|
| 786 | |
---|
| 787 | // the monitor always returns 200 without any additional information. The client must |
---|
| 788 | // never get more or less information. This is especially important for preventing |
---|
| 789 | // hackers from finding out more |
---|
| 790 | assertEquals(200, response.getStatusLine().getStatusCode()); |
---|
| 791 | assertTrue |
---|
[1245] | 792 | ((response.getEntity() == null) || (response.getEntity().getContentLength() == 0) || |
---|
| 793 | ((response.getEntity().getContentLength() == 1) && |
---|
| 794 | (response.getEntity().getContent().read() == ' '))); |
---|
[864] | 795 | } |
---|
| 796 | finally { |
---|
| 797 | httpPost.releaseConnection(); |
---|
| 798 | } |
---|
| 799 | } |
---|
| 800 | |
---|
| 801 | /** |
---|
| 802 | * |
---|
| 803 | */ |
---|
[1069] | 804 | private void assertEvent(Event event, |
---|
| 805 | int timestamp, |
---|
| 806 | Class<? extends IEventType> eventType, |
---|
| 807 | IGUIElement eventTarget, |
---|
| 808 | int xCoordinate, |
---|
| 809 | int yCoordinate) |
---|
[864] | 810 | { |
---|
[1069] | 811 | assertEquals(timestamp, event.getTimestamp()); |
---|
| 812 | assertTrue(eventType.isInstance(event.getType())); |
---|
| 813 | assertEquals(eventTarget, event.getTarget()); |
---|
[864] | 814 | |
---|
[1069] | 815 | if (event.getType() instanceof MouseButtonInteraction) { |
---|
| 816 | assertEquals(xCoordinate, ((MouseButtonInteraction) event.getType()).getX()); |
---|
| 817 | assertEquals(yCoordinate, ((MouseButtonInteraction) event.getType()).getY()); |
---|
[864] | 818 | } |
---|
| 819 | } |
---|
| 820 | |
---|
| 821 | /** |
---|
| 822 | * |
---|
| 823 | */ |
---|
| 824 | private void deleteFiles(File file) { |
---|
| 825 | if (file.exists()) { |
---|
| 826 | if (file.isDirectory()) { |
---|
| 827 | for (File child : file.listFiles()) { |
---|
| 828 | deleteFiles(child); |
---|
| 829 | } |
---|
| 830 | } |
---|
| 831 | |
---|
| 832 | try { |
---|
| 833 | file.delete(); |
---|
| 834 | } |
---|
| 835 | catch (Exception e) { |
---|
| 836 | // ignore and delete as much as possible |
---|
| 837 | } |
---|
| 838 | } |
---|
| 839 | } |
---|
| 840 | |
---|
| 841 | } |
---|