Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
weixin_46011646
wechaty
提交
b67d84d2
W
wechaty
项目概览
weixin_46011646
/
wechaty
与 Fork 源项目一致
Fork自
wechaty / wechaty
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
W
wechaty
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
b67d84d2
编写于
5月 08, 2016
作者:
Huan (李卓桓)
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
webdriver smoking test
上级
0a2a2b96
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
194 addition
and
102 deletion
+194
-102
lib/puppet-web-browser.js
lib/puppet-web-browser.js
+23
-27
lib/puppet-web-injectio.js
lib/puppet-web-injectio.js
+103
-64
tests/puppet-web-browser-tests.js
tests/puppet-web-browser-tests.js
+24
-11
tests/webdriver-tests.js
tests/webdriver-tests.js
+44
-0
未找到文件。
lib/puppet-web-browser.js
浏览文件 @
b67d84d2
...
@@ -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/
'
// 'http
s://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
,
arg
s
)
return
this
.
driver
.
executeScript
.
apply
(
this
.
driver
,
argument
s
)
}
}
}
}
...
...
lib/puppet-web-injectio.js
浏览文件 @
b67d84d2
...
@@ -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
:
z
log
,
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
)
{
z
log
(
'
socket.emit(message, data) fail:
'
)
c
log
(
'
socket.emit(message, data) fail:
'
)
z
log
(
e
)
c
log
(
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
()
{
z
log
(
'
on connect entried
'
)
c
log
(
'
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
))
tests/puppet-web-browser-tests.js
浏览文件 @
b67d84d2
...
@@ -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()
// })
})
})
})
})
tests/webdriver-tests.js
0 → 100755
浏览文件 @
b67d84d2
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.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录