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

try to support phantomjs

上级 d858c315
......@@ -31,11 +31,12 @@
"homepage": "https://github.com/zixia/wechaty#readme",
"dependencies": {
"body-parser": "^1.15.0",
"chromedriver": "^2.21.2",
"socket.io": "^1.4.5",
"express": "^4.13.4",
"npmlog": "^2.0.3",
"selenium-webdriver": "",
"socket.io": "^1.4.5"
"phantomjs-prebuilt": "^2.1.7",
"chromedriver": "^2.21.2",
"npmlog": "^2.0.3"
},
"eslintConfig": {
"env": {
......
/****************************************
/**
* Wechat for Bot. and for human who can talk with bot/robot
*
* Class Browser
* Interface for puppet
*
header cookie
BaseRequest
Uin
Sid
Skey
DeviceId
* Licenst: ISC
* https://github.com/zixia/wechaty
*
*/
***************************************/
const fs = require('fs')
const path = require('path')
const WebDriver = require('selenium-webdriver')
......@@ -41,40 +38,40 @@ class Browser {
return this.driver.get(WX_URL)
}
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)
getDriver() {
var driver
switch (this.browser) {
case 'phantomjs':
driver = getPhantomJsDriver()
break
default:
driver = new WebDriver.Builder().forBrowser(this.browser).build()
break
}
return driver
}
//build custom phantomJS driver
return new selenium.Builder()
.withCapabilities(customPhantom)
.build()
}
}
static getPhantomJsDriver() {
// https://github.com/SeleniumHQ/selenium/issues/2069
//setup custom phantomJS capability
const phantomjsExe = require('phantomjs-prebuilt').path
const customPhantom = WebDriver.Capabilities.phantomjs()
.set('phantomjs.binary.path', phantomjsExe)
//build custom phantomJS driver
return new WebDriver.Builder()
.withCapabilities(customPhantom)
.build()
}
getInjectio() {
return fs.readFileSync(
path.join(path.dirname(__filename), 'puppet-web-injectio.js')
static getInjectio() {
return fs.readFileSync(
path.join(path.dirname(__filename), 'puppet-web-injectio.js')
, 'utf8'
)
)
}
inject() {
const injectio = this.getInjectio()
const injectio = Browser.getInjectio()
log.verbose('Browser', 'injecting')
try {
var p = this.execute(injectio, this.port)
......@@ -90,24 +87,25 @@ 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())
})
}
execute(script, ...args) {
//log.verbose('Browser', `Browser.execute(${script})`)
if (!this.driver)
if (!this.driver) {
throw new Error('driver not found')
}
// return promise
return this.driver.executeScript.apply(this.driver, arguments)
}
......
const Browser = require('./puppet-web-browser')
/****************************************
/**
* Wechat for Bot. and for human who can talk with bot/robot
*
* Class Server
* Interface for puppet
*
***************************************/
* Licenst: ISC
* https://github.com/zixia/wechaty
*
*/
const fs = require('fs')
const io = require('socket.io')
const path = require('path')
const path = require('path')
const https = require('https')
const bodyParser = require('body-parser')
const log = require('npmlog')
const Express = require('express')
const EventEmitter = require('events')
const log = require('npmlog')
const Browser = require('./puppet-web-browser')
class Server extends EventEmitter {
constructor(options) {
super()
options = options || {}
this.port = options.port || 8788 // W(87) X(88), ascii char code ;-]
this.logined = false
this.on('login' , () => this.logined = true )
this.on('logout', () => this.logined = false )
this.on('login' , () => this.logined = true)
this.on('logout', () => this.logined = false)
}
......@@ -69,7 +71,7 @@ class Server extends EventEmitter {
*/
createHttpsServer(express) {
return https.createServer({
key : require('./ssl-key-cert').key
key: require('./ssl-key-cert').key
, cert: require('./ssl-key-cert').cert
}, express).listen(this.port, () => {
log.verbose('Server', `createHttpsServer port ${this.port}`)
......@@ -86,12 +88,12 @@ class Server extends EventEmitter {
app.use(bodyParser.json())
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*")
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept")
res.header('Access-Control-Allow-Origin', '*')
res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept')
next()
})
app.get('/ding', function (req, res) {
app.get('/ding', function(req, res) {
log.silly('Server', '%s GET /ding', new Date())
res.end('dong')
})
......@@ -120,7 +122,7 @@ class Server extends EventEmitter {
* Possible conditions:
* 1. Browser reload
* 2. Lost connection(Bad network
* 3.
* 3.
*/
this.socketClient = null
})
......@@ -131,10 +133,10 @@ class Server extends EventEmitter {
, 'login'
, 'logout'
, 'unload'
].map(e => {
s.on(e, data => {
].map(e => {
s.on(e, data => {
log.silly('Server', `recv event[${e}] from browser`)
this.emit(e, data)
this.emit(e, data)
})
})
......@@ -181,8 +183,9 @@ class Server extends EventEmitter {
*
*/
browserExecute(script) {
if (!this.browser)
if (!this.browser) {
throw new Error('no browser!')
}
return this.browser.execute(script)
}
......
......@@ -3,7 +3,7 @@ const log = require('npmlog')
const test = require('tape')
const Server = require('../src/puppet-web-server')
test('Server basic tests', function (t) {
test('Server basic tests', function(t) {
//t.plan(9)
const PORT = 58788
......@@ -33,7 +33,7 @@ test('Server basic tests', function (t) {
s.quit() + t.end()
})
test('Server smoke testing', function (t) {
test('Server smoke testing', function(t) {
const PORT = 58788
const server = new Server({port: PORT})
......@@ -90,18 +90,19 @@ test('Server smoke testing', function (t) {
setTimeout(testDing, waitTime)
function testDing() {
//log.verbose('TestingServer', server.socketio)
log.silly('TestingServer', server.socketio)
if (!server.socketClient) {
totalTime += waitTime
if (totalTime > maxTime)
return reject('timeout after ' + totalTime + 'ms');
if (totalTime > maxTime) {
return reject('timeout after ' + totalTime + 'ms')
}
log.verbose('TestingServer', 'waiting socketClient to connect for ' + totalTime + '/' + maxTime + ' ms...')
setTimeout(testDing, waitTime)
return
}
//log.verbose('TestingServer', server.socketClient)
server.socketClient.on ('dong', data => {
log.silly('TestingServer', server.socketClient)
server.socketClient.on('dong', data => {
log.verbose('TestingServer', 'socket on dong got: ' + data)
resolve(data)
})
......
......@@ -8,30 +8,35 @@ const By = WebDriver.By
const WebBrowser = require('../src/puppet-web-browser')
test('WebDriver smoke testing', function(t) {
/*
const driver = new WebDriver.Builder()
.withCapabilities(WebDriver.Capabilities.chrome()).build()
*/
const driver = WebBrowser.getPhantomJsDriver()
/*
.withCapabilities(
WebDriver.Capabilities.phantomjs()
//.set('phantomjs.binary.path', 'D:\\cygwin64\\home\\zixia\\git\\wechaty\\node_modules\\phantomjs-prebuilt\\lib\\phantom\\bin\\phantomjs.exe')
).build()
*/
// .set('webdriver.load.strategy', 'unstable')
// https://stackoverflow.com/questions/37071807/how-to-executescript-before-page-load-by-webdriver-in-selenium
// .set('webdriver.load.strategy', 'unstable')
// https://stackoverflow.com/questions/37071807/how-to-executescript-before-page-load-by-webdriver-in-selenium
const injectio = WebBrowser.getInjectio()
const injectio = new WebBrowser().getInjectio()
driver.get('https://wx.qq.com/')
.then(() => {
Promise.all([
execute('return 1+1') // ret_add
, execute(injectio, 8788) // ret_inject
]).then(([
ret_add
, ret_inject
retAdd
, retInject
]) => {
t.equal(ret_add , 2 , 'execute js in browser')
t.equal(ret_inject, 'Wechaty' , 'injected wechaty')
t.equal(retAdd , 2 , 'execute js in browser')
t.equal(retInject, 'Wechaty' , 'injected wechaty')
t.end()
driver.quit()
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册