我一直在努力学习如何为我的项目使用expect.js断言插件。我的问题是,当我点击我的按钮时,我想获取仅显示在浏览器的inspect元素的控制台部分上的错误消息,以警告我的HTML页面。这是我到目前为止所做的事情
<body>
<script>mocha.setup('bdd')</script>
<div id="mocha"></div>
<ol>
<li>tes</li>
<li>tes</li>
<li>tes</li>
<li>tes</li>
<label></label>
<div class="post">
</div>
<button id="klik">Klik</button>
</ol>
<script type="text/javascript">
var salah="";
describe("Index", function () {
it("should have div",function(){
$expect('input').to.exist(function (willThrow) {
// Some cleanup code.
return 'Please add a input to the page.';
});
});
it("should have img",function()
{
$expect('img').to.exist('Please add a img to the page!');
});
it("should have li 4",function()
{
$expect('ol > li').to.have.items(4);
});
});
mocha.run();
var fn = function(){
var missingPorHMsg = "Your div should surround the h2 and two p's";
$expect('div.post').to.exist('Make sure your new div has a class name of "post"!')
.and.to.have.children('h2', missingPorHMsg)
.and.to.have.children('p', missingPorHMsg)
.that.has.elements(4, missingPorHMsg).end()
.and.to.have.css( 'border'
, '1px dashed #ccc'
, "Make sure you give your post div a border!");
};
$('#klik').click(function(e){
alert(fn());
});
</script>
</body>
当我点击按钮时,当我从浏览器中检查inspect元素时,错误将显示在控制台上。我正在尝试做的是获取错误消息并在我单击按钮时提醒消息。