donglanche9257 2014-08-07 06:30
浏览 95

如何获取要在HTML上显示的断言错误消息?

I've been trying to learn how to use expect.js assertion plugin for my project. My problem is i want to get the error message which only displayed on console section of my browser's inspect element to be alerted into my HTML page when i click my button. Here's what i've been doing so far

<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>

When i click the button the error will show on console when i check inspect element from my browser. What I'm trying to do is get the error message and alert the message when i click the button.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 基于卷积神经网络的声纹识别
    • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
    • ¥100 为什么这个恒流源电路不能恒流?
    • ¥15 有偿求跨组件数据流路径图
    • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
    • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
    • ¥15 CSAPPattacklab
    • ¥15 一直显示正在等待HID—ISP
    • ¥15 Python turtle 画图
    • ¥15 stm32开发clion时遇到的编译问题