How can I access PhantomJS WebPage module from inside Mocha tests in NodeJS?

1k views Asked by At

I'm basing this off of the Page Loading section of http://phantomjs.org/quick-start.html

I want to do something like this:

tests.js

var should = require('chai').should();
var page = require('webpage').create();

describe('test website with phantomJS', function() {
    it('should load html from page', function() {
        page.open('myHomePageToTest.html', function(status) {
            if (status === 'success') {
                page.content.should.equal('<!DOCTYPE html>...etc...</html>');
            }
        });
    }); 
});

If I try to run this with 'mocha-phantomjs test.js' I get the error 'Failed to start mocha: Init timeout'

If I try to run this with 'mocha test.js' I get the error 'Cannot find module "webpage"'

I'm sure those are the expected error messages given the code. It's my understanding that is failing. The code is my description of what I want to do. After several hours of treading water last night, I have no idea how to actually do it.

Thank you for any help or nudge in the right direction.

1

There are 1 answers

0
Atishay Jain On
var assert = require('assert');

var phantom = require('phantom');

describe('Mocha and phantom', function () {

    this.timeout(150000);

    it('Tweeking with phantomjs', function (done) {
        phantom.create(function (ph) {
            ph.createPage(function (page) {
                page.open('https://www.facebook.com/', function (status) {


                    page.evaluate(function () {
                       return document.all[0].outerHTML      //can check different elements
                    }, function (result) {
                        console.log('----------->>>>result',result);
                        assert.equal(status,'success','Not appropriate status');
                        done();
                    })
                })
            })
        })
    })
})