How to write own CasperJS modules?

老子叫甜甜 提交于 2019-12-06 21:28:23

问题


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...

Thanks!


回答1:


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

tests/casper/test.coolPage.coffee

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

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'

casper.run ->
  @test.done()

tests/casper/test.login.coffee

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 test.coolPage.coffee --url=http..my-test-url


来源:https://stackoverflow.com/questions/15138761/how-to-write-own-casperjs-modules

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!