uitest.test.ets 18.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/**
 * Copyright (c) 2022 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect } from '@ohos/hypium'
import abilityDelegatorRegistry from '@ohos.application.abilityDelegatorRegistry';
17
import { UiDriver, BY, ResizeDirection, MatchPattern, WindowMode } from '@ohos.uitest'
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
import ability_featureAbility from '@ohos.ability.featureAbility';

const delegator = abilityDelegatorRegistry.getAbilityDelegator();
const bundleName = abilityDelegatorRegistry.getArguments().bundleName;
const waitUiReadyMs = 1000;

async function startAbility(bundleName: string, abilityName: string) {
  await delegator.executeShellCommand(`aa start -b ${bundleName} -a ${abilityName}`).then(result => {
    console.info(`UiTestCase, start abilityFinished: ${result}`)
  }).catch(err => {
    console.error(`UiTestCase, start abilityFailed: ${err}`)
  })
}

async function stopApplication(bundleName: string) {
  await delegator.executeShellCommand(`aa force-stop ${bundleName} `).then(result => {
    console.info(`UiTestCase, stop application finished: ${result}`)
  }).catch(err => {
    console.error(`UiTestCase,stop application failed: ${err}`)
  })
}

export default function abilityTest() {
  describe('UiTestCase', function () {
42
    it('testInputNumber', 0, async function () {
43 44 45 46 47 48 49 50 51 52
      let driver = UiDriver.create()
      await driver.delayMs(waitUiReadyMs)
      let input = await driver.findComponent(BY.type('TextInput'))
      await input.inputText('123')
      await driver.delayMs(waitUiReadyMs)
      let input_new = await driver.findComponent(BY.type('TextInput'))
      let text = await input_new.getText()
      expect(text == '123').assertTrue()
    })

53
    it('testClearText', 0, async function () {
54 55
      let driver = UiDriver.create()
      await driver.delayMs(waitUiReadyMs)
56 57 58 59
      let input1 = await driver.findComponent(BY.type('TextInput'))
      await input1.inputText('abc')
      let input2 = await driver.findComponent(BY.type('TextInput'))
      await input2.clearText()
60 61 62 63 64 65
      await driver.delayMs(waitUiReadyMs)
      let input_new = await driver.findComponent(BY.type('TextInput'))
      let text = await input_new.getText()
      expect(text).assertEqual('')
    })

66
    it('testCheckable', 0, async function () {
67 68 69 70
      let driver = UiDriver.create()
      await driver.delayMs(waitUiReadyMs)
      let button = await driver.findComponent(BY.checkable(true).type('Checkbox'))
      let checkable = await button.isCheckable()
71
      expect(checkable == true).assertTrue()
72 73
    })

74
    it('testChecked', 0, async function () {
75 76 77 78
      let driver = UiDriver.create()
      await driver.delayMs(waitUiReadyMs)
      let button = await driver.findComponent(BY.checked(false).type('Checkbox'))
      let checked = await button.isChecked()
79 80 81 82 83 84 85 86 87 88 89 90 91 92
      expect(checked == false).assertTrue()
    })

    it('testMatchPattren', 0, async function () {
      let driver = UiDriver.create()
      await driver.delayMs(waitUiReadyMs)
      let Button1 = await driver.findComponent(BY.text('next page',MatchPattern.EQUALS))
      expect(await Button1.getText() == 'next page').assertTrue()
      let Button2 = await driver.findComponent(BY.text('next',MatchPattern.STARTS_WITH))
      expect(await Button2.getText() == 'next page').assertTrue()
      let Button3 = await driver.findComponent(BY.text('page',MatchPattern.ENDS_WITH))
      expect(await Button3.getText() == 'next page').assertTrue()
      let Button4 = await driver.findComponent(BY.text('ext',MatchPattern.CONTAINS))
      expect(await Button4.getText() == 'next page').assertTrue()
93 94
    })

95
    it('testClick', 0, async function () {
96 97
      let driver = UiDriver.create()
      await driver.delayMs(waitUiReadyMs)
98 99 100
      let Button = await driver.findComponent(BY.text('next page'))
      let center = await Button.getBoundsCenter()
      await driver.click(center.X, center.Y)
101 102 103 104 105 106 107
      await driver.delayMs(waitUiReadyMs)
      let newButton = await driver.findComponent(BY.text('back to index'))
      let text = await newButton.getText()
      expect(text == 'back to index').assertTrue()
      await newButton.click()
    })

108 109 110 111 112 113 114 115 116 117 118 119 120 121
    it('testDoubleClick', 0, async function () {
      let driver = UiDriver.create()
      await driver.delayMs(waitUiReadyMs)
      let Button = await driver.findComponent(BY.text('Click twice'))
      let center = await Button.getBoundsCenter()
      await driver.doubleClick(center.X, center.Y)
      await driver.delayMs(waitUiReadyMs)
      let button = await driver.findComponent(BY.text('doubleClick'))
      let text = await button.getText()
      expect(text == 'doubleClick').assertTrue()
      await button.click()
    })

    it('testLongClick', 0, async function () {
122 123
      let driver = UiDriver.create()
      await driver.delayMs(waitUiReadyMs)
124 125 126
      let Button = await driver.findComponent(BY.text('next page'))
      let center = await Button.getBoundsCenter()
      await driver.longClick(center.X, center.Y)
127 128 129 130 131 132 133
      await driver.delayMs(waitUiReadyMs)
      let newButton = await driver.findComponent(BY.text('longClick'))
      let text = await newButton.getText()
      expect(text == 'longClick').assertTrue()
      await newButton.click()
    })

134
    it('testUiComponentClick', 0, async function () {
135 136
      let driver = UiDriver.create()
      await driver.delayMs(waitUiReadyMs)
137 138 139 140 141 142 143
      let button = await driver.findComponent(BY.text('next page'))
      await button.click()
      await driver.delayMs(waitUiReadyMs)
      let newButton = await driver.findComponent(BY.text('back to index'))
      let text = await newButton.getText()
      expect(text == 'back to index').assertTrue()
      await newButton.click()
144 145
    })

146
    it('testUiComponentDoubleClick', 0, async function () {
147 148
      let driver = UiDriver.create()
      await driver.delayMs(waitUiReadyMs)
149 150
      let button = await driver.findComponent(BY.text('Click twice'))
      await button.doubleClick()
151
      await driver.delayMs(waitUiReadyMs)
152
      let newButton = await driver.findComponent(BY.text('doubleClick'))
153
      let text = await newButton.getText()
154
      expect(text == 'doubleClick').assertTrue()
155 156 157
      await newButton.click()
    })

158
    it('testUiComponentLongClick', 0, async function () {
159 160 161 162 163 164 165 166 167 168 169
      let driver = UiDriver.create()
      await driver.delayMs(waitUiReadyMs)
      let button = await driver.findComponent(BY.text('next page'))
      await button.longClick()
      await driver.delayMs(waitUiReadyMs)
      let newButton = await driver.findComponent(BY.text('longClick'))
      let text = await newButton.getText()
      expect(text == 'longClick').assertTrue()
      await newButton.click()
    })

170
    it('testKey', 0, async function () {
171 172
      let driver = UiDriver.create()
      await driver.delayMs(waitUiReadyMs)
173 174
      let button = await driver.findComponent(BY.key('my-key'))
      expect(await button.getKey() == 'my-key').assertTrue()
175 176
    })

177
    it('testId', 0, async function () {
178 179 180
      let driver = UiDriver.create()
      await driver.delayMs(waitUiReadyMs)
      let button = await driver.findComponent(BY.text('next page'))
181 182 183
      let id = await button.getId()
      let button2 = await driver.findComponent(BY.id(id))
      expect(await button.getText() == 'next page').assertTrue()
184 185
    })

186 187 188 189 190 191 192 193 194
    it('testGetType', 0, async function () {
      let driver = UiDriver.create()
      await driver.delayMs(waitUiReadyMs)
      let text = await driver.findComponent(BY.type('Text'))
      let type = await text.getType()
      expect(type == 'Text').assertTrue()
    })

    it('testClickable', 0, async function () {
195 196 197 198
      let driver = UiDriver.create()
      await driver.delayMs(waitUiReadyMs)
      let button = await driver.findComponent(BY.text('next page').clickable(false))
      let clickable = await button.isClickable()
199
      expect(clickable == false).assertTrue()
200 201
    })

202
    it('testLongClickable', 0, async function () {
203 204 205 206
      let driver = UiDriver.create()
      await driver.delayMs(waitUiReadyMs)
      let button = await driver.findComponent(BY.text('next page').longClickable(false))
      let longClickable = await button.isLongClickable()
207
      expect(longClickable== false).assertTrue()
208 209
    })

210
    it('testScrollable', 0, async function () {
211 212 213 214
      let driver = UiDriver.create()
      await driver.delayMs(waitUiReadyMs)
      let scrollBar = await driver.findComponent(BY.type('Scroll').scrollable(false))
      let scrollable = await scrollBar.isScrollable()
215
      expect(scrollable == false).assertTrue()
216 217
    })

218
    it('testEnabled', 0, async function () {
219 220 221 222
      let driver = UiDriver.create()
      await driver.delayMs(waitUiReadyMs)
      let button = await driver.findComponent(BY.text('next page').enabled(true))
      let enable = await button.isEnabled()
223
      expect(enable == true).assertTrue()
224 225
    })

226
    it('testFocused', 0, async function () {
227 228 229 230
      let driver = UiDriver.create()
      await driver.delayMs(waitUiReadyMs)
      let button = await driver.findComponent(BY.text('next page').focused(false))
      let focused = await button.isFocused()
231
      expect(focused == false).assertTrue()
232 233
    })

234
    it('testSelected', 0, async function () {
235 236 237 238
      let driver = UiDriver.create()
      await driver.delayMs(waitUiReadyMs)
      let button = await driver.findComponent(BY.text('next page').selected(false))
      let selected = await button.isSelected()
239
      expect(selected == false).assertTrue()
240 241
    })

242
    it('testScrollToBottom', 0, async function () {
243 244 245 246 247 248 249 250 251
      let driver = UiDriver.create()
      await driver.delayMs(waitUiReadyMs)
      let scrollBar = await driver.findComponent(BY.type('Scroll'))
      expect(scrollBar != null).assertTrue()
      await scrollBar.scrollToBottom()
      let button = await driver.findComponent(BY.text('bottom'))
      expect(await button.getText() == 'bottom').assertTrue()
    })

252
    it('testScrollToTop', 0, async function () {
253 254 255 256
      let driver = UiDriver.create()
      await driver.delayMs(waitUiReadyMs)
      let scrollBar = await driver.findComponent(BY.type('Scroll'))
      expect(scrollBar !== null).assertTrue()
257
      await scrollBar.scrollToBottom()
258 259 260 261 262
      await scrollBar.scrollToTop()
      let button = await driver.findComponent(BY.text('next page'))
      expect(await button.getText() == 'next page').assertTrue()
    })

263
    it('testAssertComponentExist', 0, async function () {
264 265 266 267 268
      let driver = UiDriver.create()
      await driver.delayMs(waitUiReadyMs)
      await driver.assertComponentExist(BY.text('next page'))
    })

269
    it('testPressBack', 0, async function () {
270 271 272 273 274 275 276 277 278 279 280
      let driver = UiDriver.create()
      await driver.delayMs(waitUiReadyMs)
      let button = await driver.findComponent(BY.text('next page'))
      await button.click()
      await driver.delayMs(waitUiReadyMs)
      await driver.pressBack()
      await driver.delayMs(waitUiReadyMs)
      let button_ori = await driver.findComponent(BY.text('next page'))
      expect(await button_ori.getText() == 'next page').assertTrue()
    })

281
    it('testFindComponents', 0, async function () {
282 283 284 285 286 287
      let driver = UiDriver.create()
      await driver.delayMs(waitUiReadyMs)
      let buttons = await driver.findComponents(BY.type('Button'))
      expect(await buttons[0].getText() != null).assertTrue()
    })

288
    it('testTriggerKey', 0, async function () {
289 290 291 292 293
      let driver = UiDriver.create()
      await driver.delayMs(waitUiReadyMs)
      let button = await driver.findComponent(BY.text('next page'))
      await button.click()
      await driver.delayMs(waitUiReadyMs)
294 295
      let keyBack = 2
      await driver.triggerKey(keyBack)
296 297 298 299 300
      await driver.delayMs(waitUiReadyMs)
      let button_ori = await driver.findComponent(BY.text('next page'))
      expect(await button_ori.getText() == 'next page').assertTrue()
    })

301
    it('testGetUiComponentBounds', 0, async function () {
302 303
      let driver = UiDriver.create()
      await driver.delayMs(waitUiReadyMs)
304
      let text = await driver.findComponent(BY.text('next page'))
305 306 307 308 309 310 311
      expect(text !== null).assertTrue()
      let bounds = await text.getBounds();
      expect(bounds !== null).assertTrue()
      expect(bounds.rightX).assertLarger(bounds.leftX)
      expect(bounds.bottomY).assertLarger(bounds.topY)
    })

312
    it('testGetUiComponentBoundsCenter', 0, async function () {
313 314 315 316
      let driver = UiDriver.create()
      await driver.delayMs(waitUiReadyMs)
      let button = await driver.findComponent(BY.text('next page'))
      let point = await button.getBoundsCenter()
317
      let bounds = await button.getBounds()
318
      expect(point!== null).assertTrue()
319 320
      expect(point.Y == (bounds.bottomY + bounds.topY)/2).assertTrue()
      expect(point.X == (bounds.rightX + bounds.leftX)/2).assertTrue()
321 322
    })

323
    it('testWaitForComponent', 0, async function () {
324 325 326 327 328 329
      let driver = UiDriver.create()
      await driver.delayMs(waitUiReadyMs)
      let button = await driver.waitForComponent(BY.text('next page'), waitUiReadyMs)
      expect(button !== null).assertTrue()
    })

330 331 332 333 334 335 336 337 338
    it('testScreenCap', 0, async function () {
      let driver = UiDriver.create()
      await driver.delayMs(waitUiReadyMs)
      let savePath = '/data/local/tmp/1.png'
      let success = await driver.screenCap(savePath)
      expect(success == true).assertTrue()
    })

    it('testIsBefore', 0, async function () {
339 340
      let driver = UiDriver.create()
      await driver.delayMs(waitUiReadyMs)
341 342
      let button = await driver.findComponent(BY.isBefore(BY.text('Click twice')).type('Button'))
      expect(await button.getType() == 'Button').assertTrue()
343 344
    })

345
    it('testIsAfter', 0, async function () {
346 347
      let driver = UiDriver.create()
      await driver.delayMs(waitUiReadyMs)
348 349
      let button = await driver.findComponent(BY.isAfter(BY.text('next page')).type('Text'))
      expect(await button.getText() == 'Click twice').assertTrue()
350 351
    })

352
    it('testScrollSearch', 0, async function () {
353 354
      let driver = UiDriver.create()
      await driver.delayMs(waitUiReadyMs)
355 356 357 358 359
      let scrollBar = await driver.findComponent(BY.type('Scroll'))
      let text = await scrollBar.scrollSearch(BY.text('4'))
      await driver.delayMs(waitUiReadyMs)
      expect(text != null).assertTrue()
      await scrollBar.scrollToTop()
360 361
    })

362 363 364 365 366 367 368 369 370 371 372
    it('testGetWindowMode', 0, async function () {
      await startAbility('com.example.windows', 'MainAbility')
      let driver = UiDriver.create()
      await driver.delayMs(waitUiReadyMs)
      let window = await driver.findWindow({bundleName:'com.example.windows'})
      let mode = await window.getWindowMode()
      expect(mode == WindowMode.FULLSCREEN).assertTrue()
      await stopApplication('com.example.windows')
    })

    it('testWindowMoveTo', 0, async function () {
373 374 375 376 377 378 379
      await startAbility('com.example.windows', 'MainAbility')
      let driver = UiDriver.create()
      await driver.delayMs(waitUiReadyMs)
      let window = await driver.findWindow({bundleName:'com.example.windows'})
      await window.resume()
      await driver.delayMs(waitUiReadyMs)
      let window1 = await driver.findWindow({bundleName:'com.example.windows'})
380
      let bounds1 = await window1.getBounds()
381 382 383
      await window1.moveTo(100,100)
      await driver.delayMs(waitUiReadyMs)
      let window2 = await driver.findWindow({bundleName:'com.example.windows'})
384 385
      let bounds2 = await window2.getBounds()
      expect(bounds1 != bounds2).assertTrue()
386
      await stopApplication('com.example.windows')
387
    })
388

389
    it('testWindowResize', 0, async function () {
390 391 392 393
      await startAbility('com.example.windows', 'MainAbility')
      let driver = UiDriver.create()
      await driver.delayMs(waitUiReadyMs)
      let window = await driver.findWindow({bundleName:'com.example.windows'})
394
      await window.resume()
395 396 397 398 399
      await driver.delayMs(waitUiReadyMs)
      let window1 = await driver.findWindow({bundleName:'com.example.windows'})
      let bounds1 = await window1.getBounds()
      await window1.resize(400,400,ResizeDirection.RIGHT_DOWN)
      await driver.delayMs(waitUiReadyMs)
400
      let window2 = await driver.findWindow({bundleName:'com.example.windows'})
401 402
      let bounds2 = await window2.getBounds()
      expect(bounds1 != bounds2).assertTrue()
403
      await stopApplication('com.example.windows')
404 405
    })

406
    it('testWindowFocus', 0, async function () {
407 408 409 410
      await startAbility('com.example.windows', 'MainAbility')
      let driver = UiDriver.create()
      await driver.delayMs(waitUiReadyMs)
      let window = await driver.findWindow({bundleName:'com.example.windows'})
411 412 413
      await window.focus()
      let IsActive = await window.focus()
      expect(IsActive == true).assertTrue()
414
      await stopApplication('com.example.windows')
415 416
    })

417
    it('testWindowMaximize', 0, async function () {
418 419 420 421
      await startAbility('com.example.windows', 'MainAbility')
      let driver = UiDriver.create()
      await driver.delayMs(waitUiReadyMs)
      let window = await driver.findWindow({bundleName:'com.example.windows'})
422 423 424 425 426 427 428 429
      await window.resume()
      let window2 = await driver.findWindow({bundleName:'com.example.windows'})
      await window2.maximize()
      await driver.delayMs(waitUiReadyMs)
      let window3 = await driver.findWindow({bundleName:'com.example.windows'})
      let mode = await window3.getWindowMode()
      expect(mode == WindowMode.FULLSCREEN).assertTrue()
      await stopApplication('com.example.windows')
430 431
    })

432
    it('testWindowMinimize', 0, async function () {
433 434 435 436
      await startAbility('com.example.windows', 'MainAbility')
      let driver = UiDriver.create()
      await driver.delayMs(waitUiReadyMs)
      let window = await driver.findWindow({bundleName:'com.example.windows'})
437 438 439 440
      await window.minimize()
      await driver.delayMs(waitUiReadyMs)
      let button = await driver.findComponent(BY.text('Clock'))
      expect(button != null).assertTrue()
441
      await stopApplication('com.example.windows')
442 443
    })

444 445 446 447 448 449 450 451 452
    it('testWindowClose', 0, async function () {
      await startAbility('com.example.windows', 'MainAbility')
      let driver = UiDriver.create()
      await driver.delayMs(waitUiReadyMs)
      let window = await driver.findWindow({bundleName:'com.example.windows'})
      await window.close()
      await driver.delayMs(waitUiReadyMs)
      let button = await driver.findComponent(BY.text('Clock'))
      expect(button != null).assertTrue()
453 454
    })

455
  })
456

457
}