mardi 27 juin 2017

Running asynchronous tests on Phantom and node

I'm using phantom module on node and want to test UI on different resolutions. Basically I've achieved what I wanted with this piece of code:

const phantom = require('phantom');

const   lgResolutionSuite = require('./suites/lg'),
        mdResolutionSuite = require('./suites/md'),
        xlResolutionSuite = require('./suites/xl');


let testSuites = [lgResolutionSuite, mdResolutionSuite, xlResolutionSuite];

(async function() {

    let instance = await phantom.create(),
        page = await instance.createPage();

        page.property('onConsoleMessage', function(msg, lineNum, sourceId) {
            console.log(msg);
        });

    for (let testSuite of testSuites) {
        if (!testSuite.viewportSize) {
            throw new Error('Set up viewportsize for suite.')
        }
        if (!testSuite.suite) {
            throw new Error('Set up suite to run.')
        }

        await page.open('./tests/css-test.html')
            .then(status => {
                page.property('viewportSize', testSuite.viewportSize);
                return page.evaluateJavaScript("" + testSuite.suite);
            })
            .then(() => {
                return page.evaluate(function() {
                    mocha.run();
                });
            });
    }


    await instance.exit();
}());

But as I'm learning JS I would like to rewrite it to something more elegant. Right now I'm trying this code:

const phantom = require('phantom');

const   lgResolutionSuite = require('./suites/lg'),
        mdResolutionSuite = require('./suites/md'),
        xlResolutionSuite = require('./suites/xl');


let testSuites = [lgResolutionSuite, mdResolutionSuite, xlResolutionSuite];

(async function() {
    let instance,
        phantomPage;

    let tests = await phantom.create()
            .then(inst => {
                instance = inst;
                return instance.createPage();
            })
            .then(page => {
                phantomPage = page;
                phantomPage.property('onConsoleMessage', function(msg, lineNum, sourceId) {
                    console.log(msg);
                });
                return phantomPage;
            })
            .then(phantomPage => {
                return phantomPage.open('./tests/css-test.html');
            })
            .then((status) => {
                return testSuites.map((testSuite) => {
                    return phantomPage.property('viewportSize', testSuite.viewportSize)
                        .then(() => {
                            phantomPage.evaluateJavaScript("" + testSuite.suite)
                        })
                        .then(() => {
                            return phantomPage.evaluate(function () {
                                mocha.run();
                            });
                        })
                        .then(() => {
                            return phantomPage.reload();
                        })
                });
            })
            .catch(console.error);


    Promise.all(tests).then((values) =>{
        console.log(values);
    })
        .catch(console.error);

    await instance.exit();
}());

but running it ( node file.js ) throws error:

Error: Error reading from stdin: Error: read ECONNRESET
    at Phantom._rejectAllCommands (node_modules/phantom/lib/phantom.js:361:41)
    at Phantom.kill (node_modules/phantom/lib/phantom.js:341:14)
    at Socket.Phantom.process.stdin.on.e (node_modules/phantom/lib/phantom.js:175:18)
    at emitOne (events.js:115:13)
    at Socket.emit (events.js:210:7)
    at emitErrorNT (internal/streams/destroy.js:62:8)
    at _combinedTickCallback (internal/process/next_tick.js:102:11)
    at process._tickCallback (internal/process/next_tick.js:161:9)

even though the tests array contains promises. Also I would like to reload page on each suite to remove previously injected test code, that's why reload function is used. What am I missing? :)

Aucun commentaire:

Enregistrer un commentaire