From b2fd5d1bbcddecef7907076cff126bd5e1b32437 Mon Sep 17 00:00:00 2001 From: "Zhuohuan LI (CARPE DIEM)" Date: Thu, 12 May 2016 13:47:52 +0800 Subject: [PATCH] compatible phantomjs-prebult executable path --- README.md | 2 +- circle.yml | 7 +++++ src/puppet-web-browser.js | 62 +++++++++++++++++++++++++-------------- 3 files changed, 48 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index 4aef1d51..cead2cf7 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ # Wechaty [![Circle CI](https://circleci.com/gh/zixia/wechaty.svg?style=svg)](https://circleci.com/gh/zixia/wechaty) Wechaty is Wechat for Bot. -It's a library/framework for easy creating wechat bot in 10 lines of code. +> Easy creating wechat robot code in 10 lines. [![Join the chat at https://gitter.im/zixia/wechaty](https://badges.gitter.im/zixia/wechaty.svg)](https://gitter.im/zixia/wechaty?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) [![npm version](https://badge.fury.io/js/wechaty.svg)](https://badge.fury.io/js/wechaty) diff --git a/circle.yml b/circle.yml index b4bf0051..00d71350 100644 --- a/circle.yml +++ b/circle.yml @@ -4,3 +4,10 @@ machine: dependencies: post: + +notify: + webhooks: + # A list of hook hashes, containing the url field + # gitter hook + - url: https://webhooks.gitter.im/e/537e61e880e78bc0b271 + diff --git a/src/puppet-web-browser.js b/src/puppet-web-browser.js index af6a0cff..90cf28d2 100644 --- a/src/puppet-web-browser.js +++ b/src/puppet-web-browser.js @@ -36,22 +36,40 @@ class Browser { const WX_URL = 'https://wx.qq.com' log.verbose('Browser', `init ${this.browser}:${this.port}`) - this.driver = new WebDriver.Builder().forBrowser(this.browser).build() - - /* - this.driver = new WebDriver.Builder()//.forBrowser(this.browser).build() - .withCapabilities( - WebDriver.Capabilities.phantomjs() - .set('phantomjs.binary.path', 'D:\\cygwin64\\home\\zixia\\git\\wechaty\\node_modules\\phantomjs-prebuilt\\lib\\phantom\\bin\\phantomjs.exe') - ).build() - */ + this.driver = this.getDriver() return this.driver.get(WX_URL) } - getInjectio() { - return fs.readFileSync( - path.join(path.dirname(__filename), 'puppet-web-injectio.js') + getDriver() { + var driver + switch(this.browser) { + case 'phantomjs': + driver = getPhantomJsPreBuilt() + break + default: + driver = new WebDriver.Builder().forBrowser(this.browser).build() + break + } + return driver + + function getPhantomJsPreBuilt() { + // https://github.com/SeleniumHQ/selenium/issues/2069 + //setup custom phantomJS capability + const phantomjs_exe = require('phantomjs-prebuilt').path + var customPhantom = selenium.Capabilities.phantomjs() + .set("phantomjs.binary.path", phantomjs_exe) + + //build custom phantomJS driver + return new selenium.Builder() + .withCapabilities(customPhantom) + .build() + } + } + + getInjectio() { + return fs.readFileSync( + path.join(path.dirname(__filename), 'puppet-web-injectio.js') , 'utf8' ) } @@ -72,16 +90,16 @@ class Browser { }) } - quit() { - log.verbose('Browser', 'Browser.quit') - if (!this.driver) { - log.verbose('Browser', 'no need to quite because no driver') - return new Promise((resolve, reject) => resolve('no driver')) - } - log.verbose('Browser', 'Browser.driver.quit') - return this.execute('return (typeof Wechaty)!=="undefined" && Wechaty.quit()').then(() => { - this.driver.quit() - this.driver = null + quit() { + log.verbose('Browser', 'Browser.quit') + if (!this.driver) { + log.verbose('Browser', 'no need to quite because no driver') + return new Promise((resolve, reject) => resolve('no driver')) + } + log.verbose('Browser', 'Browser.driver.quit') + return this.execute('return (typeof Wechaty)!=="undefined" && Wechaty.quit()').then(() => { + this.driver.quit() + this.driver = null return new Promise((resolve, reject) => resolve()) }) } -- GitLab