使用mocha-phantomjs自动化功能测试 [英] using mocha-phantomjs to automate functional testing

查看:83
本文介绍了使用mocha-phantomjs自动化功能测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的项目正在使用:Node,Coffeescript,SocketIO,Browserify和Mocha. (用于标准服务器端单元测试的Mocha)

My project is using: Node, Coffeescript, SocketIO, Browserify and Mocha. (mocha for standard server-side unit tests)

我想使用无头浏览器自动执行一些客户端接口测试. PhantomJS看起来是理想的选择(由于Web套接字的支持而被Zombie选中).

I would like to automate some client-side interface testing using a headless browser. PhantomJS looked like the ideal choice (picked over Zombie due to web socket support).

PhantomJS页面警告它不是测试运行程序,据我了解,他们建议使用 mocha-phantomjs 项目来驱动您的测试.

The PhantomJS pages warn it is not a test runner, which I understand, and they recommend using the mocha-phantomjs project to drive your tests.

因此我已经能够运行示例测试(例如mocha-phantomjs tests/mixed.html),但是我当前的问题实际上是在测试中使用PHANTOM. Mocha-phantomjs存储库中的所有示例测试似乎都使用标准的Mocha服务器端单元测试.

So I've been able to get the sample tests running (e.g. mocha-phantomjs tests/mixed.html), but my current problem is actually using PHANTOM within the tests. All the sample tests in the mocha-phantomjs repo seem to use standard mocha server-side unit test.

例如我可以轻松地运行mocha-phantomjs tests/mixed.html来查看无聊的旧单元测试.或者我可以运行phantomjs tests/login.coffee来加载我的登录屏幕...但是如何结合两者对我应该在登录屏幕上看到的内容进行断言?

e.g. I can easily run mocha-phantomjs tests/mixed.html to view boring old unit tests. Or I can run phantomjs tests/login.coffee to load up my login screen... but how do I combine the two to make assertions on what I should expect to see on my login screen?

我在网络上找不到任何此类示例,并且正在努力理解实现此目标的最佳方法.

I can't find any examples of this on the web, and I'm struggling with understanding the best way to go about this.

希望这一切都有道理.预先感谢您的协助.

Hope this all makes sense. Thanks in advance for any assistance.

更新:我发现了作者的以下建议(此处 ),但我不太清楚该怎么做:phantomjs lib/mocha-phantomjs.coffee test/mixed.html

UPDATE: I found the following suggestion by the author (here), but I don't really understand exactly what to do with it: phantomjs lib/mocha-phantomjs.coffee test/mixed.html

推荐答案

有一个相当不错的教程,可用于测试Mocha和Phantom.JS

There's a fairly nice tutorial for testing with Mocha and Phantom.JS here.

关于Mocha和PhantomJS的部分很短,但是基本思想是将DOM断言和交互放入您的Mocha测试套件中,通过testrunner.html文件在客户端运行它,然后将mocha-phantomjs指向testrunner.html文件.

The section on Mocha and PhantomJS is short, but the basic idea is to put DOM assertions and interactions into your Mocha test suite, run it a la client-side via a testrunner.html file, and then point mocha-phantomjs at the testrunner.html file.

换句话说,您的Mocha测试可能看起来像这样:

To paraphrase, your Mocha test might look like this:

describe("DOM Test", function () {

    var el = document.createElement("div");
    el.id = "myDiv";
    el.innerHTML = "Hello World!";
    document.body.appendChild(el);
    var myEl = document.getElementById('myDiv');

    it("has the right text", function () {
        (myEl.innerHTML).should.equal("Hello World!");
    });
});

testrunner.html文件将是正常设置:

And the testrunner.html file would be the normal setup:

<html>
    <head>
        <title> Tests </title>
        <link rel="stylesheet" href="./node_modules/mocha/mocha.css" />
    </head>
    <body>
        <div id="mocha"></div>
        <script src="./node_modules/mocha/mocha.js"></script>
        <script src="./node_modules/chai/chai.js"></script>
        <script>
            mocha.ui('bdd');
            mocha.reporter('html');
            var should = chai.should();
        </script>
        <script src="test/test.js"></script>
        <script>
            if (window.mochaPhantomJS) { mochaPhantomJS.run(); }
            else { mocha.run(); }
        </script>
    </body>
</html>

如果您希望完全从node.js生态系统运行的解决方案,则值得考虑 Zombie.JS .此堆栈溢出问题提供了一个基本示例.

If you'd prefer a solution run entirely from the node.js ecosystem, it's worth considering Zombie.JS. This Stack Overflow question provides a basic example.

需要权衡的是,虽然Zombie.JS可以仅通过需要node模块来使用,而且速度非常快,但它不是一个真正的" Web浏览器. PhantomJS更接近,因为它基于webkit.另外,使用mocha-phantomjs的第一种方法将允许您在您选择的不同浏览器中运行客户端Mocha测试,PhantomJS只是其中之一.

The tradeoff is that while Zombie.JS can be used simply by requiring the node module, and is extremely fast, it's not a "real" web browser. PhantomJS is closer, as its based on webkit. Also, the first approach with mocha-phantomjs would allow you to run the client-side Mocha tests in different browsers of your choice, PhantomJS being just one of them.

这篇关于使用mocha-phantomjs自动化功能测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆