百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
L

lotte

> 编程语言
开源

自动化、无头浏览器测试(使用 PhantomJS)。

99 stars0 点赞1 次浏览
访问官网GitHub

工具介绍

自动化、无头浏览器测试(使用 PhantomJS)。

Lotte

Lotte is a headless, automated testing framework built on top of PhantomJS and inspired by Ghostbuster. It adds jQuery-like methods and chaining, more assertion logic and an extensible core. Tests can be written in either JavaScript or CoffeeScript.

Lotte comes with tools for accessing the DOM, evaluating arbitrary code, simulating mouse and keyboard input.

Tests are sandboxed and run asynchronously. Blocking methods are available to simulate dependencies and to control the flow of execution.

This project is still highly experimental. Using it may cause your computer to blow up. Seriously.

Prerequisites

  • [node.js][nodejs] >=0.8
  • [npm][npm] >=1.0
  • PhantomJS **

    ...body of test...

…

coffeescript @title 'Github' @base 'https://github.com'

@open '/', 'the homepage', -> @describe 'counter shows number of repositories', -> # ...assertion logic... @group 'Sign Up button', -> @describe 'is in place', -> # ...assertion logic... @describe 'takes you to /plans', -> # ...assertion logic...


#### Flow of Execution

Each test case is executed in the order in which it is defined:

```coffeescript
@describe 'I run first', ->
@describe 'I run second', ->
# etc.

If a test case contains an asynchronous function call, the next test case is executed without waiting for the function to finish:

coffeescript
@describe 'I run first', ->
@describe 'I run second', ->
  setTimeout ( -> ), 2500
@describe 'I run third in parallel with second still running', ->

Be extremely careful when dealing with asynchronous function. For example, using .click() to follow an anchor could change the page while another test case is running.

If a test case fails, any remaining test cases are skipped:

coffeescript
@describe 'I run first', ->
  throw 'Whoops!'
@describe 'I should run second, but I never will', ->

To simulate dependencies and control the flow of execution, you can use the following functions:

  • @wait(name..., block)

    Blocks the current test case until all dependencies have finished (either passed or failed).

The earlier example can now be rewritten as follows to make it synchronous again:

coffeescript
@describe 'I run first', ->
@describe 'I run second', ->
  setTimeout ( -> ), 2500
@describe 'I run third', ->
  @wait 'I run second', ->
    # ...assertion logic...

Environments

Lotte uses PhantomJS to execute tests. While you may be writing tests in JavaScript and expect to be able to access the DOM of a page directly, this is not the case.

Each test file runs in its own sandboxed environment. Each page you request also runs in a sandbox. You cannot access variables across environments, i.e., you cannot define a variable in your test file and access it within the page you have just requested:

coffeescript
@open 'https://github.com', ->
  @describe 'Sandbox', ->
    val = 'value'
    # following line throws an exception
    console.log(@page.evaluate( -> return val))
    throw 'exit'

In the above code snippet @page.evaluate runs the function as if it were defined on the page you just requested, i.e, github.com. In order to do so, PhantomJS serializes the function, but it does not include the context in which it was defined. When the function is executed, val is missing in the new context causing it to throw an exception.

Another limitation of PhantomJS is the fact you cannot return complex types from the page. Objects are serialized before they leave the page sandbox and unserialized back in the parent (test case) environment:

coffeescript
@open 'https://github.com', ->
  @describe 'serialize/unserialize', ->
    h1 = @page.evaluate( -> return document.querySelector('h1'))
    # prints 'H1' correctly
    console.log h1.tagName
    # prints 'undefined' as functions cannot be serialized/unserialized
    console.log h1.focus
    throw 'exit'

Lotte comes with a workaround which allows you to pass variables to the PhantomJS environment. This hack has the same limitations as outlined above (it uses JSON.stringify internally):

  • @using(hash, block)

    hash is a key: value object where:

    - `key` must be a valid identifier and defines the name of the variable within the PhantomJS environment
    - `value` must be serializable and contains the value of the variable

    block is either a function or a string.

    Returns a Function string with all key: value pairs from hash as arguments.

coffeescript
@open 'https://github.com', ->
  @describe '@using(..)', ->
    expected = 'git'
    @$('h2').first @using { expected }, (element) -> element.innerHTML.indexOf(expected) is 0
    throw 'exit'

Document Queries

There is a lot of boilerplate code required to access the DOM of a page. Lotte comes with a jQuery-like query function to abstract some of the most common operations:

  • @$(selector)

    selector is a string containing a selector expression.

    Returns a DocumentQuery object.

The earlier example can now be rewritten as follows:

coffeescript
@open 'https://github.com', ->
  @describe 'Document Queries', ->
    # prints 'H1' correctly
    console.log @$('h1').tagName
    # prints 'undefined'
    console.log @$('h1').focus
    throw 'exit'
Additional Methods

A DocumentQuery object has the following methods to deal with the DOM:

  • DocumentQuery.prototype.attr([index], property)

    Gets the value of property for the element at index.

    The following code snippets are equivalent:

    coffeescript
    @$('h1').attr('tagName')
    @$('h1').tagName
…

coffeescript @open 'https://github.com', -> @describe 'click(..)', -> # prints https://github.com/ console.log('I am now on: ' + @page.evaluate( -> location.href)) @$('.signup-button').click -> # prints https://github.com/plans console.log('I am now on: ' + @page.evaluate( -> location.href)) throw 'exit' ```

  • DocumentQuery.prototype.input(value, [index], [message])

    Inputs value in the element at index. This method can be used to simulate keyboard input.

    value is a string to input as if it were keyboard input.
    index is the position of the element in the matched selector collection. If you omit this argument, the first element is assumed.
    message is an optional message to display if something goes wrong, i.e., if no elements were matched.

    coffeescript
    @open 'http://www.google.com', ->
      @describe 'input(..)', ->
        @$('[name="q"]').input 'meaning of life'
        @$('input[type="submit"]').click ->
          console.log @$('#res').attr('innerText')
          throw 'exit'
…

coffeescript @open 'https://github.com', -> @describe 'Navigation and children have classes', -> @$('.nav').first (element) -> element.classList.contains('logged_out') @$('.nav li').each (element) -> !! element.className throw 'exit'


Passing Tests
-------------

So far we have ended tests with `throw 'exit'`. To pass a test case, use the following functions:

- **`@success()`**

    Marks the test case as passed. You must call this once within each `@describe` block.

If you don't end a test case either by failing any of the asserts or calling `@success()`, the entire test file will hang until `timeout` is reached at which point it is recorded as failed.

FAQs
----

- **Q**: How can I log in before requesting a page?

    **A**: You can nest `@open(..)` functions to achieve this:

    ```coffeescript
    @base 'http://local.dev'
    @open '/login', ->
      @describe 'Log in', ->
        @$('.username').input 'admin'
        @$('.password').input 'password'
        @$('input[type="submit"]').click ->
          @success()
          @open '/account', ->
            @describe 'My Account', ->
              # ...assertion logic...
              @success()
    ```

- **Q**: Can I pass arguments to my tests?

    **A**: Yes. Use `--` on the command-line followed by the arguments:

    ```
    $ lotte -- arg1 arg2
    ```

    Arguments will be available in your test files as `phantom.args[..]`.

- **Q**: How can I see what PhantomJS 'sees'?

    **A**: Within the context of a test case (`@describe`), you can refer to `@page` which is the PhantomJS [WebPage][webpage] object.

    ```coffeescript
    @describe 'snapshot', ->
      @page.render 'home.png'
      @success()
    ```

- **Q**: I don't have `phantomjs` and/or `coffee` on `$PATH`.

    **A**: See `lotte --help` for information on how to specify a path to the missing binary.

    Optionally, you can install the corresponding [NPM] modules [coffee-script] and [phantomjs] in your project.

  [webpage]:   http://code.google.com/p/phantomjs/wiki/Interface#'_WebPage_'_Object
  [NPM]: https://npmjs.org/
  [coffee-script]: https://npmjs.org/package/coffee-script
  [phantomjs]: https://npmjs.org/package/phantomjs

Contributing
------------

The goal of this project is to provide an awesome tool for developers to test their websites or apps in their favourite language with minimum effort.

Commit and code reviews, ideas and documentation improvements are welcomed.

Changelog
---------

- [0.1.2](https://github.com/StanAngeloff/lotte/compare/0.1.1...0.1.2)
- [0.1.2-1](https://github.com/StanAngeloff/lotte/compare/0.1.2...0.1.2-1)
- [0.1.2-2](https://github.com/StanAngeloff/lotte/compare/0.1.2-1...0.1.2-2)
- [0.2.0](https://github.com/StanAngeloff/lotte/compare/0.1.2-2...0.2.0)
- [0.2.0-1](https://github.com/StanAngeloff/lotte/compare/0.2.0...0.2.0-1)
- [0.2.1](https://github.com/StanAngeloff/lotte/compare/0.2.0-1...0.2.1)
- [0.2.2](https://github.com/StanAngeloff/lotte/compare/0.2.1...0.2.2)
- [0.2.2-1](https://github.com/StanAngeloff/lotte/compare/0.2.2...0.2.2-1)
- [0.2.2-2](https://github.com/StanAngeloff/lotte/compare/0.2.2-1...0.2.2-2)
- [0.2.2-3](https://github.com/StanAngeloff/lotte/compare/0.2.2-2...0.2.2-3)
- [0.2.3](https://github.com/StanAngeloff/lotte/compare/0.2.2-3...0.2.3)

### Copyright

> Copyright (c) 2011 Stan Angeloff. See [LICENSE.md](https://github.com/StanAngeloff/lotte/blob/master/LICENSE.md) for details.

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

JavaScript

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言