{"id":7925,"date":"2015-11-10T11:30:23","date_gmt":"2015-11-10T11:30:23","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2015\/11\/10\/how-to-use-casperjs-in-node-js-open-source-projects-n1k0-casperjs\/"},"modified":"2015-11-10T11:30:23","modified_gmt":"2015-11-10T11:30:23","slug":"how-to-use-casperjs-in-node-js-open-source-projects-n1k0-casperjs","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2015\/11\/10\/how-to-use-casperjs-in-node-js-open-source-projects-n1k0-casperjs\/","title":{"rendered":"How to Use CasperJS in node.js?-open source projects n1k0\/casperjs"},"content":{"rendered":"<p><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/f5d4e58ba97f712ca99b4a1dc570345b?s=128&amp;d=identicon&amp;r=PG\" \/> <strong>a paid nerd<\/strong><\/p>\n<p>One solution (which worked for me) is to start and stop your server on a per-test basis. For example, I have a <code>runtests.coffee<\/code> which looks like:<\/p>\n<pre><code>http = require 'http'\nglob = require 'glob'\nspawn = require('child_process').spawn\n\ndb = require '.\/db' # Contains all database stuff.\nwebapp = require '.\/webapp' # Contains all of the Express stuff.\n\ndb.connect 'test' # Connects to the db server and creates an empty test db.\nserver = http.createServer webapp.makeApp()\nserver.listen 0, -&gt;\n    port = server.address().port\n    process.env.URL = \"http:\/\/localhost:#{ port }\"\n    glob 'tests\/*', (err, filenames) -&gt;\n        child = spawn 'casperjs', ['test'].concat(filenames)\n        child.stdout.on 'data', (msg) -&gt; process.stdout.write msg\n        child.stderr.on 'data', (msg) -&gt; process.stderr.write msg\n        child.on 'exit', (code) -&gt;\n            db.disconnect() # Drops the test db.\n            server.close()\n            process.exit code\n<\/code><\/pre>\n<p>And my CasperJS tests in <code>tests\/<\/code> look like:<\/p>\n<pre><code>URL = require('system').env.URL # Note, Casper code here, not Node.\n\ncasper.test.begin 'Test something', 1, (test) -&gt;\n    casper.start \"#{ URL }\/welcome\"\n    casper.then -&gt;\n        test.assertHttpStatus 200\n        # ....\n    casper.run -&gt;\n        test.done()\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>a paid nerd One solution (which worked for me) is to start and stop your server on a per-test basis. For example, I have a runtests.coffee which looks like: http = require &#8216;http&#8217; glob = require &#8216;glob&#8217; spawn = require(&#8216;child_process&#8217;).spawn db = require &#8216;.\/db&#8217; # Contains all database stuff. webapp = require &#8216;.\/webapp&#8217; # Contains [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-7925","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/7925","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=7925"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/7925\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=7925"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=7925"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=7925"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}