Skip to content

Commit

Permalink
1. setCookies & getCookies support & unit tests
Browse files Browse the repository at this point in the history
2. loadSession & saveSession support & unit tests
3. wechaty.reply() method support
  • Loading branch information
huan committed Jun 7, 2016
1 parent 494f476 commit c206f51
Show file tree
Hide file tree
Showing 14 changed files with 249 additions and 139 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,4 @@ coverage
0
t.js
t
tmp-chatbot-name-for-unit-testing.wechaty
3 changes: 1 addition & 2 deletions example/ding-dong-bot.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ bot
// logToFile(JSON.stringify(msg.rawObj))

if (/^(ding|ping|bing)$/i.test(m.get('content')) && !m.self()) {
const replyMsg = m.reply('dong')
bot.send(replyMsg)
bot.reply(m, 'dong')
.then(() => { log.warn('Bot', 'REPLY: dong') })
}
})
Expand Down
2 changes: 1 addition & 1 deletion example/roger-bot.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ bot.init()
console.log(`Use Wechat to Scan QR Code in url to login: ${code}\n${url}`)
})
.on('message', m => {
(!m.self()) && bot.send(m.reply('roger')) // 1. reply others' msg
(!m.self()) && bot.reply(m, 'roger') // 1. reply others' msg
.then(() => console.log(`RECV: ${m}, REPLY: "roger"`)) // 2. log message
.catch(e => console.error(e)) // 3. catch exception
})
Expand Down
2 changes: 1 addition & 1 deletion src/contact.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class Contact {
return !rawObj ? {} : {
id: rawObj.UserName
, uin: rawObj.Uin // stable id? 4763975
, weixin: rawObj.Alias
, weixin: rawObj.Alias // Wechat ID
, name: rawObj.NickName
, remark: rawObj.RemarkName
, sex: rawObj.Sex
Expand Down
18 changes: 0 additions & 18 deletions src/message.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,24 +81,6 @@ class Message {
return this.obj.self === this.obj.from
}

reply(replyContent) {
if (this.self()) {
throw new Error('dont reply message send by myself')
}
const m = new Message()
.set('content' , replyContent)

.set('from' , this.obj.to)
.set('to' , this.obj.from)
.set('room' , this.obj.room)

// FIXME: find a alternate way to check a message create by `self`
.set('self' , this.obj.self)

// console.log(m)
return m
}

ready() {
log.silly('Message', 'ready()')

Expand Down
1 change: 1 addition & 0 deletions src/puppet-web-bridge.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ class Bridge {
log.silly('Bridge', 'proxyWechaty: ' + wechatyScript)
return this.execute(wechatyScript)
}

execute(script, ...args) { return this.browser.execute(script, ...args) }
}

Expand Down
59 changes: 42 additions & 17 deletions src/puppet-web-browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,28 +23,16 @@ class Browser {
toString() { return `Class Browser({head:${this.head})` }

init() {
log.verbose('Browser', 'init()')

return this.initDriver()
.then(r => {
log.verbose('Browser', 'initDriver() done')
return this.open()
})
.then(r => {
log.verbose('Browser', 'open() done')
return true
.then(() => this)
.catch(e => { // XXX: must has a `.catch` here, or promise will hang! 2016/6/7
log.error('Browser', 'init() rejectec: %s', e)
throw e
})
}

open() {
const WX_URL = 'https://wx.qq.com'
log.verbose('Browser', `open()ing at ${WX_URL}`)

return this.driver.get(WX_URL)
}

initDriver() {
log.verbose('Browser', 'initDriver()')
log.verbose('Browser', 'init()')
if (this.head) {
this.driver = new WebDriver.Builder()
.setAlertBehavior('ignore')
Expand All @@ -56,6 +44,13 @@ class Browser {
return Promise.resolve(this.driver)
}

open() {
const WX_URL = 'https://wx.qq.com'
log.verbose('Browser', `open()ing at ${WX_URL}`)

return this.driver.get(WX_URL)
}

getPhantomJsDriver() {
// https://github.com/SeleniumHQ/selenium/issues/2069
// setup custom phantomJS capability
Expand Down Expand Up @@ -89,6 +84,7 @@ class Browser {
log.verbose('Browser', 'driver.quit() skipped because no driver')
return Promise.resolve('no driver')
} else if (!this.driver.getSession()) {
this.driver = null
log.verbose('Browser', 'driver.quit() skipped because no driver session')
return Promise.resolve('no driver session')
}
Expand Down Expand Up @@ -148,6 +144,35 @@ class Browser {
return Promise.reject(e)
}
}

getCookies() {
log.silly('Browser', 'getCookies()')
// console.trace("#################")

if (!this.driver || !this.driver.getSession()) {
return Promise.reject('no driver or no session')
}

return this.driver.manage().getCookies()
.then(cookies => {
log.silly('Browser', 'getCookies() got %s cookies', cookies.length)
return cookies
})
.catch(e => {
log.error('Browser', 'getCookies %s', e)
throw e
})
}

setCookies(cookies) { return this.setCookie(cookies) }
setCookie(cookie) {
if (cookie.map) {
return cookie.map(c => {
return this.setCookie(c)
})
}
return this.driver.manage().addCookie(cookie.name, cookie.value, cookie.path, cookie.domain, cookie.secure, cookie.expiry)
}
}

module.exports = Browser
Loading

0 comments on commit c206f51

Please sign in to comment.