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

webdriver smoking test

上级 0a2a2b96
...@@ -29,7 +29,7 @@ class Browser { ...@@ -29,7 +29,7 @@ class Browser {
} }
open() { open() {
const WX_URL = 'https://wx.qq.com' const WX_URL = 'http://zixia.net/~zixia/dl/' // 'https://wx.qq.com'
console.error(`browser init ${this.browser}:${this.port}`) console.error(`browser init ${this.browser}:${this.port}`)
this.driver = new WebDriver.Builder().forBrowser(this.browser).build() this.driver = new WebDriver.Builder().forBrowser(this.browser).build()
...@@ -37,41 +37,37 @@ class Browser { ...@@ -37,41 +37,37 @@ class Browser {
return this.driver.get(WX_URL) return this.driver.get(WX_URL)
} }
inject() { getInjectio() {
const injectio = fs.readFileSync( return fs.readFileSync(
path.join(path.dirname(__filename), 'puppet-web-injectio.js') path.join(path.dirname(__filename), 'puppet-web-injectio.js')
, 'utf8' , 'utf8'
) )
}
const socketio = fs.readFileSync( inject() {
// 'https://cdnjs.cloudflare.com/ajax/libs/socket.io/1.4.5/socket.io.min.js' const injectio = getInjectio()
path.join(path.dirname(__filename), '/socket.io.min.js')
, 'utf8'
)
console.error('injecting') console.error('injecting')
return this.execute(socketio) return this.execute(injectio, this.port)
.then(() => {
console.error('injected socketio')
this.execute(injectio, this.port)
})
.then(() => { .then(() => {
console.error('injected injectio') console.error('injected / call Wechaty.init()')
return this.execute('return Wechaty.init()') return this.execute('return Wechaty.init()')
}) }).then((data) => {
.then(() => { console.error('Wechaty.init() return: ' + data)
console.error('injected Wechaty()') return new Promise((resolve, reject) => resolve(data))
return new Promise((resolve, reject) => resolve())
}) })
} }
quit() { quit() {
// console.error('Browser.quit') console.error('Browser.quit')
if (this.driver) { if (!this.driver) {
console.error('Browser.driver.quit') console.error('no need to quite because no driver')
// this.driver.quit() return new Promise((resolve, reject) => resolve('no driver'))
delete this.driver
} }
console.error('Browser.driver.quit')
this.execute('return (typeof Wechaty)!=="undefined" && Wechaty.quit()').then(() => {
this.driver.quit(true)
delete this.driver
return new Promise((resolve, reject) => resolve())
})
} }
execute(script, ...args) { execute(script, ...args) {
...@@ -79,7 +75,7 @@ class Browser { ...@@ -79,7 +75,7 @@ class Browser {
if (!this.driver) if (!this.driver)
throw new Error('driver not found') throw new Error('driver not found')
// a promise // a promise
return this.driver.executeScript(script, args) return this.driver.executeScript.apply(this.driver, arguments)
} }
} }
......
...@@ -19,27 +19,31 @@ ...@@ -19,27 +19,31 @@
408: 未确认 408: 未确认
*/ */
;(function (port) { ;return (function (port) {
port = port || 8788 port = port || 8788
var injector = angular.element(document).injector()
var zlog = createZlog() /**
* Log to console
* http://stackoverflow.com/a/7089553/1123955
*/
function clog(s) {
var d = new Date()
s = d.getHours() + ':' + d.getMinutes() + ':' + d.getSeconds() + ' <Wechaty> ' + s
var i = document.createElement('iframe')
i.style.display = 'none'
document.body.appendChild(i)
i.contentWindow.console.log(s)
i.parentNode.removeChild(i)
}
var Wechaty = { var Wechaty = {
// get all we need from wx in browser(angularjs) glue: {} // see glueAngular() function
glue: {
injector: injector
, rootScope: injector.get("$rootScope")
, http: injector.get("$http")
, chatFactory: injector.get("chatFactory")
, confFactory: injector.get("confFactory")
, loginScope: angular.element(".login_box").scope()
}
// glue funcs // glue funcs
, getLoginStatusCode: function () { return loginScope.code } , getLoginStatusCode: function () { return Wechaty.glue.loginScope.code }
, getLoginQrImgUrl: function () { return loginScope.qrcodeUrl } , getLoginQrImgUrl: function () { return Wechaty.glue.loginScope.qrcodeUrl }
, isLogined: function () { return 200===loginScope.code } , isLogined: function () { return 200===Wechaty.glue.loginScope.code /* MMCgi.isLogin ??? */}
, isReady: function () { return !!(angular && angular.element && angular.element("body")); } , isReady: isReady
// variable // variable
, socket: null , socket: null
...@@ -47,40 +51,65 @@ ...@@ -47,40 +51,65 @@
// funcs // funcs
, init: init , init: init
, send: send , send: send
, zlog: zlog , clog: clog // Console log
, log: log , slog: slog // Socket IO log
, ping: ping , ping: ping
, quit: quit
} }
function isReady() {
return (typeof angular)!=='undefined' && angular.element && angular.element("body")
}
function init() { function init() {
zlog('wechaty port ' + port) // XXX
// return 'init skiped in browser'
if (!isReady()) {
clog('angular not ready. wait 500ms...')
setTimeout(init, 500)
return // AngularJS not ready, wait 500ms then try again.
}
initSocket() clog('init on port:' + port)
glueAngular()
connectSocket()
hookUnload() hookUnload()
hookMessage() hookMessage()
zlog('Wechaty injected!. ;-D') clog('inited!. ;-D')
return true
} }
function createZlog() { function glueAngular() {
var enable = true var injector = angular.element(document).injector()
if (enable) { var rootScope = injector.get("$rootScope")
if (!console.memory && console.time) { // wechat debuger exist var http = injector.get("$http")
delete console var chatFactory = injector.get("chatFactory")
} var confFactory = injector.get("confFactory")
console.zlog = window.console.log var loginScope = angular.element(".login_box").scope()
window.zlog = function (s) { return console.zlog(s) }
console.log = function () {} // get all we need from wx in browser(angularjs)
} else { Wechaty.glue = {
window.zlog = function () {} injector: injector
, rootScope: rootScope
, http: http
, chatFactory: chatFactory
, confFactory: confFactory
, loginScope: loginScope
} }
return window.zlog
} }
function log(msg) { Wechaty.socket && Wechaty.socket.emit('log', msg) } function quit() {
function ping() { return 'pong' } if (Wechaty.socket) {
Wechaty.socket.close()
function send (ToUserName, Content) { Wechaty.socket = undefined
}
clog('quit()')
}
function slog(data) { return Wechaty.socket && Wechaty.socket.emit('log', data) }
function ping() { return 'pong' }
function send(ToUserName, Content) {
var c = Wechaty.glue.chatFactory var c = Wechaty.glue.chatFactory
var m = c.createMessage({ var m = c.createMessage({
ToUserName: ToUserName ToUserName: ToUserName
...@@ -89,58 +118,68 @@ ...@@ -89,58 +118,68 @@
c.appendMessage(m) c.appendMessage(m)
return c.sendMessage(m) return c.sendMessage(m)
} }
function hookMessage() { function hookMessage() {
var rootScope = Wechaty.glue.rootScope var rootScope = Wechaty.glue.rootScope
var zlog = Wechaty.zlog
rootScope.$on("message:add:success", function (event, data) { rootScope.$on("message:add:success", function (event, data) {
Wechaty.socket.emit('message', data) Wechaty.socket.emit('message', data)
.catch(function (e) { .catch(function (e) {
zlog('socket.emit(message, data) fail:') clog('socket.emit(message, data) fail:')
zlog(e) clog(e)
}) })
}) })
} }
function hookUnload() { function hookUnload() {
window.addEventListener ('unload', function (e) { window.addEventListener ('unload', function (e) {
Wechaty.socket.emit('unload') Wechaty.socket.emit('unload')
}) })
} }
function connectSocket() {
clog('connectSocket()')
if (typeof io!=='function') {
clog('connectSocket: io not found. loading lib...')
// http://stackoverflow.com/a/3248500/1123955
var script = document.createElement('script')
script.onload = function() {
clog('socket io lib loaded.')
setTimeout(connectSocket, 50)
}
script.src = "https://cdnjs.cloudflare.com/ajax/libs/socket.io/1.4.5/socket.io.min.js"
document.getElementsByTagName('head')[0].appendChild(script);
return // wait to be called via script.onload()
}
function initSocket() {
// Wechaty global variable: socket // Wechaty global variable: socket
var socket = Wechaty.socket = io.connect('https://127.0.0.1:' + port) var socket = Wechaty.socket = io.connect('https://127.0.0.1:' + port)
var zlog = Wechaty.zlog
socket.on('connect', function() { socket.on('connect', function() {
zlog('on connect entried') clog('on connect entried')
// new message
Wechaty.glue.rootScope.$on("message:add:success", function (event, data) { Wechaty.glue.rootScope.$on("message:add:success", function (event, data) {
socket.emit('message', data) socket.emit('message', data)
}) })
// ping -> pong. for test & live check purpose
socket.on('disconnect', function(e) { socket.on('ping', function (e) {
zlog('event: socket disconnect') clog('received socket io event: ping. emit pong...')
socket.emit('pong', 'pong')
// for test & live check purpose: ping -> pong
socket.on('ping', function (e) {
Wechaty.zlog('received socket io event: ping. emit pong...')
socket.emit('pong', 'pong')
})
// Reconnect...
setTimeout(function () {
zlog('starting initSocket after disconnect')
initSocket()
}, 1000)
}) })
// re-connect XXX will socketio library auto re-connect by itself???
// socket.on('disconnect', function(e) {
// clog('event: socket disconnect')
// // Reconnect...
// setTimeout(function () {
// clog('starting initSocket after disconnect')
// initSocket()
// }, 1000)
// })
}) })
} }
window.Wechaty = Wechaty window.Wechaty = Wechaty
var callback = arguments[arguments.length - 1]
if (typeof callback==='function')
callback('Wechaty')
return 'Wechaty' return 'Wechaty'
}(arguments[0])) }.apply(window, arguments))
...@@ -11,22 +11,35 @@ test('Browser class smoking tests', function (t) { ...@@ -11,22 +11,35 @@ test('Browser class smoking tests', function (t) {
.then(() => { .then(() => {
t.ok(true, 'url opened') t.ok(true, 'url opened')
b.inject() b.execute('return 1+1')
.then(() => { .then(n => {
t.ok(true, 'wechaty injected') t.equal(n, 2, 'exec 1+1 in browser, equal 2')
b.execute('return 1+1') // b.inject().then( r => {
.then(n => t.equal(n, 2, 'exec 1+1 in browser, equal 2')) // console.log('injected: ' + r)
b.execute('return Wechaty && Wechaty.ping()') b.quit()
.then(r => t.equal(r, 'pong', 'Wechaty.ping() returns pong')) t.end()
b.execute('return Wechaty && Wechaty.isReady()') // })
.then(r => t.notEqual(typeof r, 'bool', 'Wechaty.isReady() returns bool'))
b.quit()
t.end()
}) })
// b.inject()
// .then(() => {
// t.ok(true, 'wechaty injected')
// b.execute('return Wechaty && Wechaty.ping()')
// .then(r => t.equal(r, 'pong', 'Wechaty.ping() returns pong'))
// b.execute('return Wechaty && Wechaty.isReady()')
// .then(r => t.notEqual(typeof r, 'bool', 'Wechaty.isReady() returns bool'))
// b.quit(true)
// t.end()
// })
}) })
}) })
const path = require('path')
const test = require('tape')
const WebDriver = require('selenium-webdriver')
const Browser = WebDriver.Browser
const By = WebDriver.By
const WebBrowser = require('../lib/puppet-web-browser')
test('WebDriver smoking test', function(t) {
const driver = new WebDriver.Builder()
.withCapabilities(WebDriver.Capabilities.chrome()).build()
// .set('webdriver.load.strategy', 'unstable')
// https://stackoverflow.com/questions/37071807/how-to-executescript-before-page-load-by-webdriver-in-selenium
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
]) => {
t.equal(ret_add , 2 , 'execute js in browser')
t.equal(ret_inject, 'Wechaty' , 'injected wechaty')
t.end()
driver.quit()
})
})
function verifyJson(expected) {
return function(actual) {
assert(JSON.stringify(actual)).equalTo(JSON.stringify(expected))
};
}
function execute() {
return driver.executeScript.apply(driver, arguments)
}
})
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册