0.2.1 • Published 5 years ago

uitester-puppeteer v0.2.1

Weekly downloads
1
License
ISC
Repository
github
Last release
5 years ago

uitester-puppeteer

基于puppeteer的UI自动化测试

使用

安装

npm i uitester-puppeteer -S

具体使用

const Tester = require('uitester-puppeteer');
const cases = [
  // ...
];
const options = {
  // ...
};
let tester = new Tester(cases, options);

tester.on('start', () => {});
tester.on('describe_start', (describe) => {});
tester.on('describe_done', (describe) => {});
tester.on('action_start', (action) => {});
tester.on('action_done', (action) => {});
tester.on('it_start', (it) => {});
tester.on('it_done', (it) => {});
tester.on('done', (result) => {});
tester.on('fail', (result) => {});
tester.on('complete', (result) => {});

cases 示例

以下示例模拟登陆,并检查是否登录成功

const cases = [
  {
    describe: '全局测试',
    newBrowser: false,
    newPage: false,
    actions: [
      {
        name: '打开环境',
        value: 'https://xxx',
        action: 'open',
        waitForAfter: 1000,
        screenshot: true
      },
      {
        name: '填充用户名',
        action: 'type',
        selector: '#username',
        value: 'admin'
      },
      {
        name: '填充密码',
        action: 'type',
        selector: '#password',
        value: '123456'
      },
      {
        name: '提交',
        action: 'click',
        selector: '#submit',
        waitForAfter: 1000
      },
    ],
    it: [{
      name: '测试验证',
      selector: '#content',
      condition: 'hasText'
    }],
    afterIt: [
        // 同actions
    ]
  }
];

options

属性说明类型默认值
global全局变量,可供测试用例中的 action、it、afterIt 的 value 使用map-
dev决定是否打开调试日志、是否headlessbooleanfalse
sandBox是否沙盒模式booleanfalse
clientWidth默认视口宽度number1024
clientHeight默认视口高度number768
screenshotPrePath截图存放目录,需要保证其写入权限没问题。目录不存在,会自动创建。string

describle

字段解释
newBrowser一般连续写多个 describe,而且后面的 describe 依赖前面的 describe (比如在之前的 action 中本地记录过cookie、localStorage、sessionStorage),所以默认一个完整测试中,新的 describe 不重新打开浏览器。反之,将会销毁之前的浏览器,重新打开浏览器。
newPage一般连续写多个 describe,而且后面的 describe 依赖前面的 describe (比如在之前的 action 中已经登陆过,进入到了某个页面),所以默认一个完整测试中,新的 describe 不重新打开新的页面。反之,将会关闭之前的page,重新打开一个新的page(这时候往往需要重新 open 一个 url)
actions执行的动作
it执行的验证(只有一个验证条件,建议都用 its 的方式)
its执行的验证(数组,有多个验证条件)
afterIt数据格式和actions是一样的,其作用一般是用来清除前面actions带来的副作用。比如,测试过程中建立了一条数据,我们就可以在afterIt里面定义action,把这条数据删除掉。

actions && afterIt字段定义

字段解释
name动作名称-
value动作可能涉及到的内容,比如打开的url、填充的值,支持引用和模拟-
action执行动作,后续会逐步扩展详情见action管理表格
selector选择器支持css选择器和xpath
screenshot产品报告中是否截图(初始化不传screenshotPrePath,则不会截图)默认false
waitFor等待当前selector出现超时时间,ms,默认1000ms
waitBefore动作前延时ms
waitAfter动作后延时ms
delay延时ms,type输入的时候会用到
frameName测试对象切换到iframeiframe的name属性值
frameUrl测试对象切换到iframeiframe的src属性值
frameTitle测试对象切换到iframeiframe的title属性值

当action===frame的时候,可以通过iframe的 frameName、frameUrl、frameTitle属性来定位(只需要一个字段即可,frameName > frameUrl > frameTitle)

action管理

name说明
open打开一个页面
click点击一个元素
type表单输入
frame切换到 iframe
mainframe(切换到 iframe 之后)切换到父窗口
setCookie设置 cookie
script执行原生脚本, string: script 或者 promise: fun
screenshot仅截图

setCookie时,其value示例:

{
  name: string,
  value: string,
  url: string,
  domain: string,
  path: string,
  expires: string|number,
  httpOnly: boolean,
  secure: boolean,
  sameSite: boolean
}

it字段定义

字段说明类型
name验证器名称string
selector选择器string
condition条件, 参考下边 “it.condition字段定义”string
value用来做判断的值,支持引用和模拟boolean | string | number
screenshot是否截图false

it.condition字段定义:

name说明对应value类型
isEmpty无innerTextboolean
isEmptyHtml无innerHTMLboolean
isNotEmpty有innerTextboolean
isNotEmptyHtml有innerHTMLboolean
equalinnerText全等boolean
equalHtmlinnerHTML全等boolean
notEqualinnerText不全等boolean
startWithinnerText以value开始string
endWithinnerText中以value结束string
notStartWithinnerText不以value开始string
notEndWithinnerText中不以value结束string
containinnerText中含有valuestring
notContaininnerText中不含有valuestring
countEqualselectors的长度为valuestring
countNotEqualselectors的长度不为valuestring
countMoreselectors的长度大于valuenumber
countLessselectors的长度小于valuenumber

value 值的引用和模拟

0.1.0 版本开始,测试用例中的 action、it、afterIt 中的 value 值支持引用和模拟。

而被引用的 global 里面的值也可以被模拟。

引用

引用是指从 global 对象中进行引用。相关模板字符串为:'<global:VARIABLE>'

如果 VARIABLE 变量在 global 中不存在,则返回空字符串。

示例:

const Tester = require('uitester-puppeteer');
const cases = [
  {
    describe: '全局测试',
    newBrowser: false,
    newPage: false,
    actions: [
      {
        name: '填充用户名',
        value: '<global:username>',
        action: 'open',
        waitForAfter: 1000,
        screenshot: true
      },
      ...
    ],
    ...
  }
];
const options = {
  global: {
    username: 'admin'
  },
  ...
};
let tester = new Tester(cases, options);

模拟

数据的模拟基于 mock.js。相关模板字符串为:'<mock:@PLACEHOLDER>'。PLACEHOLDER 为 mock.js 的占位符。

模板字符串中的 @占位符Mock.Random 中的方法一一对应

Mock.random 子方法:

TypeMethod
Basicboolean, natural, integer, float, character, string, range, date, time, datetime, now
Imageimage, dataImage
Colorcolor
Textparagraph, sentence, word, title, cparagraph, csentence, cword, ctitle
Namefirst, last, name, cfirst, clast, cname
Weburl, domain, email, ip, tld
Addressregion, province, city, county, zip
Miscellaneousguid, id

示例:

const Tester = require('uitester-puppeteer');
const cases = [
  {
    describe: '全局测试',
    newBrowser: false,
    newPage: false,
    actions: [
      {
        name: '填充任务 ID',
        value: '<global:taskId>',
        action: 'type',
        selector: '#taskId'
      },
      {
        name: '填充邮箱',
        value: '<mock:@email>',
        action: 'type',
        selector: '#email'
      },
      ...
    ],
    ...
  }
];
const options = {
  global: {
    taskId: '<mock:@guid>'
  },
  ...
};
let tester = new Tester(cases, options);
0.2.1

5 years ago

0.2.0

5 years ago

0.1.0

5 years ago

0.0.6

5 years ago

0.0.5

5 years ago

0.0.4

5 years ago

0.0.3

5 years ago

0.0.2

5 years ago

0.0.1

5 years ago

1.0.0

5 years ago