Skip to content

Instantly share code, notes, and snippets.

@oliveiraev
Last active December 26, 2015 05:49
Show Gist options
  • Save oliveiraev/7103110 to your computer and use it in GitHub Desktop.
Save oliveiraev/7103110 to your computer and use it in GitHub Desktop.

Revisions

  1. oliveiraev revised this gist Oct 22, 2013. 1 changed file with 9 additions and 8 deletions.
    17 changes: 9 additions & 8 deletions Jasmine-to-Casper.js
    Original file line number Diff line number Diff line change
    @@ -22,16 +22,18 @@ casper.parseJasmineTests = (function (casper) {
    * Raise up the DOM tree, looking for DIV.suite elements. Each found
    * element represent a spec level.
    *
    * @param {Node,HTMLAnchorElement} element Usually, an a.description
    * element.
    * @param {Node,HTMLDocument,HTMLElement} element Usually, an
    * a.description element.
    * @returns {Array} The text of all the DIV.suite > A.description
    * elements found.
    */
    function getSuiteFrom(element) {
    var suite = [];
    var suite, child;
    suite = [];
    while (element.parentNode) {
    child = element.getElementsByClassName("description")[0];
    if (/suite/.test(element.className)) {
    suite.unshift(element.childNodes[0].innerHTML);
    suite.unshift(child.innerHTML);
    }
    element = element.parentNode;
    }
    @@ -60,16 +62,15 @@ casper.parseJasmineTests = (function (casper) {
    spec = spec[name].specs;
    }
    }
    spec = spec[name].tests;
    if (!(spec && element.parentNode.className.match(/Summary/))) {
    if (!(name && element.parentNode.className.match(/^spec/))) {
    return;
    }
    spec.push([
    spec[name].tests.push([
    element.innerHTML,
    /passed/.test(element.parentNode.className)
    ]);
    }
    reporter = document.getElementById("HTMLReporter");
    reporter = document.getElementsByClassName("jasmine_reporter")[0];
    results = reporter.getElementsByClassName("description");
    output = {};
    Array.prototype.slice.apply(results).map(parse);
  2. oliveiraev created this gist Oct 22, 2013.
    143 changes: 143 additions & 0 deletions Jasmine-to-Casper.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,143 @@
    /**
    * Convert Jasmine suite into parseable CasperJS test suite.
    */
    casper.parseJasmineTests = (function (casper) {
    "use strict";

    /**
    * Build a structure from Jasmine test results.
    *
    * Each spec generates an object with two keys: specs and tests.
    * Specs contains another object with sub-spectations.
    * Tests maintain an array with spec test results.
    *
    * @returns {Object} Object.specName = {specs: {}, tests[]}
    */
    function getJasmineResult() {
    var reporter, results, output;

    /**
    * Find out the suite name starting from given element.
    *
    * Raise up the DOM tree, looking for DIV.suite elements. Each found
    * element represent a spec level.
    *
    * @param {Node,HTMLAnchorElement} element Usually, an a.description
    * element.
    * @returns {Array} The text of all the DIV.suite > A.description
    * elements found.
    */
    function getSuiteFrom(element) {
    var suite = [];
    while (element.parentNode) {
    if (/suite/.test(element.className)) {
    suite.unshift(element.childNodes[0].innerHTML);
    }
    element = element.parentNode;
    }
    return suite;
    }

    /**
    * Process a detail element.
    *
    * The function asks for the whole suite name tree and, if a test
    * result, append the result to the outputs.
    *
    * @param {HTMLAnchorElement} element On most cases, the element will
    * contain a test result. But it can be the suite name too.
    */
    function parse(element) {
    var suite, name, spec;
    spec = output;
    suite = getSuiteFrom(element);
    while (suite.length) {
    name = suite.shift();
    if (!spec[name]) {
    spec[name] = {"specs": {}, "tests": []};
    }
    if (suite.length) {
    spec = spec[name].specs;
    }
    }
    spec = spec[name].tests;
    if (!(spec && element.parentNode.className.match(/Summary/))) {
    return;
    }
    spec.push([
    element.innerHTML,
    /passed/.test(element.parentNode.className)
    ]);
    }
    reporter = document.getElementById("HTMLReporter");
    results = reporter.getElementsByClassName("description");
    output = {};
    Array.prototype.slice.apply(results).map(parse);
    return output;
    }

    /**
    * Convert the Jasmine output into CasperJS suites.
    *
    * Grabs the object generated by getJasmineResult(), iterates over it and
    * adds specs as CasperJS spectations and tests as CasperJS assertions.
    *
    * @param {Object} jasmineResult A {specs: {}, tests: []} set. It may be
    * generated with getJasmineResult().
    * @param {String} parentName Any text that should be prepended to the
    * spec title. It exists because CasperJS doesn't nest specs like Jasmine.
    * With parentName, we can create an output like "# SuperSpec > SubSpec".
    */
    function jasmineToCasper(jasmineResult, parentName) {
    var specName;

    /**
    * Starts a CasperJS spec.
    *
    * This function shouldn't be passed as casper.test.begin() callback. It
    * is the responsible for the begin() calls and passes its own callback,
    * the runSuite function.
    *
    * @param {String} specName The text that will be output as CasperJS
    * spec.
    */
    function begin(specName) {
    var suite;
    /**
    * Performs assertions over the Jasmine tests.
    *
    * It iterates over the specName.tests array asserting that the 2nd
    * item evaluates to true. It happens if the Jasmine expectation
    * passed. This approach ensures that failed Jasmine tests also fail
    * the CasperJS suite.
    *
    * @param {Casper.Tester} tester The Tester instance passed to
    * callback on test.begin() calls.
    */
    function runSuite(tester) {
    function runTest(test) {
    tester.assert(test[1], test[0]);
    }
    suite.tests.map(runTest);
    tester.done();
    }
    suite = jasmineResult[specName];
    if (parentName) {
    specName = parentName + " > " + specName;
    }
    // Empty suites causes CasperJS to fail.
    if (suite.tests.length) {
    casper.test.begin(specName, runSuite);
    }
    jasmineToCasper(suite.specs, specName);
    }
    for (specName in jasmineResult) {
    if (jasmineResult.hasOwnProperty(specName)) {
    begin(specName);
    }
    }
    }
    return function () {
    jasmineToCasper(casper.evaluate(getJasmineResult), null);
    }
    }(casper));