1 var initialize_ConsoleTest = function() {
3 InspectorTest
.preloadModule("source_frame");
4 InspectorTest
.preloadPanel("console");
6 InspectorTest
.evaluateInConsole = function(code
, callback
)
8 callback
= InspectorTest
.safeWrap(callback
);
10 var consoleView
= WebInspector
.ConsolePanel
._view();
11 consoleView
.visible
= true;
12 consoleView
._prompt
.setText(code
);
13 var event
= document
.createEvent("KeyboardEvent");
14 event
.initKeyboardEvent("keydown", true, true, null, "Enter", "");
15 consoleView
._prompt
.proxyElementForTests().dispatchEvent(event
);
16 InspectorTest
.addConsoleViewSniffer(function(commandResult
) {
17 callback(commandResult
.toMessageElement().deepTextContent());
21 InspectorTest
.addConsoleViewSniffer = function(override
, opt_sticky
)
23 var sniffer = function (viewMessage
) {
24 override(viewMessage
);
27 InspectorTest
.addSniffer(WebInspector
.ConsoleView
.prototype, "_consoleMessageAddedForTest", sniffer
, opt_sticky
);
30 InspectorTest
.evaluateInConsoleAndDump = function(code
, callback
)
32 callback
= InspectorTest
.safeWrap(callback
);
34 function mycallback(text
)
36 InspectorTest
.addResult(code
+ " = " + text
.replace(/\bVM\d+/g, "VM").replace(/InjectedScript\.\w+ @ VM:\d+/g, ""));
39 InspectorTest
.evaluateInConsole(code
, mycallback
);
42 InspectorTest
.prepareConsoleMessageText = function(messageElement
, consoleMessage
)
44 var messageText
= messageElement
.deepTextContent().replace(/\u200b/g, "");
45 // Replace scriptIds with generic scriptId string to avoid flakiness.
46 messageText
= messageText
.replace(/VM\d+/g, "VM");
47 // Strip out InjectedScript line numbers from stack traces to avoid rebaselining each time InjectedScriptSource is edited.
48 messageText
= messageText
.replace(/InjectedScript\.\w+ @ VM:\d+/g, "");
49 // Strip out InjectedScript line numbers from console message anchor.
50 var functionName
= consoleMessage
&& consoleMessage
.stackTrace
&& consoleMessage
.stackTrace
[0] && consoleMessage
.stackTrace
[0].functionName
|| "";
51 if (functionName
.indexOf("InjectedScript") !== -1)
52 messageText
= messageText
.replace(/\bVM:\d+/, ""); // Only first replace.
53 if (messageText
.startsWith("Navigated to")) {
54 var fileName
= messageText
.split(" ").pop().split("/").pop();
55 messageText
= "Navigated to " + fileName
;
57 // The message might be extremely long in case of dumping stack overflow message.
58 messageText
= messageText
.substring(0, 1024);
62 InspectorTest
.disableConsoleViewport = function()
64 InspectorTest
.fixConsoleViewportDimensions(600, 2000);
67 InspectorTest
.fixConsoleViewportDimensions = function(width
, height
)
69 var viewport
= WebInspector
.ConsolePanel
._view()._viewport
;
70 viewport
.element
.style
.width
= width
+ "px";
71 viewport
.element
.style
.height
= height
+ "px";
72 viewport
.element
.style
.position
= "absolute";
73 viewport
.invalidate();
76 InspectorTest
.consoleMessagesCount = function()
78 var consoleView
= WebInspector
.ConsolePanel
._view();
79 return consoleView
._consoleMessages
.length
;
82 InspectorTest
.dumpConsoleMessages = function(printOriginatingCommand
, dumpClassNames
, formatter
)
84 InspectorTest
.addResults(InspectorTest
.dumpConsoleMessagesIntoArray(printOriginatingCommand
, dumpClassNames
, formatter
));
87 InspectorTest
.dumpConsoleMessagesIntoArray = function(printOriginatingCommand
, dumpClassNames
, formatter
)
89 formatter
= formatter
|| InspectorTest
.prepareConsoleMessageText
;
91 InspectorTest
.disableConsoleViewport();
92 var consoleView
= WebInspector
.ConsolePanel
._view();
93 if (consoleView
._needsFullUpdate
)
94 consoleView
._updateMessageList();
95 var viewMessages
= consoleView
._visibleViewMessages
;
96 for (var i
= 0; i
< viewMessages
.length
; ++i
) {
97 var uiMessage
= viewMessages
[i
];
98 var message
= uiMessage
.consoleMessage();
99 var element
= uiMessage
.contentElement();
101 if (dumpClassNames
) {
103 for (var node
= element
.firstChild
; node
; node
= node
.traverseNextNode(element
)) {
104 if (node
.nodeType
=== Node
.ELEMENT_NODE
&& node
.className
)
105 classNames
.push(node
.className
.replace("platform-linux", "platform-*").replace("platform-mac", "platform-*").replace("platform-windows", "platform-*"));
109 if (InspectorTest
.dumpConsoleTableMessage(uiMessage
, false, result
)) {
111 result
.push(classNames
.join(" > "));
113 var messageText
= formatter(element
, message
);
114 result
.push(messageText
+ (dumpClassNames
? " " + classNames
.join(" > ") : ""));
117 if (printOriginatingCommand
&& uiMessage
.consoleMessage().originatingMessage())
118 result
.push("Originating from: " + uiMessage
.consoleMessage().originatingMessage().messageText
);
123 InspectorTest
.formatterIgnoreStackFrameUrls = function(messageFormatter
, node
)
125 function isNotEmptyLine(string
)
127 return string
.trim().length
> 0;
130 function ignoreStackFrameAndMutableData(string
)
132 var buffer
= string
.replace(/\u200b/g, "");
133 buffer
= buffer
.replace(/VM\d+/g, "VM");
134 buffer
= buffer
.replace(/InjectedScript\.\w+ @ VM:\d+/g, "");
135 return buffer
.replace(/^\s+at [^\]]+(]?)$/, "$1");
138 messageFormatter
= messageFormatter
|| InspectorTest
.textContentWithLineBreaks
;
139 var buffer
= messageFormatter(node
);
140 return buffer
.split("\n").map(ignoreStackFrameAndMutableData
).filter(isNotEmptyLine
).join("\n");
143 InspectorTest
.dumpConsoleMessagesIgnoreErrorStackFrames = function(printOriginatingCommand
, dumpClassNames
, messageFormatter
)
145 InspectorTest
.addResults(InspectorTest
.dumpConsoleMessagesIntoArray(printOriginatingCommand
, dumpClassNames
, InspectorTest
.formatterIgnoreStackFrameUrls
.bind(this, messageFormatter
)));
148 InspectorTest
.dumpConsoleTableMessage = function(viewMessage
, forceInvalidate
, results
)
151 WebInspector
.ConsolePanel
._view()._viewport
.invalidate();
152 var table
= viewMessage
.contentElement();
153 var headers
= table
.querySelectorAll("th > div:first-child");
158 for (var i
= 0; i
< headers
.length
; i
++)
159 headerLine
+= headers
[i
].textContent
+ " | ";
161 addResult("HEADER " + headerLine
);
163 var rows
= table
.querySelectorAll(".data-container tr");
165 for (var i
= 0; i
< rows
.length
; i
++) {
168 var items
= row
.querySelectorAll("td > span");
169 for (var j
= 0; j
< items
.length
; j
++)
170 rowLine
+= items
[j
].textContent
+ " | ";
173 addResult("ROW " + rowLine
);
176 function addResult(x
)
181 InspectorTest
.addResult(x
);
187 InspectorTest
.dumpConsoleMessagesWithStyles = function(sortMessages
)
190 var messageViews
= WebInspector
.ConsolePanel
._view()._visibleViewMessages
;
191 for (var i
= 0; i
< messageViews
.length
; ++i
) {
192 var element
= messageViews
[i
].contentElement();
193 var messageText
= InspectorTest
.prepareConsoleMessageText(element
)
194 InspectorTest
.addResult(messageText
);
195 var spans
= element
.querySelectorAll(".console-message-text > span *");
196 for (var j
= 0; j
< spans
.length
; ++j
)
197 InspectorTest
.addResult("Styled text #" + j
+ ": " + (spans
[j
].style
.cssText
|| "NO STYLES DEFINED"));
201 InspectorTest
.dumpConsoleMessagesWithClasses = function(sortMessages
) {
203 var messageViews
= WebInspector
.ConsolePanel
._view()._visibleViewMessages
;
204 for (var i
= 0; i
< messageViews
.length
; ++i
) {
205 var element
= messageViews
[i
].contentElement();
206 var messageText
= InspectorTest
.prepareConsoleMessageText(element
);
207 result
.push(messageText
+ " " + messageViews
[i
].toMessageElement().getAttribute("class") + " > " + element
.getAttribute("class"));
211 for (var i
= 0; i
< result
.length
; ++i
)
212 InspectorTest
.addResult(result
[i
]);
215 InspectorTest
.dumpConsoleClassesBrief = function()
217 var messageViews
= WebInspector
.ConsolePanel
._view()._visibleViewMessages
;
218 for (var i
= 0; i
< messageViews
.length
; ++i
)
219 InspectorTest
.addResult(messageViews
[i
].toMessageElement().className
);
222 InspectorTest
.expandConsoleMessages = function(callback
, deepFilter
, sectionFilter
)
224 var messageViews
= WebInspector
.ConsolePanel
._view()._visibleViewMessages
;
226 // Initiate round-trips to fetch necessary data for further rendering.
227 for (var i
= 0; i
< messageViews
.length
; ++i
)
228 messageViews
[i
].contentElement();
230 InspectorTest
.runAfterPendingDispatches(expandTreeElements
);
232 function expandTreeElements()
234 for (var i
= 0; i
< messageViews
.length
; ++i
) {
235 var element
= messageViews
[i
].contentElement();
236 for (var node
= element
; node
; node
= node
.traverseNextNode(element
)) {
237 if (node
.treeElementForTest
)
238 node
.treeElementForTest
.expand();
241 if (sectionFilter
&& !sectionFilter(node
._section
))
243 node
._section
.expand();
247 var treeElements
= node
._section
.rootElement().children();
248 for (var j
= 0; j
< treeElements
.length
; ++j
) {
249 for (var treeElement
= treeElements
[j
]; treeElement
; treeElement
= treeElement
.traverseNextTreeElement(true, null, true)) {
250 if (deepFilter(treeElement
))
251 treeElement
.expand();
256 InspectorTest
.runAfterPendingDispatches(callback
);
260 InspectorTest
.expandConsoleMessagesErrorParameters = function(callback
)
262 var messageViews
= WebInspector
.ConsolePanel
._view()._visibleViewMessages
;
263 // Initiate round-trips to fetch necessary data for further rendering.
264 for (var i
= 0; i
< messageViews
.length
; ++i
)
265 messageViews
[i
].contentElement();
266 InspectorTest
.runAfterPendingDispatches(expandErrorParameters
);
267 function expandErrorParameters()
269 for (var i
= 0; i
< messageViews
.length
; ++i
) {
270 var element
= messageViews
[i
].contentElement();
271 var spans
= element
.querySelectorAll("span.object-value-error");
272 for (var j
= 0; j
< spans
.length
; ++j
) {
273 var links
= spans
[j
].querySelectorAll("a");
274 for (var k
= 0; k
< links
.length
; ++k
) {
276 if (link
&& link
._showDetailedForTest
)
277 link
._showDetailedForTest();
285 InspectorTest
.waitForRemoteObjectsConsoleMessages = function(callback
)
287 var messages
= WebInspector
.ConsolePanel
._view()._visibleViewMessages
;
288 for (var i
= 0; i
< messages
.length
; ++i
)
289 messages
[i
].toMessageElement();
290 InspectorTest
.runAfterPendingDispatches(callback
);
293 InspectorTest
.checkConsoleMessagesDontHaveParameters = function()
295 var messageViews
= WebInspector
.ConsolePanel
._view()._visibleViewMessages
;
296 for (var i
= 0; i
< messageViews
.length
; ++i
) {
297 var m
= messageViews
[i
].consoleMessage();
298 InspectorTest
.addResult("Message[" + i
+ "]:");
299 InspectorTest
.addResult("Message: " + WebInspector
.displayNameForURL(m
.url
) + ":" + m
.line
+ " " + m
.message
);
300 if ("_parameters" in m
) {
302 InspectorTest
.addResult("FAILED: message parameters list is not empty: " + m
.parameters
);
304 InspectorTest
.addResult("SUCCESS: message parameters list is empty. ");
306 InspectorTest
.addResult("FAILED: didn't find _parameters field in the message.");
311 InspectorTest
.waitUntilMessageReceived = function(callback
)
313 InspectorTest
.addSniffer(InspectorTest
.consoleModel
, "addMessage", callback
, false);
316 InspectorTest
.waitUntilNthMessageReceived = function(count
, callback
)
321 InspectorTest
.safeWrap(callback
)();
323 InspectorTest
.addSniffer(InspectorTest
.consoleModel
, "addMessage", override
, false);
325 InspectorTest
.addSniffer(InspectorTest
.consoleModel
, "addMessage", override
, false);
328 InspectorTest
.changeExecutionContext = function(namePrefix
)
330 var selector
= WebInspector
.ConsolePanel
._view()._executionContextModel
._selectElement
;
331 var option
= selector
.firstChild
;
333 if (option
.textContent
&& option
.textContent
.startsWith(namePrefix
))
335 option
= option
.nextSibling
;
338 InspectorTest
.addResult("FAILED: context with prefix: " + namePrefix
+ " not found in the context list");
341 option
.selected
= true;
342 WebInspector
.ConsolePanel
._view()._executionContextModel
._executionContextChanged();
345 InspectorTest
.waitForConsoleMessages = function(expectedCount
, callback
)
347 var consoleView
= WebInspector
.ConsolePanel
._view();
350 function checkAndReturn()
352 if (consoleView
._visibleViewMessages
.length
=== expectedCount
) {
353 InspectorTest
.addResult("Message count: " + expectedCount
);
356 InspectorTest
.addSniffer(consoleView
, "_messageAppendedForTests", checkAndReturn
);