9

たとえば、テストの前にユーザーがログインするなど、頻繁に実行する必要のあるステップがあります。

CasperJSの再利用可能なコードのチャンクを作成するにはどうすればよいですか?CasperJSを拡張するためのドキュメントは、1つのファイルに対してのみ記述されています...

ありがとう!

4

1 に答える 1

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
于 2013-05-22T14:43:07.983 に答える