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

webdriver smoking test

上级 0a2a2b96
......@@ -29,7 +29,7 @@ class Browser {
}
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}`)
this.driver = new WebDriver.Builder().forBrowser(this.browser).build()
......@@ -37,41 +37,37 @@ class Browser {
return this.driver.get(WX_URL)
}
inject() {
const injectio = fs.readFileSync(
getInjectio() {
return fs.readFileSync(
path.join(path.dirname(__filename), 'puppet-web-injectio.js')
, 'utf8'
)
const socketio = fs.readFileSync(
// 'https://cdnjs.cloudflare.com/ajax/libs/socket.io/1.4.5/socket.io.min.js'
path.join(path.dirname(__filename), '/socket.io.min.js')
, 'utf8'
)
)
}
inject() {
const injectio = getInjectio()
console.error('injecting')
return this.execute(socketio)
.then(() => {
console.error('injected socketio')
this.execute(injectio, this.port)
})
return this.execute(injectio, this.port)
.then(() => {
console.error('injected injectio')
console.error('injected / call Wechaty.init()')
return this.execute('return Wechaty.init()')
})
.then(() => {
console.error('injected Wechaty()')
return new Promise((resolve, reject) => resolve())
}).then((data) => {
console.error('Wechaty.init() return: ' + data)
return new Promise((resolve, reject) => resolve(data))
})
}
quit() {
// console.error('Browser.quit')
if (this.driver) {
console.error('Browser.driver.quit')
// this.driver.quit()
delete this.driver
console.error('Browser.quit')
if (!this.driver) {
console.error('no need to quite because no driver')
return new Promise((resolve, reject) => resolve('no 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) {
......@@ -79,7 +75,7 @@ class Browser {
if (!this.driver)
throw new Error('driver not found')
// a promise
return this.driver.executeScript(script, args)
return this.driver.executeScript.apply(this.driver, arguments)
}
}
......
......@@ -19,27 +19,31 @@
408: 未确认
*/
;(function (port) {
;return (function (port) {
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 = {
// get all we need from wx in browser(angularjs)
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: {} // see glueAngular() function
// glue funcs
, getLoginStatusCode: function () { return loginScope.code }
, getLoginQrImgUrl: function () { return loginScope.qrcodeUrl }
, isLogined: function () { return 200===loginScope.code }
, isReady: function () { return !!(angular && angular.element && angular.element("body")); }
, getLoginStatusCode: function () { return Wechaty.glue.loginScope.code }
, getLoginQrImgUrl: function () { return Wechaty.glue.loginScope.qrcodeUrl }
, isLogined: function () { return 200===Wechaty.glue.loginScope.code /* MMCgi.isLogin ??? */}
, isReady: isReady
// variable
, socket: null
......@@ -47,40 +51,65 @@
// funcs
, init: init
, send: send
, zlog: zlog
, log: log
, clog: clog // Console log
, slog: slog // Socket IO log
, ping: ping
, quit: quit
}
function isReady() {
return (typeof angular)!=='undefined' && angular.element && angular.element("body")
}
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()
hookMessage()
zlog('Wechaty injected!. ;-D')
clog('inited!. ;-D')
return true
}
function createZlog() {
var enable = true
if (enable) {
if (!console.memory && console.time) { // wechat debuger exist
delete console
}
console.zlog = window.console.log
window.zlog = function (s) { return console.zlog(s) }
console.log = function () {}
} else {
window.zlog = function () {}
function glueAngular() {
var injector = angular.element(document).injector()
var rootScope = injector.get("$rootScope")
var http = injector.get("$http")
var chatFactory = injector.get("chatFactory")
var confFactory = injector.get("confFactory")
var loginScope = angular.element(".login_box").scope()
// get all we need from wx in browser(angularjs)
Wechaty.glue = {
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 ping() { return 'pong' }
function send (ToUserName, Content) {
function quit() {
if (Wechaty.socket) {
Wechaty.socket.close()
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 m = c.createMessage({
ToUserName: ToUserName
......@@ -89,58 +118,68 @@
c.appendMessage(m)
return c.sendMessage(m)
}
function hookMessage() {
var rootScope = Wechaty.glue.rootScope
var zlog = Wechaty.zlog
rootScope.$on("message:add:success", function (event, data) {
Wechaty.socket.emit('message', data)
.catch(function (e) {
zlog('socket.emit(message, data) fail:')
zlog(e)
clog('socket.emit(message, data) fail:')
clog(e)
})
})
}
function hookUnload() {
window.addEventListener ('unload', function (e) {
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
var socket = Wechaty.socket = io.connect('https://127.0.0.1:' + port)
var zlog = Wechaty.zlog
socket.on('connect', function() {
zlog('on connect entried')
clog('on connect entried')
// new message
Wechaty.glue.rootScope.$on("message:add:success", function (event, data) {
socket.emit('message', data)
})
socket.on('disconnect', function(e) {
zlog('event: socket disconnect')
// 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)
// ping -> pong. for test & live check purpose
socket.on('ping', function (e) {
clog('received socket io event: ping. emit pong...')
socket.emit('pong', 'pong')
})
// 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
var callback = arguments[arguments.length - 1]
if (typeof callback==='function')
callback('Wechaty')
return 'Wechaty'
}(arguments[0]))
}.apply(window, arguments))
......@@ -11,22 +11,35 @@ test('Browser class smoking tests', function (t) {
.then(() => {
t.ok(true, 'url opened')
b.inject()
.then(() => {
t.ok(true, 'wechaty injected')
b.execute('return 1+1')
.then(n => {
t.equal(n, 2, 'exec 1+1 in browser, equal 2')
b.execute('return 1+1')
.then(n => t.equal(n, 2, 'exec 1+1 in browser, equal 2'))
// b.inject().then( r => {
// console.log('injected: ' + r)
b.execute('return Wechaty && Wechaty.ping()')
.then(r => t.equal(r, 'pong', 'Wechaty.ping() returns pong'))
b.quit()
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.
先完成此消息的编辑!
想要评论请 注册