Javascript 中 Java 的 System.out.println() 的等价物是什么?
我正在为 Javascript 代码编写一些测试,当遇到错误时,我需要在编译过程中转储一些消息。
Javascript 中是否有与 Java 的 System.out.println() 等效的函数?
PS:我还需要在实现测试时转储调试语句。
更新
我在包含所有合并测试的文件上使用maven插件:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.1</version>
<executions>
<execution>
<phase>test</phase>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>org.mozilla.javascript.tools.shell.Main</mainClass>
<arguments>
<argument>-opt</argument>
<argument>-1</argument>
<argument>${basedir}/src/main/webapp/html/js/test/test.js</argument>
</arguments>
</configuration>
</plugin>
更新II
我尝试了console.log("...")
,但我得到:
js: "src/main/webapp/html/js/concat/tests_all.js", line 147:
uncaught JavaScript runtime exception: ReferenceError: "console" is not defined
我正在测试的代码是一组函数(就像在库中一样)。我正在使用 QUnit。
I am writing some tests for Javascript code and I need to dump some messages during the compile process when errors are encountered.
Is there any equivalent to Java's System.out.println()
in Javascript?
P.S.: I also need to dump debug statements while implementing tests.
UPDATE
I am using a maven plugin on a file containing all merged tests:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.1</version>
<executions>
<execution>
<phase>test</phase>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>org.mozilla.javascript.tools.shell.Main</mainClass>
<arguments>
<argument>-opt</argument>
<argument>-1</argument>
<argument>${basedir}/src/main/webapp/html/js/test/test.js</argument>
</arguments>
</configuration>
</plugin>
UPDATE II
I tried console.log("...")
, but I get:
js: "src/main/webapp/html/js/concat/tests_all.js", line 147:
uncaught JavaScript runtime exception: ReferenceError: "console" is not defined
The code I am testing is a set of functions (like in a library). I am using QUnit.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
如果浏览器有支持控制台,则本质上是
console.log("Put a message here.")
。另一种典型的调试方法是使用警报,
alert("Put a message here.")
RE: Update II
这似乎是有道理的,您正在尝试自动化 QUnit 测试,根据我在 QUnit 上读到的内容,这是一个浏览器内单元测试套件/库。 QUnit 期望在浏览器中运行,因此期望浏览器能够识别您正在调用的所有 JavaScript 函数。
根据您的 Maven 配置,您似乎正在使用 Rhino 在命令行/终端执行 Javascript。这不适用于测试浏览器细节,您可能需要研究 Selenium。如果您不关心在浏览器中测试 JavaScript,而只是在命令行级别测试 JavaScript(出于我不熟悉的原因),那么 Rhino 似乎可以识别
print()
计算表达式并将其打印出来的方法。查看此文档。您可能会对这些链接感兴趣。
QUnit 和自动化测试
使用 QUnit 进行 JavaScript 单元测试
Essentially
console.log("Put a message here.")
if the browser has a supporting console.Another typical debugging method is using alerts,
alert("Put a message here.")
RE: Update II
This seems to make sense, you are trying to automate QUnit tests, from what I have read on QUnit this is an in-browser unit testing suite/library. QUnit expects to run in a browser and therefore expects the browser to recognize all of the JavaScript functions you are calling.
Based on your Maven configuration it appears you are using Rhino to execute your Javascript at the command line/terminal. This is not going to work for testing browser specifics, you would likely need to look into Selenium for this. If you do not care about testing your JavaScript in a browser but are only testing JavaScript at a command line level (for reason I would not be familiar with) it appears that Rhino recognizes a
print()
method for evaluating expressions and printing them out. Checkout this documentation.These links might be of interest to you.
QUnit and Automated Testing
JavaScript Unit Tests with QUnit
我找到了一个解决方案:
I found a solution:
我正在使用 Chrome,print() 将文本打印在纸上。这对我有用:
I'm using Chrome and print() literally prints the text on paper. This is what works for me:
console.log()
。Chrome、Safari 和 IE 8+ 带有内置控制台(作为更大的开发工具集的一部分)。如果您使用的是 Firefox,请访问 getfirebug.com。
console.log()
.Chrome, Safari, and IE 8+ come with built-in consoles (as part of a larger set of development tools). If you're using Firefox, getfirebug.com.
至少,除非您在浏览器中使用某种“开发人员”工具,例如 Firefox 中的 Firebug 或 Safari 中的开发人员工具,否则没有这样的工具。然后你通常可以使用console.log。
如果我在 iOS 设备上做某事,我可能会添加一个
然后登录到它。
There isn't one, at least, not unless you are using a "developer" tool of some kind in your browser, e.g. Firebug in Firefox or the Developer tools in Safari. Then you can usually use
console.log
.If I'm doing something in, say, an iOS device, I might add a
<div id="debug" />
and then log to it.您始终可以简单地在函数中的任何位置添加alert() 提示。对于了解函数是否被调用、函数是否完成或函数在哪里失败特别有用。
你明白了。
You can always simply add an alert() prompt anywhere in a function. Especially useful for knowing if a function was called, if a function completed or where a function fails.
You get the idea.
我也正想问同样的问题。但是根据我从 codeacademy.com 了解到的信息,下面的代码足以显示输出或文本吗?
I'm also about to ask the same question. But from what I've learned from codeacademy.com below code is enough to display the output or text?
在java中
System.out.println()
打印一些东西到控制台。在 javascript 中,可以使用console.log() 来实现相同的效果。您需要按
F12
键查看浏览器控制台,打开开发人员工具,然后切换到console
选项卡。In java
System.out.println()
prints something to console. In javascript same can be achieved usingconsole.log()
.You need to view browser console by pressing
F12
key which opens developer tool and then switch toconsole
tab.