例如,我有一个经常需要执行的步骤,例如在某次测试之前用户登录。
如何为CasperJS编写可重用的代码块?他们扩展CasperJS的文档仅针对一个文件编写......
谢谢!
答案 0 :(得分:8)
这是一个简单的方法。如果不熟悉coffeescript,请在js2coffee将其转换为JS。
<强>测试/卡斯帕/ 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()
<强>测试/卡斯帕/ 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
从命令行运行:
cd tests/casper
casperjs test test.coolPage.coffee --url=http..my-test-url