webdriver.spec.ts 2.7 KB
Newer Older
1
import { test }   from 'ava'
2

3 4 5 6
// import {
//   Browser
//   , By
// }                 from 'selenium-webdriver'
7 8

import {
9 10 11 12
    Bridge  as PuppetWebBridge
  , Browser as PuppetWebBrowser
  , PuppetWeb
} from '../src/puppet-web/'
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
13

14 15
/**
 * WHY USE test.serial
16
 *
17 18
 * serial here is because we are checking browser pids inside test.
 * if 2 tests run parallel in the same process,
Huan (李卓桓)'s avatar
doc  
Huan (李卓桓) 已提交
19
 * there will have race conditions for the conflict of `getBrowserPids()`
20
 */
21
test.serial('WebDriver process create & quit test', async t => {
22
    const b = new PuppetWebBrowser()
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
23 24 25
    t.truthy(b, 'should instanciate a browser')

    await b.init()
26
    t.pass('should be inited successful')
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
27
    await b.open()
28
    t.pass('should open successful')
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
29

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
30 31
    let pids = await b.getBrowserPids()
    t.truthy(pids.length > 0, 'should exist browser process after b.open()')
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
32 33

// console.log(b.driver.getSession())
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
34

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
35
    await b.quit()
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
36
    t.pass('quited')
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
37

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
38
    const useAva = false
39 40 41 42 43
    if (!useAva) { // ava will run tests concurency...
      pids = await b.getBrowserPids()
      t.is(pids.length, 0, 'no driver process after quit')
    }

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
44 45
})

46
test.serial('WebDriver smoke testing', async t => {
47
  const wb = new PuppetWebBrowser()
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
48
  t.truthy(wb, 'Browser instnace')
49

50 51
  const mockPuppet = <PuppetWeb>{browser: wb}
  const bridge = new PuppetWebBridge(mockPuppet, 8788)
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
52
  t.truthy(bridge, 'Bridge instnace')
53

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
54
  let driver // for help function `execute`
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
55

56 57
  const m = (await wb.getBrowserPids()).length
  t.is(m, 0, 'should has no browser process before get()')
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
58

59 60
  driver = await wb.initDriver()
  t.truthy(driver, 'should init driver success')
61

62 63
  const injectio = bridge.getInjectio()
  t.truthy(injectio.length > 10, 'should got injectio script')
64

65 66 67 68
  // XXX: if get rid of this dummy,
  // driver.get() will fail due to cant start phantomjs process
  // 20160828 fixed in new version of selenium webdriver
  // await Promise.resolve()
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
69

70 71
  await driver.get('https://wx.qq.com/')
  t.pass('should open wx.qq.com')
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
72

73 74 75 76 77 78 79 80
  const n = (await wb.getBrowserPids()).length
  // console.log(n)
  // await new Promise((resolve) => {
  //   setTimeout(() => {
  //     resolve()
  //   }, 3000)
  // })
  t.truthy(n > 0, 'should exist browser process after get()')
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
81

82 83
  const retAdd = await driverExecute('return 1+1')
  t.is(retAdd, 2, 'should return 2 for execute 1+1 in browser')
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
84

85 86 87
  const retInject = await driverExecute(injectio, 8788)
  t.truthy(retInject, 'should return a object contains status of inject operation')
  t.is(retInject.code, 200, 'should got code 200 for a success wechaty inject')
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
88

89
  await wb.quit()
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
90 91 92 93

  return

  //////////////////////////////////
94
  function driverExecute(arg1: any, arg2?: any) {
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
95 96 97
    return driver.executeScript.apply(driver, arguments)
  }
})