|
| 1 | +// a micro-test that can run super quickly between code edits, for sanity checks |
| 2 | +var outputDir = "/test-micro-outputs" |
| 3 | + |
| 4 | + |
| 5 | +var baseUrl = 'http://localhost:8003/'; |
| 6 | + |
| 7 | +// test visualize.html (mode=edit, mode=display), iframe-embed.html, and live.html |
| 8 | +var urlFragments = [ |
| 9 | + // Python |
| 10 | + '#code=%23%20Higher-order%20functions%0A%23%20Adapted%20from%20MIT%206.01%20course%20notes%20(Section%20A.2.2%29%0A%23%20http%3A//mit.edu/6.01/mercurial/spring10/www/handouts/readings.pdf%0A%0Adef%20summation(low,%20high,%20f,%20next%29%3A%0A%20%20%20%20s%20%3D%200%0A%20%20%20%20x%20%3D%20low%0A%20%20%20%20while%20x%20%3C%3D%20high%3A%0A%20%20%20%20%20%20%20%20s%20%3D%20s%20%2B%20f(x%29%0A%20%20%20%20%20%20%20%20x%20%3D%20next(x%29%0A%20%20%20%20return%20s%0A%0Adef%20sumsquares(low,%20high%29%3A%0A%20%20%20%20return%20summation(low,%20high,%20lambda%20x%3A%20x**2,%20lambda%20x%3A%20x%2B1%29%0A%0Aprint(sumsquares(1,%2010%29%29&curInstr=41&origin=opt-frontend.js&py=2&rawInputLstJSON=%5B%5D', |
| 11 | +] |
| 12 | + |
| 13 | + |
| 14 | +var DELAY_MS = 100; |
| 15 | + |
| 16 | +var fs = require('fs'); |
| 17 | +var path = fs.absolute(fs.workingDirectory + '/phantomcss.js'); |
| 18 | +var phantomcss = require(path); |
| 19 | + |
| 20 | +casper.test.begin('Testing page loads from URL strings', function (test) { |
| 21 | + // boring setup code taken from PhantomCSS demo |
| 22 | + phantomcss.init({ |
| 23 | + rebase: casper.cli.get( "rebase" ), |
| 24 | + // SlimerJS needs explicit knowledge of this Casper, and lots of absolute paths |
| 25 | + casper: casper, |
| 26 | + libraryRoot: fs.absolute( fs.workingDirectory + '' ), |
| 27 | + screenshotRoot: fs.absolute( fs.workingDirectory + outputDir + '/screenshots' ), |
| 28 | + failedComparisonsRoot: fs.absolute( fs.workingDirectory + outputDir + '/failures' ), |
| 29 | + addLabelToFailedImage: false, |
| 30 | + }); |
| 31 | + |
| 32 | + casper.on('remote.message', function(msg) {this.echo(msg);}); |
| 33 | + casper.on('error', function (err) {this.die( "PhantomJS has errored: " + err );}); |
| 34 | + casper.on('resource.error', function (err) {casper.log( 'Resource load error: ' + err, 'warning' );}); |
| 35 | + |
| 36 | + |
| 37 | + // start with a baseline image |
| 38 | + casper.start(); |
| 39 | + casper.viewport(1440, 900); |
| 40 | + casper.options.waitTimeout = 10000; // longer default timeout |
| 41 | + |
| 42 | + casper.each(urlFragments, function then(self, e) { |
| 43 | + var urlFrag = e; |
| 44 | + var visualizeHtmlEditMode = baseUrl + 'visualize.html' + e + '&mode=edit'; |
| 45 | + var visualizeHtmlDisplayMode = baseUrl + 'visualize.html' + e + '&mode=display'; |
| 46 | + var iframeEmbedHtml = baseUrl + 'iframe-embed.html' + e; |
| 47 | + var liveHtml = baseUrl + 'live.html' + e + '&mode=display'; // annoying hack - need mode=display to jump to a step in curInstr |
| 48 | + |
| 49 | + // the TRICK is to first go to a different URL or else the app |
| 50 | + // doesn't recognize the URL hashstate change ... which is admittedly a |
| 51 | + // bug in OPT right now. but to work around it, simply open a |
| 52 | + // different URL between page loads: |
| 53 | + self.thenOpen('about:blank'); |
| 54 | + |
| 55 | + self.thenOpen(visualizeHtmlEditMode, function() { |
| 56 | + phantomcss.screenshot('#pyInputPane', 'testEditModeEditor'); |
| 57 | + |
| 58 | + casper.wait(DELAY_MS, function() { |
| 59 | + this.click("#executeBtn"); |
| 60 | + // after clicking on executeBtn, wait for the dataViz div to appear |
| 61 | + // because that means the visualizer has rendered: |
| 62 | + casper.waitFor(function check() { |
| 63 | + return this.evaluate(function() { |
| 64 | + return document.querySelectorAll("#dataViz").length > 0; |
| 65 | + }); |
| 66 | + }, function then() { |
| 67 | + // jump to the last step, then take a screenshot |
| 68 | + casper.click('#jmpLastInstr'); |
| 69 | + phantomcss.screenshot('.visualizer', 'testEditModeVisualizer'); |
| 70 | + }); |
| 71 | + }); |
| 72 | + }); |
| 73 | + |
| 74 | + |
| 75 | + self.thenOpen('about:blank'); |
| 76 | + self.thenOpen(visualizeHtmlDisplayMode, function() { |
| 77 | + casper.waitFor(function check() { |
| 78 | + return this.evaluate(function() { |
| 79 | + return document.querySelectorAll("#dataViz").length > 0; |
| 80 | + }); |
| 81 | + }, function then() { |
| 82 | + phantomcss.screenshot('.visualizer', 'testDisplayMode'); |
| 83 | + }); |
| 84 | + }); |
| 85 | + |
| 86 | + |
| 87 | + self.thenOpen('about:blank'); |
| 88 | + self.thenOpen(iframeEmbedHtml, function() { |
| 89 | + casper.waitFor(function check() { |
| 90 | + return this.evaluate(function() { |
| 91 | + return document.querySelectorAll("#dataViz").length > 0; |
| 92 | + }); |
| 93 | + }, function then() { |
| 94 | + phantomcss.screenshot('.visualizer', 'testIframeEmbed'); |
| 95 | + }); |
| 96 | + }); |
| 97 | + |
| 98 | + |
| 99 | + self.thenOpen('about:blank'); |
| 100 | + self.thenOpen(liveHtml, function() { |
| 101 | + casper.waitFor(function check() { |
| 102 | + return this.evaluate(function() { |
| 103 | + return document.querySelectorAll("#dataViz").length > 0; |
| 104 | + }); |
| 105 | + }, function then() { |
| 106 | + phantomcss.screenshot('body', 'testLiveMode'); // full-page screenshot! |
| 107 | + }); |
| 108 | + }); |
| 109 | + }); |
| 110 | + |
| 111 | + |
| 112 | + // run all tests: |
| 113 | + casper.then(function now_check_the_screenshots() { |
| 114 | + phantomcss.compareAll(); // compare screenshots |
| 115 | + }); |
| 116 | + |
| 117 | + casper.run(function() { |
| 118 | + //phantomcss.getExitStatus() // pass or fail? |
| 119 | + casper.test.done(); |
| 120 | + }); |
| 121 | +}); |
0 commit comments