From 3f7791ef4fddfc4c32761d35223ad08bf4c76891 Mon Sep 17 00:00:00 2001 From: Huan LI Date: Sat, 26 Aug 2017 19:28:31 +0800 Subject: [PATCH] support(switch account): try to click the button if found (#636) --- src/puppet-web/bridge.ts | 4 ---- src/puppet-web/puppet-web.ts | 24 ++++++++++++------------ test/puppet-web/puppet-web.spec.ts | 5 ++++- 3 files changed, 16 insertions(+), 17 deletions(-) diff --git a/src/puppet-web/bridge.ts b/src/puppet-web/bridge.ts index eb8e4325..4376df30 100644 --- a/src/puppet-web/bridge.ts +++ b/src/puppet-web/bridge.ts @@ -583,10 +583,6 @@ export class Bridge { }) } - public async clickSwitchAccount(): Promise { - log.verbose('PuppetWebBridge', 'clickSwitchAccount()') - return this.puppet.browser.clickSwitchAccount() - } } /* tslint:disable:jsdoc-format */ diff --git a/src/puppet-web/puppet-web.ts b/src/puppet-web/puppet-web.ts index 3a2afafb..e41f7a52 100644 --- a/src/puppet-web/puppet-web.ts +++ b/src/puppet-web/puppet-web.ts @@ -103,11 +103,11 @@ export class PuppetWeb extends Puppet { await this.initServer() log.verbose('PuppetWeb', 'initServer() done') - await this.initBrowser() + this.browser = await this.initBrowser() log.verbose('PuppetWeb', 'initBrowser() done') try { - await this.initBridge() + this.bridge = await this.initBridge() } catch (e) { const blockedMessage = await this.bridge.blockedMessageBody() || await this.bridge.blockedMessageAlert() @@ -119,7 +119,7 @@ export class PuppetWeb extends Puppet { } log.verbose('PuppetWeb', 'initBridge() done') - const clicked = await this.bridge.clickSwitchAccount() + const clicked = await this.browser.clickSwitchAccount() if (clicked) { log.verbose('PuppetWeb', 'init() bridge.clickSwitchAccount() clicked') } @@ -230,15 +230,15 @@ export class PuppetWeb extends Puppet { } } - public async initBrowser(): Promise { + public async initBrowser(): Promise { log.verbose('PuppetWeb', 'initBrowser()') - this.browser = new Browser({ + const browser = new Browser({ head: this.setting.head, sessionFile: this.setting.profile, }) - this.browser.on('dead', Event.onBrowserDead.bind(this)) + browser.on('dead', Event.onBrowserDead.bind(this)) if (this.state.target() === 'dead') { const e = new Error('found state.target()) != live, no init anymore') @@ -247,19 +247,19 @@ export class PuppetWeb extends Puppet { } try { - await this.browser.init() + await browser.init() } catch (e) { log.error('PuppetWeb', 'initBrowser() exception: %s', e.message) Raven.captureException(e) throw e } - return + return browser } - public async initBridge(): Promise { + public async initBridge(): Promise { log.verbose('PuppetWeb', 'initBridge()') - this.bridge = new Bridge( + const bridge = new Bridge( this, // use puppet instead of browser, is because browser might change(die) duaring run time, this.port, ) @@ -271,7 +271,7 @@ export class PuppetWeb extends Puppet { } try { - await this.bridge.init() + await bridge.init() } catch (e) { Raven.captureException(e) if (!this.browser) { @@ -284,7 +284,7 @@ export class PuppetWeb extends Puppet { throw e } } - return + return bridge } private async initServer(): Promise { diff --git a/test/puppet-web/puppet-web.spec.ts b/test/puppet-web/puppet-web.spec.ts index c9c5e9ee..baa4e855 100644 --- a/test/puppet-web/puppet-web.spec.ts +++ b/test/puppet-web/puppet-web.spec.ts @@ -41,7 +41,10 @@ import { */ test.serial('login/logout events', async t => { const STUB_INIT_BROWSER = stub(PuppetWeb.prototype, 'initBrowser') - STUB_INIT_BROWSER.resolves() + STUB_INIT_BROWSER.resolves({ + clickSwitchAccount: () => false, + }) + const STUB_BRIDGE_INIT = stub(PuppetWebBridge.prototype, 'init') STUB_BRIDGE_INIT.resolves() -- GitLab