testing existing pages with mocha-phantomjs -


i'm not quite getting how use phantomjs , mocha together, through mocha-phantomjs.

i've read tutorials (the 1 @ tutsplus quite helpful) , not seeing how can test external pages using phantom , mocha. i'm sure need nudge.

in tutorial author creates tests.js file dom setup/manipulation, mocha assertions. then, creates separate html file loads tests.js file , uses mocha-phantomjs fire phantom , run tests.

that's i'm little confused, how mochaphantomjs.run() method things behind scenes, whether knows search js file describe block , run tests within, sort of thing. don't need chapter , verse, high-level summary ideal.

also, if want test outside page, how can best that? in tutorial dom investigation , testing done on test page. if want test different page, change or setup assertions differently? should call phantomjs api tests , point external site?

mocha run tests have been specified in javascript has been included in html page launched. if @ example html page on mocha-phantomjs expects test definitions using describe/it calls in test/mycode.js file. if put like

these tests testing in main file , associated javascript, there isn't special mocha-phantomjs provides test external html files. if want test own html files think can head in couple of directions, came these:

first option: create way load parts of app want test main testing html file. how depends lot on application setup. well-suited modular system. include javascript application , test it. not full-page-html tests.

second option: open new windows pages test main testing html file (from within phantom is). can open page using window.open() normal browser. created proof of concept this:

describe('hello web', function () {         it('world', function (done) {             var windowreference = window.open("fileundertest.html");              // windowreference.onload didn't work me, resorted solution             var intervalid = window.setinterval(function () {                 if (windowreference.document && windowreference.document.readystate === 'complete') {                     window.clearinterval(intervalid);                     expect(windowreference.document.title).to.equal("file under test");                     done();                 } else {                     console.log('not ready yet');                 }             }, 10);         });     } ) 

this solution relatively simple, has same drawbacks page-loading solution: never know when page initialized , have resort sort of timeout/wait system wait page correct state. if want test lot of separate html files these delays start add up. additionally waiting 'onload' on page opened wouldn't work created own function based on setinterval , (non-perfect) test on document being loaded. found out there differences in behavior between loading html page filesystem , loading same page via web-server.

third option: create mocha test run nodejs-style command line, , launch phantomjs specific page part of mocha tests. i'd need if system depends on html pages quite different each other.

i tested third option, here test based on example found on phantom page (which alternative solution phantomjs used mocha-phantomjs -- i've used neither more brief experiments cannot recommend 1 use)

'use strict';  var chai = require('chai'),     phantom = require('phantom');  var expect = chai.expect;  describe('hello node', function () {         it('world', function (done) {             phantom.create(function (ph) {                 ph.createpage(function (page) {                     page.open("http://www.google.com", function (status) {                         console.log("opened google? ", status);                         page.evaluate(function () { return document.title; }, function (result) {                             console.log('page title ' + result);                             ph.exit();                             expect(result).to.equal("google");                             done();                         });                     });                 });             });          });     } ) 

while possible test way i'd maybe overhead of communication between code in phantom-world , testing code in nodejs world isn't worth it. can of course move lot of general functionality couple of helper functions, still stuck having call page.evaluate() perform specific tests on page. same issues timeouts/waits above apply.

as aside: know casperjs? maybe can helpful setup should choose build on 'plain' phantomjs.


Comments

Popular posts from this blog

php - SPIP: From Tag directly to an article -

jquery - isAjaxRequest always return false -

ruby on rails - In a controller spec, how to find a specific tag in the generated view? -