Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

How to write own CasperJS modules?

How to write own CasperJS modules?


For example, I have a step that often needs to be executed, eg user login before some test.

How to write reusable chunks of code for CasperJS? Their documentation for extending Casperjs is written only for one file...


Problem courtesy of: InviS


Here's a simple approach. If not familiar with coffeescript, convert it to JS over at js2coffee.


loginModule = require("./test.login")

casper.test.comment "Testing cool stuff, should be logged in by now"

casper.thenOpen casper.cli.get("url") + "/myCoolPage", ->
  @test.assertExists '#myCoolDiv'

casper.then () ->
  @test.assertExists '.somethingElse' ->


exports.login = (username, password) ->
  casper.test.comment "Loggin in with username \"#{username}\", password \"#{password}\""

  casper.start casper.cli.get("url") + "/login", ->
    @test.assertExists "input[name=username]", "input[name=password]"

  casper.then () ->
    @sendKeys "input[name=username]", username
    @sendKeys "input[name=password]", password
    @click "input[type=submit]"

  casper.then () ->
    #assert you got logged in

running from command line:

cd tests/casper    
casperjs test
Solution courtesy of: y3sh


View additional discussion.

This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

How to write own CasperJS modules?


Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription