質問

I'm testing my web-app with Mocha and WebDriver. I'm struggling with the best practices regarding Mocha test-order and persistent state of the driver.

I want to separate tests to different files, e.g.

test\
    index.js
    selenium\
        login.js
        search.js

So in execution wise, login.js has to be first because it logs in to the app and gets authenticated. Only after that search.js is possible to do. But how? In login.js I now have this:

webdriverjs = require('webdriverjs');

describe 'UI/Selenium', ->
    client = {}

    before ->
        client = webdriverjs.remote
            desiredCapabilities:
                browserName: 'chrome'

        client.init()
        client.windowHandleSize({width: 1920, height: 1080})

    it 'should let us login', (done) ->
        client.url('http://127.0.0.1:1337/login')
        .setValue('#username', 'username')
        .setValue('#password', 'password')
        .buttonClick('button[type="submit"]')
        .waitFor '#search_results_user', 5000, (err) -> throw err if err
        .call done

How can I persist the state of client to other tests without having to re-init it every time? And how do I define the execution order of files with Mocha?

役に立ちましたか?

解決

How can I persist the state of client to other tests without having to re-init it every time?

You setup whatever you want to share among tests in a before hook (and tear it down in an after hook). This would mean moving the code in your test for logging in into your before hook. Supposing you are testing the "foo" view, you could do:

describe("foo view", function () {
    before(function () { /* create selenium driver */ });

    describe("when used by a logged in user", function () {
        before(function () { /* log in */ });

        it(...

        it(...

        after(function () { /* log out */ });
    });

    describe("when used by a logged out user", function () {
        it(...

        it(...
    });
    after(function () { /* shut down the driver */ });
});

And how do I define the execution order of files with Mocha?

Mocha tests should not depend on one another and consequently should not depend on the order in which they are executed.

If you are in a situation where you must break this cardinal rule, you could just invoke Mocha from the command line with the list of test files, in the order you want. Or you could launch Mocha programmatically and use addFile to add the files in order.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top