browser_webconsole_clear_method.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
  2. /* Any copyright is dedicated to the Public Domain.
  3. * http://creativecommons.org/publicdomain/zero/1.0/ */
  4. // Check that calls to console.clear from a script delete the messages
  5. // previously logged.
  6. "use strict";
  7. add_task(function* () {
  8. const TEST_URI = "http://example.com/browser/devtools/client/webconsole/" +
  9. "test/test-console-clear.html";
  10. yield loadTab(TEST_URI);
  11. let hud = yield openConsole();
  12. ok(hud, "Web Console opened");
  13. info("Check the console.clear() done on page load has been processed.");
  14. yield waitForLog("Console was cleared", hud);
  15. ok(hud.outputNode.textContent.includes("Console was cleared"),
  16. "console.clear() message is displayed");
  17. ok(!hud.outputNode.textContent.includes("log1"), "log1 not displayed");
  18. ok(!hud.outputNode.textContent.includes("log2"), "log2 not displayed");
  19. info("Logging two messages log3, log4");
  20. ContentTask.spawn(gBrowser.selectedBrowser, {}, function* () {
  21. content.wrappedJSObject.console.log("log3");
  22. content.wrappedJSObject.console.log("log4");
  23. });
  24. yield waitForLog("log3", hud);
  25. yield waitForLog("log4", hud);
  26. ok(hud.outputNode.textContent.includes("Console was cleared"),
  27. "console.clear() message is still displayed");
  28. ok(hud.outputNode.textContent.includes("log3"), "log3 is displayed");
  29. ok(hud.outputNode.textContent.includes("log4"), "log4 is displayed");
  30. info("Open the variables view sidebar for 'objFromPage'");
  31. yield openSidebar("objFromPage", { a: 1 }, hud);
  32. let sidebarClosed = hud.jsterm.once("sidebar-closed");
  33. info("Call console.clear from the page");
  34. ContentTask.spawn(gBrowser.selectedBrowser, {}, function* () {
  35. content.wrappedJSObject.console.clear();
  36. });
  37. // Cannot wait for "Console was cleared" here because such a message is
  38. // already present and would yield immediately.
  39. info("Wait for variables view sidebar to be closed after console.clear()");
  40. yield sidebarClosed;
  41. ok(!hud.outputNode.textContent.includes("log3"), "log3 not displayed");
  42. ok(!hud.outputNode.textContent.includes("log4"), "log4 not displayed");
  43. ok(hud.outputNode.textContent.includes("Console was cleared"),
  44. "console.clear() message is still displayed");
  45. is(hud.outputNode.textContent.split("Console was cleared").length, 2,
  46. "console.clear() message is only displayed once");
  47. info("Logging one messages log5");
  48. ContentTask.spawn(gBrowser.selectedBrowser, {}, function* () {
  49. content.wrappedJSObject.console.log("log5");
  50. });
  51. yield waitForLog("log5", hud);
  52. info("Close and reopen the webconsole.");
  53. yield closeConsole(gBrowser.selectedTab);
  54. hud = yield openConsole();
  55. yield waitForLog("Console was cleared", hud);
  56. ok(hud.outputNode.textContent.includes("Console was cleared"),
  57. "console.clear() message is still displayed");
  58. ok(!hud.outputNode.textContent.includes("log1"), "log1 not displayed");
  59. ok(!hud.outputNode.textContent.includes("log2"), "log1 not displayed");
  60. ok(!hud.outputNode.textContent.includes("log3"), "log3 not displayed");
  61. ok(!hud.outputNode.textContent.includes("log4"), "log4 not displayed");
  62. ok(hud.outputNode.textContent.includes("log5"), "log5 still displayed");
  63. });
  64. /**
  65. * Wait for a single message to be logged in the provided webconsole instance
  66. * with the category CATEGORY_WEBDEV and the SEVERITY_LOG severity.
  67. *
  68. * @param {String} message
  69. * The expected messaged.
  70. * @param {WebConsole} webconsole
  71. * WebConsole instance in which the message should be logged.
  72. */
  73. function* waitForLog(message, webconsole, options) {
  74. yield waitForMessages({
  75. webconsole: webconsole,
  76. messages: [{
  77. text: message,
  78. category: CATEGORY_WEBDEV,
  79. severity: SEVERITY_LOG,
  80. }],
  81. });
  82. }
  83. /**
  84. * Open the variables view sidebar for the object with the provided name objName
  85. * and wait for the expected object is displayed in the variables view.
  86. *
  87. * @param {String} objName
  88. * The name of the object to open in the sidebar.
  89. * @param {Object} expectedObj
  90. * The properties that should be displayed in the variables view.
  91. * @param {WebConsole} webconsole
  92. * WebConsole instance in which the message should be logged.
  93. *
  94. */
  95. function* openSidebar(objName, expectedObj, webconsole) {
  96. let msg = yield webconsole.jsterm.execute(objName);
  97. ok(msg, "output message found");
  98. let anchor = msg.querySelector("a");
  99. let body = msg.querySelector(".message-body");
  100. ok(anchor, "object anchor");
  101. ok(body, "message body");
  102. yield EventUtils.synthesizeMouse(anchor, 2, 2, {}, webconsole.iframeWindow);
  103. let vviewVar = yield webconsole.jsterm.once("variablesview-fetched");
  104. let vview = vviewVar._variablesView;
  105. ok(vview, "variables view object exists");
  106. yield findVariableViewProperties(vviewVar, [
  107. expectedObj,
  108. ], { webconsole: webconsole });
  109. }