diff --git a/package.json b/package.json index 2a31e513cc7e351ba2f4e02c508dad02dd51a142..bf662e57333fc300d77b8148c8c8e2e075efb1da 100644 --- a/package.json +++ b/package.json @@ -130,6 +130,7 @@ "@types/request": "2.0.3", "@types/selenium-webdriver": "3.0.4", "@types/sinon": "2.3.3", + "@types/sinon-test": "^1.0.2", "@types/ws": "3.0.2", "@types/xml2js": "^0.4.0", "apiai": "4.0.3", @@ -151,6 +152,7 @@ "qrcode-terminal": "0.11.0", "shx": "0.2.2", "sinon": "3.2.1", + "sinon-test": "^2.1.0", "sloc": "0.2.0", "ts-node": "3.3.0", "tslint": "5.3.2", diff --git a/src/puppet-web/bridge.spec.ts b/src/puppet-web/bridge.spec.ts new file mode 100755 index 0000000000000000000000000000000000000000..404054f2b4958fedb6bb6e0810566ccbdbe099df --- /dev/null +++ b/src/puppet-web/bridge.spec.ts @@ -0,0 +1,32 @@ +/** + * Wechaty - https://github.com/chatie/wechaty + * + * Copyright 2016-2017 Huan LI + * + * 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') +})) diff --git a/src/puppet-web/bridge.ts b/src/puppet-web/bridge.ts index 4376df30cc6371e2bf8b8935953dbd2383bd0845..92d7f7ccb4f68e801d5fff4e68b5d4e5d011a71d 100644 --- a/src/puppet-web/bridge.ts +++ b/src/puppet-web/bridge.ts @@ -59,15 +59,13 @@ export class Bridge { public async init(): Promise { 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 { diff --git a/src/puppet-web/browser-driver.spec.ts b/src/puppet-web/browser-driver.spec.ts new file mode 100755 index 0000000000000000000000000000000000000000..bce7d0f1250ac8935e3cbaa5dc4ce03dce6217fc --- /dev/null +++ b/src/puppet-web/browser-driver.spec.ts @@ -0,0 +1,55 @@ +/** + * Wechaty - https://github.com/chatie/wechaty + * + * Copyright 2016-2017 Huan LI + * + * 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) + } +}) diff --git a/src/puppet-web/browser-driver.ts b/src/puppet-web/browser-driver.ts index e6d2bbf2db056c6de071f05fb15f46653fdbc2a2..fa8b88aa36a1e7db1658e05a29d962d3f64a936a 100644 --- a/src/puppet-web/browser-driver.ts +++ b/src/puppet-web/browser-driver.ts @@ -435,3 +435,5 @@ export { Session, TargetLocator, } + +export default BrowserDriver diff --git a/test/webdriver.spec.ts b/test/webdriver.spec.ts index 38ec90b57d250e7d69bf7a47f00772d588ffb770..9d3d2ee989bc67536867c322406dcfb9616b3d3a 100644 --- a/test/webdriver.spec.ts +++ b/test/webdriver.spec.ts @@ -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 = {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) - } })