如何编写自己的CasperJS模块? [英] How to write own CasperJS modules?

查看:65
本文介绍了如何编写自己的CasperJS模块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,我有一个经常需要执行的步骤,例如在进行某些测试之前登录用户。

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

如何为CasperJS编写可重用的代码块?他们用于扩展CasperJS的文档仅针对一个文件编写。

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

谢谢!

推荐答案

这是一种简单的方法。如果不熟悉coffeescript,请通过js2coffee将其转换为JS。

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。咖啡

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

这篇关于如何编写自己的CasperJS模块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆