uniapp-cli-project.md 7.2 KB
Newer Older
1
## uni-app cli项目@uniapp-cli
W
wan201809 已提交
2 3 4

> 本文档仅适用于CLI工程。

study夏羽's avatar
study夏羽 已提交
5 6
创建 `cli` 工程,详见[https://uniapp.dcloud.io/quickstart-cli.html](https://uniapp.dcloud.io/quickstart-cli.html)

W
wan201809 已提交
7 8 9 10 11 12

如果之前是HBuilderX工程,则把HBuilderX工程内的文件(除 unpackage、node_modules 目录)拷贝至 vue-cli 工程的 src 目录。
在 vue-cli 工程内重新安装 npm 依赖(如果之前使用了 npm 依赖的话)

cli创建项目时若选择`hello uni-app`模板,可看到其中已经自带部分测试例。

study夏羽's avatar
study夏羽 已提交
13 14 15 16 17 18 19 20
注意:相关依赖不能高于以下版本

```
jest@27.0.4
jest-environment-node@27.5.1

```

W
wan201809 已提交
21 22 23 24 25 26 27
已有 `cli` 工程
1. 更新依赖包 `@dcloudio/*` >= `2.0.0-alpha-27920200613002`
2. 安装依赖包 `@dcloudio/uni-automator`
```
npm install @dcloudio/uni-automator --save-dev
```
3. package.json script节点新增命令
雪洛's avatar
雪洛 已提交
28 29

vue2项目新增如下:
W
wan201809 已提交
30 31 32 33 34 35 36 37
```
"test:h5": "cross-env UNI_PLATFORM=h5 jest -i",
"test:android": "cross-env UNI_PLATFORM=app-plus UNI_OS_NAME=android jest -i",
"test:ios": "cross-env UNI_PLATFORM=app-plus UNI_OS_NAME=ios jest -i",
"test:mp-weixin": "cross-env UNI_PLATFORM=mp-weixin jest -i",
"test:mp-baidu": "cross-env UNI_PLATFORM=mp-baidu jest -i"
```

雪洛's avatar
雪洛 已提交
38 39 40 41 42 43 44 45 46
vue3项目新增如下:
```
"test:h5": "cross-env UNI_PLATFORM=h5 jest -i",
"test:android": "cross-env UNI_PLATFORM=app UNI_OS_NAME=android jest -i",
"test:ios": "cross-env UNI_PLATFORM=app UNI_OS_NAME=ios jest -i",
"test:mp-weixin": "cross-env UNI_PLATFORM=mp-weixin jest -i",
"test:mp-baidu": "cross-env UNI_PLATFORM=mp-baidu jest -i"
```

47
### H5平台测试流程
W
wan201809 已提交
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77

1. 进入工程目录,安装依赖
```
npm install puppeteer --save-dev
```
注意: 从v3.0.0开始,`Puppeteer` 开始依赖于Node 10.18.1+


2. 根据API编写测试的js代码,参考测试用例
API文档见:[https://uniapp.dcloud.io/collocation/auto/api](https://uniapp.dcloud.io/collocation/auto/api)
测试文件目录配置见 [jest.config.js](collocation/auto/quick-start?id=jestconfigjs)

3. 运行测试
```
npm run test:h5
```

4. 测试结果
```
>> cross-env UNI_PLATFORM=h5 jest -i
...
Test Suites: 1 passed, 1 total
Tests:       4 passed, 4 total
Snapshots:   0 total
Time:        14.995s, estimated 16s
```

更多配置参考 [jest.config.js](collocation/auto/quick-start?id=jestconfigjs)


78
### App-Android测试流程
W
wan201809 已提交
79 80 81

1. 配置全局 `adb` 环境变量

雪洛's avatar
雪洛 已提交
82
2. 配置 `Hbuilder` 调试基座/自定义基座 `android_base.apk` 目录,参考 [jest.config.js](collocation/auto/quick-start?id=jestconfigjs)
W
wan201809 已提交
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97

3. 创建 `cli` 工程/现有 `cli` 工程
切换到工程目录,安装依赖包 `adbkit`
```
npm install adbkit --save-dev
```

4. 编写测试代码,参考测试用例

5. 运行测试
```
npm run test:android
```


98
### App-iOS测试流程
W
wan201809 已提交
99 100 101 102 103 104 105 106

目前仅支持 iOS 模拟器(需要mac电脑安装xcode)

1. 安装依赖 `node-simctl`
```
npm install node-simctl --save-dev
```

雪洛's avatar
雪洛 已提交
107
2. 配置模拟器id,参考 [jest.config.js](collocation/auto/quick-start?id=jestconfigjs)
W
wan201809 已提交
108

雪洛's avatar
雪洛 已提交
109
3. 配置 `Hbuilder` 调试基座/自定义基座 `Pandora_simulator.app` 目录,参考 [jest.config.js](collocation/auto/quick-start?id=jestconfigjs)
W
wan201809 已提交
110 111 112 113 114 115 116 117 118 119

4. 编写测试代码,参考测试用例

5. 运行测试
```
npm run test:ios
```



120
### 微信小程序测试流程
W
wan201809 已提交
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139

1. 创建cli项目,同H5平台 (必须配置微信小程序 appid, manifest.json -> mp-weixin -> appid)

2. 运行测试(如果微信开发者工具无法成功打开项目,请手动打开)
```
npm run test:mp-weixin
```

3. 测试结果
```
> cross-env NODE_ENV=development UNI_PLATFORM=mp-weixin vue-cli-service uni-build --watch "--auto-port" "9520"
Test Suites: 1 passed, 1 total
Tests:       4 passed, 4 total
Snapshots:   0 total
Time:        14.995s, estimated 16s
```



140
### 测试示例
W
wan201809 已提交
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234

使用 hello uni-app 工程测试 H5 平台

1. 创建 `cli` 项目,选择 `hello uni-app`
```
$ vue create -p dcloudio/uni-preset-vue#alpha my-hello-uni-app
# 进入项目目录
$ cd my-hello-uni-app
```

2. 安装 `puppeteer`
```
npm install puppeteer
```

3. 创建测试文件 `src/pages/tabBar/component/component.test.js`,复制下面代码
```
describe('pages/tabBar/component/component.nvue', () => {
    let page
    beforeAll(async () => {
        // 重新reLaunch至首页,并获取首页page对象(其中 program 是uni-automator自动注入的全局对象)
        page = await program.reLaunch('/pages/tabBar/component/component')
        await page.waitFor(1000)
    })

    it('u-link', async () => {
        // 检测首页u-link的文本内容
        expect(await (await page.$('.hello-link')).text()).toBe('https://uniapp.dcloud.io/component/')
    })

    it('视图容器', async () => {
        // 检测首个 panel 是视图容器
        expect(await (await page.$('.uni-panel-text')).text()).toBe('视图容器')
        // 检测首个 panel 切换展开
        const panelH = await page.$('.uni-panel-h');
        // 不能做完全匹配,百度小程序会生成额外的class
        expect(await panelH.attribute('class')).toContain('uni-panel-h')
        await panelH.tap()
        await page.waitFor(500)
        // 已展开
        expect(await panelH.attribute('class')).toContain('uni-panel-h-on')
    })

    it('.uni-panel', async () => {
      const lists = await page.$$('.uni-panel')
      expect(lists.length).toBe(9)
    })

    it('.uni-panel action', async () => {
      const listHead = await page.$('.uni-panel-h')
      expect(await listHead.attribute('class')).toContain('uni-panel-h-on')
      await listHead.tap()
      await page.waitFor(200)
      expect(await listHead.attribute('class')).toContain(
        'uni-panel-h',
      )

      // 展开第一个 panel,点击第一个 item,验证打开的新页面是否正确
      await listHead.tap()
      await page.waitFor(200)
      const item = await page.$('.uni-navigate-item')
      await item.tap()
      await page.waitFor(500)
      expect((await program.currentPage()).path).toBe('pages/component/view/view')
      await page.waitFor(500)

      // 执行 navigateBack 验证是否返回
      expect((await program.navigateBack()).path).toBe('pages/tabBar/component/component')
    })
})
```

4. 运行测试
```
npm run test:h5
```

5. 测试结果
```
> cross-env UNI_PLATFORM=h5 jest -i
 PASS  src/pages/tabBar/component/component.test.js (14.789s)
  pages/tabBar/component/component.nvue
    √ u-link (8ms)
    √ 视图容器 (518ms)
    √ .uni-panel (2ms)
    √ .uni-panel action (4447ms)
Test Suites: 1 passed, 1 total
Tests:       4 passed, 4 total
Snapshots:   0 total
Time:        14.995s, estimated 16s
```



235
#### 屏幕截图示例
W
wan201809 已提交
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
```
describe('pages/API/set-navigation-bar-title/set-navigation-bar-title.vue', () => {
    let page
    beforeAll(async () => {
        // 重新reLaunch至首页,并获取首页page对象(其中 program 是uni-automator自动注入的全局对象)
        page = await program.reLaunch('/pages/API/set-navigation-bar-title/set-navigation-bar-title')
        await page.waitFor(3000)
    })

    it('.uni-hello-text', async () => {
      var image = await program.screenshot({
        path: "set-navigation-bar-title.png"  // 默认项目根目录
      })
      console.log(image)
    })
})
```

更多测试示例见: hello uni-app

256
GitHub: [https://github.com/dcloudio/hello-uniapp](https://github.com/dcloudio/hello-uniapp)