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

merge

......@@ -41,7 +41,7 @@ coverage
.nyc_output
# Wechaty session file
*.wechaty.json
demo.wechaty.json
# Cloud 9
.c9/
......
......@@ -44,7 +44,7 @@ script:
- npm --version
- shellcheck bin/*.sh
- npm run lint
- npm run redist
- npm run dist
- if [ "$TRAVIS_OS_NAME" == 'linux' ]; then WECHATY_LOG=silly npm run test:phantomjs; fi
- if [ "$TRAVIS_OS_NAME" == 'linux' ]; then WECHATY_LOG=silly npm run test:chrome; fi
- if [ "$TRAVIS_OS_NAME" == 'osx' ]; then WECHATY_LOG=verbose npm run nycava; fi
......@@ -67,7 +67,7 @@ after_success:
before_deploy:
- npm version
- npm run redist
- npm run dist
deploy:
provider: npm
......
......@@ -13,10 +13,10 @@ import { createWriteStream } from 'fs'
const QrcodeTerminal = require('qrcode-terminal')
import {
Config
, Contact
, Wechaty
, log
Config,
Contact,
Wechaty,
log,
} from '../'
const welcome = `
......
......@@ -16,8 +16,7 @@
},
"scripts": {
"ts-node": "ts-node",
"dist": "tsc && shx cp package.json dist/ && shx cp src/puppet-web/*.js dist/src/puppet-web/",
"redist": "npm run clean && npm run dist",
"dist": "npm run clean && tsc && shx cp package.json dist/ && shx cp src/puppet-web/*.js dist/src/puppet-web/",
"coverage": "nyc report --reporter=text-lcov | coveralls",
"doctor": "ts-node bin/doctor",
"clean": "shx rm -fr dist/*",
......@@ -100,7 +99,7 @@
"node": ">= 6.9.0"
},
"dependencies": {
"@types/selenium-webdriver": "2.53.34",
"@types/selenium-webdriver": "2.53.37",
"@types/socket.io": "1.4.27",
"body-parser": "1.15.2",
"brolog": "0.3.10",
......@@ -119,9 +118,9 @@
"@types/express": "4.0.33",
"@types/fluent-ffmpeg": "0.0.2",
"@types/mime": "0.0.29",
"@types/node": "6.0.45",
"@types/node": "6.0.52",
"@types/request": "0.0.36",
"@types/sinon": "1.16.31",
"@types/sinon": "1.16.33",
"@types/ws": "0.0.37",
"apiai": "3.0.3",
"ava": "0.17.0",
......@@ -140,10 +139,10 @@
"sinon": "1.17.5",
"sloc": "0.1.11",
"ts-node": "1.4.1",
"tslint": "4.0.2",
"tslint": "4.1.1",
"tuling123-client": "0.0.1",
"typescript": "2.1.4",
"yarn": "0.17.10"
"yarn": "0.18.1"
},
"files_comment__whitelist_npm_publish": "http://stackoverflow.com/a/8617868/1123955",
"files": [
......
......@@ -8,7 +8,7 @@
import { test } from 'ava'
import {
Config
Config
} from './config'
import { Puppet } from './puppet'
......
/**
* Wechaty - Wechat for Bot. Connecting ChatBots
*
* Licenst: ISC
* https://github.com/wechaty/wechaty
*
*/
import { test } from 'ava'
import {
BrowserCookie
} from './browser-cookie'
import { BrowserDriver } from './browser-driver'
test('hostname() for wx.qq.com', async t => {
const driver = <BrowserDriver>{}
const browserCookie = new BrowserCookie(driver, 'test/fixture/profile/qq.wechaty.json')
const hostname = await browserCookie.hostname()
t.is(hostname, 'wx.qq.com', 'should get wx.qq.com')
})
test('hostname() for wechat.com', async t => {
const driver = <BrowserDriver>{}
const browserCookie = new BrowserCookie(driver, 'test/fixture/profile/wechat.wechaty.json')
const hostname = await browserCookie.hostname()
t.is(hostname, 'web.wechat.com', 'should get web.wechat.com')
})
test('hostname() for default', async t => {
const driver = <BrowserDriver>{}
const browserCookie = new BrowserCookie(driver)
const hostname = await browserCookie.hostname()
t.is(hostname, 'wx.qq.com', 'should get wx.qq.com')
})
test('hostname() for file not exist', async t => {
const driver = <BrowserDriver>{}
const browserCookie = new BrowserCookie(driver, 'file-not-exist.wechaty.json')
const hostname = await browserCookie.hostname()
t.is(hostname, 'wx.qq.com', 'should get wx.qq.com for non exist file')
})
......@@ -33,7 +33,10 @@ export type CookieType = {
}
export class BrowserCookie {
constructor(private driver: BrowserDriver, private storeFile?: string) {
constructor(
private driver: BrowserDriver,
private storeFile?: string,
) {
log.verbose('PuppetWebBrowserCookie', 'constructor(%s, %s)'
, driver.constructor.name
, storeFile ? storeFile : ''
......@@ -198,7 +201,11 @@ export class BrowserCookie {
return defaultHostname
}
const domain = wxCookieList[0].domain.slice(1)
let domain = wxCookieList[0].domain.slice(1)
if (domain === 'wechat.com') {
domain = 'web.wechat.com'
}
log.silly('PuppetWebBrowserCookie', 'hostname() got %s', domain)
return domain
......
......@@ -10,6 +10,7 @@
import {
Builder,
Capabilities,
logging,
WebDriver,
} from 'selenium-webdriver'
......@@ -29,20 +30,6 @@ export class BrowserDriver {
public async init(): Promise<this> {
log.verbose('PuppetWebBrowserDriver', 'init() for head: %s', this.head)
// if (this.driver) {
// try {
// // const valid = await this.valid(this.driver)
// // if (valid) {
// // // await this.driver.close()
// await this.driver.quit()
// // }
// } catch (e) {
// log.verbose('PuppetWebBrowserDriver', 'init() this.driver.quit() soft exception: %s'
// , e.message
// )
// }
// }
switch (this.head) {
case 'phantomjs':
this.driver = await this.getPhantomJsDriver()
......@@ -108,6 +95,19 @@ export class BrowserDriver {
const customChrome = Capabilities.chrome()
.set('chromeOptions', options)
// TODO: chromedriver --silent
if (!/^(verbose|silly)$/i.test(log.level())) {
const prefs = new logging.Preferences()
prefs.setLevel(logging.Type.BROWSER , logging.Level.OFF)
prefs.setLevel(logging.Type.CLIENT , logging.Level.OFF)
prefs.setLevel(logging.Type.DRIVER , logging.Level.OFF)
prefs.setLevel(logging.Type.PERFORMANCE , logging.Level.OFF)
prefs.setLevel(logging.Type.SERVER , logging.Level.OFF)
customChrome.setLoggingPrefs(prefs)
}
/**
* XXX when will Builder().build() throw exception???
*/
......@@ -335,53 +335,12 @@ export class BrowserDriver {
return true
}
// public driver1(): WebDriver
// public driver1(empty: null): void
// public driver1(newDriver: WebDriver): WebDriver
// public driver1(newDriver?: WebDriver | null): WebDriver | void {
// if (newDriver !== undefined) {
// log.verbose('PuppetWebBrowserDriver', 'driver(%s)'
// , newDriver
// ? newDriver.constructor.name
// : null
// )
// }
// if (newDriver !== undefined) {
// if (newDriver) {
// this.driver = newDriver
// return this.driver
// } else { // null
// if (this.driver && this.driver.getSession()) {
// throw new Error('driver still has session, can not set null')
// }
// this.driver = null
// return
// }
// }
// if (!this.driver) {
// const e = new Error('no driver')
// log.warn('PuppetWebBrowserDriver', 'driver() exception: %s', e.message)
// throw e
// }
// // if (!this.driver.getSession()) {
// // const e = new Error('no driver session')
// // log.warn('PuppetWebBrowserDriver', 'driver() exception: %s', e.message)
// // this.driver.quit()
// // throw e
// // }
// return this.driver
// }
public close() { return this.driver.close() }
public close() { return this.driver.close() as any as Promise<void> }
public executeAsyncScript(script: string|Function, ...args: any[]) { return this.driver.executeAsyncScript.apply(this.driver, arguments) }
public executeScript (script: string|Function, ...args: any[]) { return this.driver.executeScript.apply(this.driver, arguments) }
public get(url: string) { return this.driver.get(url) }
public getSession() { return this.driver.getSession() }
public manage() { return this.driver.manage() }
public navigate() { return this.driver.navigate() }
public quit() { return this.driver.quit() }
public get(url: string) { return this.driver.get(url) as any as Promise<void> }
public getSession() { return this.driver.getSession() as any as Promise<void> }
public manage() { return this.driver.manage() as any }
public navigate() { return this.driver.navigate() as any }
public quit() { return this.driver.quit() as any as Promise<void> }
}
......@@ -37,13 +37,13 @@ export class Browser extends EventEmitter {
private cookie: BrowserCookie
public driver: BrowserDriver
public hostname = 'wx.qq.com'
public hostname: string
public state = new StateMonitor<'open', 'close'>('Browser', 'close')
constructor(private setting: BrowserSetting = {
head: Config.head
, sessionFile: ''
head: Config.head,
sessionFile: '',
}) {
super()
log.verbose('PuppetWebBrowser', 'constructor() with head(%s) sessionFile(%s)', setting.head, setting.sessionFile)
......@@ -118,6 +118,10 @@ export class Browser extends EventEmitter {
public async open(url: string = `https://${this.hostname}`): Promise<void> {
log.verbose('PuppetWebBrowser', `open(${url})`)
if (!this.hostname) {
throw new Error('hostname unknown')
}
// TODO: set a timer to guard driver.get timeout, then retry 3 times 201607
try {
await this.driver.get(url)
......
[{"domain":".qq.com","expiry":2147385600,"httpOnly":false,"name":"pgv_pvi","path":"/","secure":false,"value":"8512099328"},{"domain":".qq.com","httpOnly":false,"name":"pgv_si","path":"/","secure":false,"value":"s5248662528"},{"domain":".wx.qq.com","expiry":1482380243,"httpOnly":false,"name":"mm_lang","path":"/","secure":false,"value":"en"}]
[{"domain":".wechat.com","expiry":1482380361,"httpOnly":false,"name":"mm_lang","path":"/","secure":false,"value":"en_US"},{"domain":".wechat.com","expiry":1797697161,"httpOnly":false,"name":"webwx_auth_ticket","path":"/","secure":false,"value":"CIsBEIW4yZYPGoABjgOYg33uUb4ct38aQsfT/zJ8FDNF8yKX4o4sJT7aVXjvBUjBLC/SXBj38TSCEmWsK8DME2FzDUl4kS2vXalfNnAFdScEuveH/h7T2CbHbvv5O4fDfxW2Olb5OQoWLrS61Wkx0XjghelAhyusVCGHiRVTtHaCx4gfyHk4+QyRiYc="},{"domain":".wechat.com","expiry":1797697161,"httpOnly":false,"name":"webwxuvid","path":"/","secure":false,"value":"e2108c64dbecd6ea0e3bc7a0266f467dcc63350fbc55559cdf6c25afd6f8bd88c7216d3aaec6427e41c2b918872bb549"},{"domain":".wechat.com","expiry":1482380361,"httpOnly":false,"name":"webwx_data_ticket","path":"/","secure":false,"value":"gScvXIoeHANgdX4Dq5kOjAGY"},{"domain":".wechat.com","httpOnly":false,"name":"pgv_si","path":"/","secure":false,"value":"s4528565248"},{"domain":".wechat.com","expiry":1482380361,"httpOnly":false,"name":"wxloadtime","path":"/","secure":false,"value":"1482337161"},{"domain":".wechat.com","expiry":1482380361,"httpOnly":false,"name":"wxsid","path":"/","secure":false,"value":"GURmztH40ijJuL3f"},{"domain":".wechat.com","expiry":2147385600,"httpOnly":false,"name":"pgv_pvi","path":"/","secure":false,"value":"6397004800"},{"domain":".wechat.com","expiry":1482380361,"httpOnly":false,"name":"wxuin","path":"/","secure":false,"value":"278208447"}]
......@@ -25,6 +25,8 @@ test('Cookie smoking test', async t => {
t.truthy(browser, 'should instanciate a browser instance')
browser.state.target('open')
browser.hostname = 'wx.qq.com'
await browser.driver.init()
t.pass('should init driver')
......@@ -100,6 +102,7 @@ test('Cookie save/load', async t => {
t.truthy(browser, 'should get a new Browser')
browser.state.target('open')
browser.hostname = 'wx.qq.com'
await browser.driver.init()
t.pass('should init driver')
......@@ -169,6 +172,7 @@ test('Cookie save/load', async t => {
t.pass('should started a new Browser')
browser.state.target('open')
browser.hostname = 'wx.qq.com'
await browser.driver.init()
t.pass('should inited the new Browser')
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册