提交 8c99271d 编写于 作者: Huan (李卓桓)'s avatar Huan (李卓桓)

clean unit tests

上级 2ea092b6
/**
* Wechaty - https://github.com/chatie/wechaty
*
* Copyright 2016-2017 Huan LI <zixia@zixia.net>
*
* 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 { test } from 'ava'
import * as sinon from 'sinon'
const sinonTest = require('sinon-test')(sinon)
import PuppetWeb from './puppet-web'
import Bridge from './bridge'
test('PuppetWebBridge smoke testing', sinonTest(async t => {
const browser = sinon.spy()
const mockPuppet = {browser} as any as PuppetWeb
const bridge = new Bridge(mockPuppet, 8788)
t.truthy(bridge, 'Bridge instnace')
}))
......@@ -59,15 +59,13 @@ export class Bridge {
public async init(): Promise<Bridge> {
log.verbose('PuppetWebBridge', 'init()')
return this.inject()
.then(r => {
// log.silly('PuppetWebBridge', 'init() inject() return %s at attempt %d', r, attempt)
try {
await this.inject()
return this
})
.catch(e => {
} catch (e) {
log.silly('PuppetWebBridge', 'init() inject() exception: %s', e && e.message || e)
throw e
})
}
}
public async inject(): Promise<any> {
......
/**
* Wechaty - https://github.com/chatie/wechaty
*
* Copyright 2016-2017 Huan LI <zixia@zixia.net>
*
* 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 { test } from 'ava'
import config from '../config'
import BrowserDriver from './browser-driver'
/**
* WHY force to use SERIAL mode
*
* serial here is because we are checking browser pids inside test.
* if 2 tests run parallel in the same process,
* there will have race conditions for the conflict of `getBrowserPids()`
*/
test.serial('BrowserDriver smoke testing', async t => {
const browserDriver = new BrowserDriver(config.head)
t.truthy(browserDriver, 'BrowserDriver instnace')
await browserDriver.init()
const driver = browserDriver.getWebDriver() // for help function `execute`
t.truthy(driver, 'should get webdriver instance')
await driver.get('https://wx.qq.com/')
t.pass('should open wx.qq.com')
const retAdd = await driverExecute('return 1+1')
t.is(retAdd, 2, 'should return 2 for execute 1+1 in browser')
await browserDriver.quit()
return
//////////////////////////////////
function driverExecute(arg1: any, arg2?: any) {
return driver.executeScript.apply(driver, arguments)
}
})
......@@ -435,3 +435,5 @@ export {
Session,
TargetLocator,
}
export default BrowserDriver
......@@ -19,26 +19,13 @@
import { test } from 'ava'
import {
Bridge,
Browser,
PuppetWeb,
} from '../src/puppet-web/'
/**
* WHY force to use SERIAL mode
*
* serial here is because we are checking browser pids inside test.
* if 2 tests run parallel in the same process,
* there will have race conditions for the conflict of `getBrowserPids()`
*/
test.serial('WebDriver smoke testing', async t => {
const browser = new Browser()
t.truthy(browser, 'Browser instnace')
const mockPuppet = <PuppetWeb>{browser: browser}
const bridge = new Bridge(mockPuppet, 8788)
t.truthy(bridge, 'Bridge instnace')
let pids = await browser.getBrowserPidList()
t.is(pids.length, 0, 'should has no browser process before init()')
......@@ -47,31 +34,16 @@ test.serial('WebDriver smoke testing', async t => {
const driver = browser.driver.getWebDriver() // for help function `execute`
t.truthy(driver, 'should get webdriver instance')
const injectio = bridge.getInjectio()
t.truthy(injectio.length > 10, 'should got injectio script')
await driver.get('https://wx.qq.com/')
t.pass('should open wx.qq.com')
pids = await browser.getBrowserPidList()
t.truthy(pids.length > 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 browser.driver.quit()
pids = await browser.getBrowserPidList()
t.is(pids.length, 0, 'should exist browser process after get()')
return
//////////////////////////////////
function driverExecute(arg1: any, arg2?: any) {
return driver.executeScript.apply(driver, arguments)
}
})
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册