如何比较只有输出部分在html和javascript
How to compare only output part in html and javascript?
我计划创建一个用于JavaScript测试的应用程序。在这里我将给出一个创建程序的问题,例如,在警告框中显示文本"HTML代码播放"?用户将编写代码,他们可以为函数、变量等使用任何名称。
我想检查输出,其中输出显示警告框"HTML Code Play"。
用户代码<script>
function showalert()
{
alert("HTML Code Play");
}
</script>
<input type="button" value="Show Alert" onclick="showalert();">
与以下代码(我的代码)
比较<button onclick="alertbox();">Show Alert</button>
<script>
function alertbox()
{
alert("HTML Code Play");
}
</script>
在上面的示例中,两者产生相同的输出,但编码不同。当我检查这两个代码时,我希望结果为true。我能做到吗?这可能吗?
我发现你的问题很有趣,并使用mocha和sinon创建了示例代码。
(要执行此操作,你必须自己安装sinon.js模块。)
我把这段代码作为工作代码发布在这里。
请将'code1','code2'块视为用户实现的代码。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>How to test user code on broweer</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mocha/2.5.3/mocha.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/mocha/2.5.3/mocha.css" rel="stylesheet">
<script type="text/javascript" src="../components/sinon/lib/sinon.js"></script>
<script type="text/javascript" src="../components/sinon/lib/sinon/util/core.js"></script>
<script type="text/javascript" src="../components/sinon/lib/sinon/extend.js"></script>
<script type="text/javascript" src="../components/sinon/lib/sinon/spy.js"></script>
<script type="text/javascript" src="../components/sinon/lib/sinon/call.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container">
<h1>How to test user code on broweer</h1>
<div id="mocha"></div>
<div id="live-code-stage"></div>
<template id="code1">
<button onclick="alertbox();">Show Alert</button>
<script>
function alertbox() {
alert("HTML Code Play");
}
</script>
</template>
<template id="code2">
<script>
function showalert() {
alert("HTML Code Play");
}
</script>
<input type="button" value="Show Alert" onclick="showalert();">
</template>
</div>
<script>
mocha.setup('bdd');
$(function () {
var assert = function (expr, msg) {
if (!expr) throw new Error(msg || 'failed');
};
describe("implement 'show alert button' test", function () {
before(function () {
window.alert = sinon.spy();
});
it("window.alert must to be called with 'HTML Code Play'", function (done) {
$("#live-code-stage").html($("#code1").html()); // testing code1
$("button, input[type='button']").click();
assert(window.alert.calledWith("HTML Code Play"));
done();
});
});
mocha.run();
});
</script>
</body>
</html>
要点你可以模拟窗口。警告:
window.alert = sinon.spy();
你可以检查它(=alert)是否被调用了正确的参数,像这样:
assert(window.alert.calledWith("HTML Code Play"));
如果用户创建的代码是错误的,mocha测试将失败。
引入单位可以改善检测报告的外观。
但这只是一个样本。必须有许多选项来存档您想要的内容。
相关文章:
- 如何使用jQuery来解析json并将其作为列表输出到HTML中
- 如何将JSON数据转换为变量,解析并输出为HTML
- Highcharts将数据输出到HTML表
- 如何将JavaScript数组输出到HTML链接
- 如何让AngularJS输出转义HTML
- 如何使用getJSON将数据从JSON对象输出为HTML
- 获取javascript以将计算输出到HTML
- JS 输出到 html 输入
- 使用 JS 将 XML 数据输出到 HTML
- 如何在输出到 html 之前停止 asp.net 编码字符
- 使用ajax读取php生成的XML文件;使用javascript输出到html
- 将输出的 Html 按钮与输出的 Php/MySQL 数据匹配
- 如何在另一个文件中使用JavaScript将中文输出到HTML表格
- 为javascript输出筛选HTML
- 附加到JSON输出的HTML代码可能是由于Google Docs Chrome扩展
- 模板显示Gmail API JSON输出在HTML中
- 根据输出对HTML数组进行排序
- 如何渲染QuillJS富文本输出到HTML
- 读取JSON并将其输出到HTML表中
- 试图从Javascript文件输出到HTML页面