| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 /** | |
| 6 * @fileoverview JS utilities automatically injected by GTalk PyAuto tests. | |
| 7 */ | |
| 8 | |
| 9 /** | |
| 10 * Key codes to use with KeyboardEvent. | |
| 11 */ | |
| 12 $KEYS = { | |
| 13 ENTER: 13, | |
| 14 ESC: 27 | |
| 15 }; | |
| 16 | |
| 17 /** | |
| 18 * The first Chrome extension view with a URL containing the query. | |
| 19 */ | |
| 20 $VIEW = function(query) { | |
| 21 var views = chrome.extension.getViews(); | |
| 22 for (var i = 0; i < views.length; i++) { | |
| 23 var url = views[i].location.href; | |
| 24 if (url && url.indexOf(query) >= 0) { | |
| 25 return views[i]; | |
| 26 } | |
| 27 } | |
| 28 return null; | |
| 29 }; | |
| 30 | |
| 31 /** | |
| 32 * The body element of the given window. | |
| 33 */ | |
| 34 $BODY = function(opt_window) { | |
| 35 return (opt_window || window).document.body; | |
| 36 }; | |
| 37 | |
| 38 /** | |
| 39 * Find the ancestor of the given element with a particular tag name. | |
| 40 */ | |
| 41 $FindByTagName = function(element, tag, index) { | |
| 42 var tagElements = element.getElementsByTagName(tag); | |
| 43 if (index < tagElements.length) { | |
| 44 return tagElements[index]; | |
| 45 } | |
| 46 return null; | |
| 47 }; | |
| 48 | |
| 49 /** | |
| 50 * Find the first ancestor of the given element containing the given text. | |
| 51 */ | |
| 52 $FindByText = function(baseElement, text) { | |
| 53 var allElements = baseElement.getElementsByTagName('*'); | |
| 54 for (var i = 0; i < allElements.length; i++) { | |
| 55 var element = allElements[i]; | |
| 56 if (element.innerText && element.innerText.indexOf(text) >= 0) { | |
| 57 var child = $FindByText(element, text); | |
| 58 return child != null ? child : element; | |
| 59 } | |
| 60 } | |
| 61 return null; | |
| 62 }; | |
| 63 | |
| 64 /** | |
| 65 * Simulate a click on a given element. | |
| 66 */ | |
| 67 $Click = function(element) { | |
| 68 var mouseEvent = element.ownerDocument.createEvent('MouseEvent'); | |
| 69 mouseEvent.initMouseEvent('click', true, true, window, | |
| 70 1, 0, 0, 0, 0, false, false, false, false, 0, null); | |
| 71 element.dispatchEvent(mouseEvent); | |
| 72 return true; | |
| 73 }; | |
| 74 | |
| 75 /** | |
| 76 * Simulate typing text on a given element. | |
| 77 */ | |
| 78 $Type = function(element, text) { | |
| 79 var keyEvent = element.ownerDocument.createEvent('TextEvent'); | |
| 80 keyEvent.initTextEvent('textInput', true, true, window, text); | |
| 81 element.dispatchEvent(keyEvent); | |
| 82 return true; | |
| 83 }; | |
| 84 | |
| 85 /** | |
| 86 * Simulate pressing a certain key on a given element. | |
| 87 */ | |
| 88 $Press = function(baseElement, keycode, opt_ctrlKey, opt_shiftKey, | |
| 89 opt_altKey, opt_metaKey) { | |
| 90 var sendKeyEvent = function(element, eventType) { | |
| 91 // Unfortuantely, using the typical KeyboardEvent and initKeyboardEvent | |
| 92 // fails in Chrome due to a webkit bug: | |
| 93 // https://bugs.webkit.org/show_bug.cgi?id=16735 | |
| 94 // We employ a workaround of synthesizing a raw 'Event' suggested here: | |
| 95 // http://code.google.com/p/selenium/issues/detail?id=567 | |
| 96 var keyEvent = element.ownerDocument.createEvent('Events'); | |
| 97 keyEvent.ctrlKey = Boolean(opt_ctrlKey); | |
| 98 keyEvent.shiftKey = Boolean(opt_shiftKey); | |
| 99 keyEvent.altKey = Boolean(opt_altKey); | |
| 100 keyEvent.metaKey = Boolean(opt_metaKey); | |
| 101 keyEvent.initEvent(eventType, true, true); | |
| 102 keyEvent.keyCode = keycode; | |
| 103 element.dispatchEvent(keyEvent); | |
| 104 } | |
| 105 sendKeyEvent(baseElement, 'keydown'); | |
| 106 sendKeyEvent(baseElement, 'keypress'); | |
| 107 sendKeyEvent(baseElement, 'keyup'); | |
| 108 return true; | |
| 109 }; | |
| OLD | NEW |