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

code clean

上级 db3955eb
......@@ -29,7 +29,7 @@
"posttest": "npm run clean && npm run sloc",
"test:phantomjs": "cross-env LC_ALL=C WECHATY_LOG=info WECHATY_HEAD=phantomjs ava --timeout=7m \"dist/test/*.spec.js\"",
"test:phantomjs.bak": "cross-env LC_ALL=C WECHATY_LOG=info WECHATY_HEAD=phantomjs ava --timeout=7m \"dist/{src,test}/**/*.spec.js\"",
"test:chrome": "cross-env LC_ALL=C WECHATY_LOG=silly WECHATY_HEAD=chrome ava --serial --verbose --ext js --timeout=7m \"dist/{src,test}/**/*.spec.js\"",
"test:chrome": "cross-env LC_ALL=C WECHATY_LOG=silly WECHATY_HEAD=chrome ava --serial --fail-fast --verbose --ext js --timeout=7m \"dist/{src,test}/**/*.spec.js\"",
"testdev": "cross-env LC_ALL=C WECHATY_LOG=silly ava --ext ts --serial --verbose --fail-fast --timeout=3m",
"testdist": "cross-env WECHATY_LOG=SILLY WECHATY_HEAD=chrome ava --ext ts --verbose --fail-fast --timeout=2m",
"ava": "cross-env LC_ALL=C WECHATY_LOG=verbose ts-node node_modules/.bin/ava \"{src,test}/**/*.spec.js\"",
......
......@@ -31,11 +31,14 @@ export class BrowserDriver {
if (this.driver) {
try {
await this.driver.close()
await this.driver.quit()
const valid = await this.valid(this.driver)
if (valid) {
// await this.driver.close()
await this.driver.quit()
}
} catch (e) {
log.warn('PuppetWebBrowserDriver', 'init() this.driver.{close,quit}() exception: %s'
, e.message
log.warn('PuppetWebBrowserDriver', 'init() this.driver.quit() exception: %s'
, e.stack
)
}
}
......@@ -77,8 +80,8 @@ export class BrowserDriver {
* and will cause strange error:
*
*/
const chrome = require('selenium-webdriver/chrome')
const path = require('chromedriver').path
const chrome = require('selenium-webdriver/chrome')
const path = require('chromedriver').path
const service = new chrome.ServiceBuilder(path).build()
try {
......@@ -232,7 +235,7 @@ export class BrowserDriver {
const session = await driver.getSession()
if (!session) {
log.warn('PuppetWebBrowserDriver', 'valid() found an invalid driver')
log.verbose('PuppetWebBrowserDriver', 'valid() found an invalid driver')
} else {
log.silly('PuppetWebBrowserDriver', 'valid() driver ok')
}
......
......@@ -51,6 +51,8 @@ export class Browser extends EventEmitter {
public toString() { return `Browser({head:${this.setting.head})` }
public async init(): Promise<void> {
log.verbose('PuppetWebBrowser', 'init()')
this.state.target('open')
this.state.current('open', false)
......@@ -60,10 +62,12 @@ export class Browser extends EventEmitter {
try {
await this.driver.init()
log.verbose('PuppetWebBrowser', 'init() driver.init() succ')
await this.open(jumpUrl)
await this.loadCookie()
.catch(e => { // fail safe
log.verbose('PuppetWeb', 'browser.loadSession(%s) exception: %s'
log.verbose('PuppetWebBrowser', 'browser.loadSession(%s) exception: %s'
, this.setting.sessionFile
, e && e.message || e
)
......@@ -85,8 +89,9 @@ export class Browser extends EventEmitter {
} catch (e) {
log.error('PuppetWebBrowser', 'init() exception: %s', e.message)
this.state.current('close')
// this.state.current('close', false)
await this.quit()
// this.state.current('close')
throw e
}
......@@ -118,7 +123,7 @@ export class Browser extends EventEmitter {
await this.quit()
if (this.state.current() === 'open' && this.state.inprocess()) {
log.warn('PuppetWebBrowser', 'restart() found state.current() === open and inprocess()')
log.warn('PuppetWebBrowser', 'restart() found state.current() === open and inprocess() after quit()!')
return
}
......@@ -138,9 +143,9 @@ export class Browser extends EventEmitter {
try {
await this.driver.close().catch(e => { /* fail safe */ }) // http://stackoverflow.com/a/32341885/1123955
log.silly('PuppetWebBrowser', 'quit() driver.close()-ed')
log.silly('PuppetWebBrowser', 'quit() driver.close() succ')
await this.driver.quit()
log.silly('PuppetWebBrowser', 'quit() driver.quit()-ed')
log.silly('PuppetWebBrowser', 'quit() driver.quit() succ')
/**
*
......@@ -168,7 +173,6 @@ export class Browser extends EventEmitter {
/* fail safe */
}
// this.currentState('close')
this.state.current('close')
return
......
......@@ -80,7 +80,7 @@ async function onBrowserDead(this: PuppetWeb, e): Promise<void> {
try {
await this.browser.quit()
log.verbose('PuppetWebEvent', 'onBrowserDead() browser quit-ed')
log.verbose('PuppetWebEvent', 'onBrowserDead() browser.quit() succ')
if (browser.state.target() !== 'open') {
log.warn('PuppetWebEvent', 'onBrowserDead() will not init browser because browser.state.target(%s) !== open'
......
......@@ -34,14 +34,14 @@ export class StateMonitor <A, B>{
*/
public target(newState?: A|B): A|B {
if (newState) {
log.verbose('StateMonitor', '%s.state.target(%s) from %s'
, this._client
log.verbose('StateMonitor', 'state.target(%s) <- %s for %s'
, newState
, this._target
, this._client
)
this._target = newState
} else {
log.silly('StateMonitor', '%s.state.target() is %s', this._client, this._target)
log.silly('StateMonitor', 'state.target() is %s of %s', this._target, this._client)
}
return this._target
}
......@@ -52,25 +52,30 @@ export class StateMonitor <A, B>{
*/
public current(newState?: A|B, stable = true): A|B {
if (newState) {
log.verbose('StateMonitor', '%s.state.current(%s,%s) from (%s,%s)'
, this._client
log.verbose('StateMonitor', 'state.current(%s,%s) <- (%s,%s) for %s'
, newState, stable
, this._current, this._stable
, this._client
)
/**
* warn for inprocess current state change twice, mostly like a logic bug outside
*/
if (this._current === newState && this._stable === stable
&& stable === false // warn for inprocess current state change twice, mostly like a logic bug outside
&& stable === false
) {
log.warn('StateMonitor', '%s.state.current(%s,%s) called but there are already in the same state'
, this._client
log.warn('StateMonitor', 'state.current(%s,%s) called but there are already in the same state for %s'
, newState, stable
, this._client
)
const e = new Error('current unchange')
log.verbose('StateMonitor', e.stack)
}
this._current = newState
this._stable = stable
} else {
log.silly('StateMonitor', '%s.state.current() is %s', this._client, this._current)
log.silly('StateMonitor', 'state.current() is %s of %s', this._current, this._client)
}
return this._current
}
......@@ -79,7 +84,7 @@ export class StateMonitor <A, B>{
* does the current state be stable(not inprocess)?
*/
public stable() {
log.silly('StateMonitor', '%s.state.stable() is %s', this._client, this._stable)
log.silly('StateMonitor', 'state.stable() is %s of %s', this._stable, this._client)
return this._stable
}
......@@ -87,7 +92,7 @@ export class StateMonitor <A, B>{
* does the current state be inprocess(not stable)?
*/
public inprocess() {
log.silly('StateMonitor', '%s.state.inprocess() is %s', this._client, !this._stable)
log.silly('StateMonitor', 'state.inprocess() is %s of %s', !this._stable, this._client)
return !this._stable
}
......
......@@ -134,5 +134,5 @@ test('getPort() for an available socket port', async t => {
t.fail('should not exception: ' + e.message + ', ' + e.stack)
}
}
t.ok('should has no exception after loop test')
t.pass('should has no exception after loop test')
})
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册