HuddleEng / PhantomFlow

Describe and visualise user flows through tests with PhantomJS
MIT License
682 stars 60 forks source link

Use with Page Objects - using require() in test flows #41

Closed psimyn closed 8 years ago

psimyn commented 8 years ago

I am trying to move some existing PhantomCSS and Casper tests to use PhantomFlow. Is there a way to require other modules/deps inside flows. From what I understand they are run in the Phantom domain, so it may need either a build step or additional config.

Alternatively, is there an example setup of PhantomFlow being included in a project as a dependency - I've been finding the included examples useful as a starting point but missing a few things.

Other info:

Current directory structure (with Casper/PhantomCSS) looks like this:

test
|-- pages
|   |-- homepage.js
|
|-- homepage.test.js
|-- test-utils.js

Then homepage.test.js looks like this:

var homepage = require('./pages/homepage.test');
var testUtils = require('./test-utils');

casper.start();
homepage.load();
testUtils.capture();

Thanks!

jamescryer commented 8 years ago

Worth taking a look at https://github.com/Huddle/grunt-phantomflow/blob/master/tasks/phantomflow.js

I don't see any reason why you cant use require to bring in scripts. PhantomFlow also has an includes directory option to automatically include and run JS in the global context.

PhantomFlow is a nested approach to expressing user-centric test flows, so in your simple case I would expect to see the following in homepage.test.js

var homepage = require('./test/pages/homepage.js');
var testUtils = require('./test/test-utils.js');

flow("Homepage", function(){
    step("Go to homepage", homepage.load);
    step("Look at page", testUtils.capture);
});
psimyn commented 8 years ago

I can get require working like that for the file being directly called by node, but still not in tests.

In addition to the above structure, I have a phantomflow.js file, which does require('phantomflow').init() and flow.run. Including pages works here, but for all tests (such as home.test.js) any require in the test files gives this error:

error: "CasperError: Can't find module ./test/pages/homepage.js

I've tried a few variations on the require path, but nothing yet. I'll try to get a better example together if this isn't clear.

Thanks for your help

psimyn commented 8 years ago

I added an example to https://github.com/psimyn/phantomflow-example

running npm test:casper, the require works and it errors on flow(). That uses a relative path from the flows directory - require('../pages/home.page.js');

running npm test with any of the following lines in home.test.js results in "can't find module" errors:

require('./test/pages/home.page.js');
require('../pages/home.page.js');
require('test/pages/home.page.js');
require('./pages/home.page.js');

Any ideas?

zhangtao07 commented 8 years ago

your relative path is not right , I test with a absolute path and it works.

psimyn commented 8 years ago

thanks @zhangtao07 - looks to have been path issue