webdriver.spec.ts 3.4 KB
Newer Older
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
1
import path from 'path'
2

3 4 5 6 7 8
import { test } from 'ava'

import {
  Browser
  , By
} from 'selenium-webdriver'
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
9

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
10 11 12 13
import {
  PuppetWeb
  , log
} from '../'
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
14

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
15
// 'use strict'
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
16

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
17 18 19
// const path  = require('path')
// const co    = require('co')
// const test   = require('tape')
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
20

21
// const log   = require('../src/brolog-env')
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
22 23 24 25 26 27 28 29

// const WebDriver = require('selenium-webdriver')
// const Browser = WebDriver.Browser
// const By = WebDriver.By

// const PuppetWebBrowser  = require('../src/puppet-web/browser')
// const PuppetWebBridge   = require('../src/puppet-web/bridge')

30 31 32 33 34
/**
 * WHY USE test.serial
 * 
 * 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 (李卓桓) 已提交
35
 * there will have race conditions for the conflict of `getBrowserPids()`
36 37
 */ 
test.serial('WebDriver process create & quit test', async t => {
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
38 39 40 41 42
  // co(function* () {
    const b = new PuppetWeb.Browser()
    t.truthy(b, 'should instanciate a browser')

    await b.init()
43
    t.pass('should be inited successful')
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
44
    await b.open()
45
    t.pass('should open successful')
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
46

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
47 48
    let pids = await b.getBrowserPids()
    t.truthy(pids.length > 0, 'should exist browser process after b.open()')
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
49 50

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

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
52
    await b.quit()
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
53
    t.pass('quited')
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
54

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
55
    const useAva = false
56 57 58 59 60
    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 (李卓桓) 已提交
61 62 63
  // })
  // .catch(e => t.fail(e))
  // .then(_ => t.end())
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
64

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
65
  // return
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
66 67
})

68
test.serial('WebDriver smoke testing', async t => {
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
69 70
  const wb = new PuppetWeb.Browser()
  t.truthy(wb, 'Browser instnace')
71

72
  const mockPuppet = {browser: wb}
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
73 74
  const bridge = new PuppetWeb.Bridge({puppet: mockPuppet})
  t.truthy(bridge, 'Bridge instnace')
75 76

  var driver // for help function `execute`
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
77

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
78 79 80
  // co(function* () {
    const m = (await wb.getBrowserPids()).length
    t.is(m, 0, 'should has no browser process before get()')
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
81

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
82 83
    driver = await wb.initDriver()
    t.truthy(driver, 'should init driver success')
84 85

    const injectio = bridge.getInjectio()
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
86
    t.truthy(injectio.length > 10, 'should got injectio script')
87

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

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

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
96
    const n = (await wb.getBrowserPids()).length
97
    // console.log(n)
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
98
    // await new Promise((resolve) => {
99 100 101 102
    //   setTimeout(() => {
    //     resolve()
    //   }, 3000)
    // })
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
    t.truthy(n > 0, 'should exist browser process after get()')

    const retAdd = await driverExecute('return 1+1')
    t.is(retAdd, 2, 'should return 2 for execute 1+1 in browser')

    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')

    await wb.quit()
  // })
  // .catch(e => t.fail('promise rejected. e:' + e)) // Rejected
  // .then(_ => {                                    // Finally
  //   wb.quit()
  //   .then(_ => t.end())
  // })
  // .catch(e => t.fail(e))                          // Exception
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
120 121 122 123

  return

  //////////////////////////////////
124
  function driverExecute() {
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
125 126 127
    return driver.executeScript.apply(driver, arguments)
  }
})