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

new bug: webdriver.get with promise has strange behaviour

上级 2dcc1963
......@@ -28,7 +28,7 @@ Please wait... I'm trying to login in...
`
console.log(welcome)
const bot = new Wechaty({head: true})
const bot = new Wechaty({head: 'chrome'})
bot
.on('login' , user => log.info('Bot', 'logined'))
......
......@@ -16,8 +16,13 @@ const retryPromise = require('retry-promise').default // https://github.com/ola
class Browser {
constructor(options) {
log.verbose('Browser', 'constructor()')
options = options || {}
this.head = options.head || false // default to headless
if (typeof options.head === 'undefined') {
this.head = false // default
} else {
this.head = options.head
}
}
toString() { return `Class Browser({head:${this.head})` }
......@@ -25,23 +30,42 @@ class Browser {
init() {
return this.initDriver()
.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
})
// XXX: if no `.catch` here, promise will hang!
// XXX: https://github.com/SeleniumHQ/selenium/issues/2233
.catch(e => throw e)
// console.log(p)
// return p.catch()
// .catch(e => { // XXX: must has a `.catch` here, or promise will hang! 2016/6/7
// log.error('Browser', 'init() rejectec: %s', e)
// throw e
// })
}
initDriver() {
log.verbose('Browser', 'init()')
log.verbose('Browser', 'initDriver(head: %s)', this.head)
if (this.head) {
this.driver = new WebDriver.Builder()
.setAlertBehavior('ignore')
.forBrowser('chrome')
.build()
if (/firefox/i.test(this.head)) {
this.driver = new WebDriver.Builder()
.setAlertBehavior('ignore')
.forBrowser('firefox')
.build()
} else { // default to chrome
this.driver = new WebDriver.Builder()
.setAlertBehavior('ignore')
.forBrowser('chrome')
.build()
}
} else {
this.driver = this.getPhantomJsDriver()
}
return Promise.resolve(this.driver)
// console.log(this.driver)
return new Promise((resolve, reject) => {
// XXX: if no `setTimeout()` here, promise will hang!
// XXX: https://github.com/SeleniumHQ/selenium/issues/2233
setTimeout(() => { resolve(this.driver) }, 0)
// resolve(this.driver)
})
}
open() {
......
......@@ -5,7 +5,7 @@ const co = require('co')
const test = require('tap').test
const log = require('npmlog')
// log.level = 'verbose'
// log.level = 'silly'
log.level = 'silly'
const WebDriver = require('selenium-webdriver')
const Browser = WebDriver.Browser
......@@ -14,6 +14,7 @@ const By = WebDriver.By
const PuppetWebBrowser = require('../src/puppet-web-browser')
const PuppetWebBridge = require('../src/puppet-web-bridge')
const PORT = 58788
const HEAD = 'chrome' // undefined
function driverProcessNum() {
return new Promise((resolve, reject) => {
......@@ -28,12 +29,13 @@ function driverProcessNum() {
test('WebDriver process create & quit test', function(t) {
co(function* () {
const b = new PuppetWebBrowser({port: PORT})
const b = new PuppetWebBrowser({port: PORT, head: HEAD})
t.ok(b, 'Browser instnace')
yield b.init()
t.pass('inited')
yield b.open()
t.pass('inited & opened')
t.pass('opened')
let n = yield driverProcessNum()
t.ok(n > 0, 'driver process exist')
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册