web端 Monkeytest 工具 gremlins

4,492 阅读2分钟

在网上检索monkey test扑面而来的信息基本都是关于Android命令行工具monkey test的相关内容。那么有没有web端的monkey test工具呢?答案是肯定的。接下来将介绍今天的主角gremlins 一个用JavaScript编写的强大的web端Monkeytest库。

开门见山

其最基本的用法如下,一行代码就可以对当前站点执行monkey test

<script src="path/to/gremlins.min.js"></script>
<script>
gremlins.createHorde().unleash();
</script>

其效果如下图

核心概念

gremlins中有两个核心概念gremlinmogwai

  • gremlin是调皮捣蛋的小精灵专门搞破坏即对当前站点做无规律的click,touche,scroll,表单填充。
  • mogwai是一心向善的小精灵做一些友好的事情比如:防止alert弹窗阻止页面测试,记录FPS,终止执行过久的monkey test

现在我们再看一下gremlins.createHorde().unleash();这行代码的含义createHorde()就是创建一个由gremlinmogwai组成的游击队,unleash()即游击队出击。

gremlins & mogwai 列表

  • clickerGremlin clicks anywhere on the visible area of the document
  • toucherGremlin touches anywhere on the visible area of the document
  • formFillerGremlin fills forms by entering data, selecting options, clicking checkboxes, etc
  • scrollerGremlin scrolls the viewport to reveal another part of the document
  • typerGremlin types keys on the keyboard
  • alertMogwai prevents calls to alert() from blocking the test
  • fpsMogwai logs the number of frames per seconds (FPS) of the browser
  • gizmoMogwai can stop the gremlins when they go too far

使用 gremlin & mogwai

默认情况下horde包含所有的gremlinmogwai你可以根据自己的需要来配置使用那些gremlinmogwai.

下面这段代码就代表着你使用了 formFiler clicker toucher scroller 和一个自定义的 gremlin,但是typer没有包含在其中

  gremlins.createHorde()
  .gremlin(gremlins.species.formFiller())
  .gremlin(gremlins.species.clicker().clickTypes(['click']))
  .gremlin(gremlins.species.toucher())
  .gremlin(gremlins.species.scroller())
  .gremlin(function() {
    window.$ = function() {};
  })
  .unleash();

如果你只是想添加一个自定义的gremlin,可以使用allGremlins

gremlins.createHorde()
  .allGremlins()
  .gremlin(function() {
    window.$ = function() {};
  })
  .unleash();

总结

gremlins是一个强力的web端monkey test库,更多细节大家可以参考官方文档,今天到此为止了之后会出文章介绍一下其原理和实际使用中遇到的问题。

深入阅读推荐