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

add unit test for clickSwitchAccount

上级 bc78fab5
......@@ -23,6 +23,10 @@ import * as test from 'blue-tape'
// import * as sinon from 'sinon'
// const sinonTest = require('sinon-test')(sinon)
import {
launch,
} from 'puppeteer'
import Profile from '../profile'
import Bridge from './bridge'
......@@ -42,20 +46,17 @@ test('testBlockedMessage()', async t => {
<message>当前登录环境异常。为了你的帐号安全,暂时不能登录web微信。你可以通过手机客户端或者windows微信登录。</message>
</error>
`
const BLOCKED_TEXT_ZH = [
'当前登录环境异常。为了你的帐号安全,暂时不能登录web微信。',
'你可以通过手机客户端或者windows微信登录。',
].join('')
// tslint:disable:max-line-length
const BLOCKED_XML_EN = `
<error>
<ret>1203</ret>
<message>For account security, newly registered WeChat accounts are unable to log in to Web WeChat. To use WeChat on a computer, use Windows WeChat or Mac WeChat at http://wechat.com</message>
</error>
`
`
const BLOCKED_TEXT_EN = [
'For account security, newly registered WeChat accounts are unable to log in to Web WeChat.',
' To use WeChat on a computer, use Windows WeChat or Mac WeChat at http://wechat.com',
......@@ -97,3 +98,43 @@ test('testBlockedMessage()', async t => {
}
})
})
test('clickSwitchAccount()', async t => {
const SWITCH_ACCOUNT_HTML = `
<div class="association show" ng-class="{show: isAssociationLogin &amp;&amp; !isBrokenNetwork}">
<img class="img" mm-src="" alt="" src="//res.wx.qq.com/a/wx_fed/webwx/res/static/img/2KriyDK.png">
<p ng-show="isWaitingAsConfirm" class="waiting_confirm ng-hide">Confirm login on mobile WeChat</p>
<a href="javascript:;" ng-show="!isWaitingAsConfirm" ng-click="associationLogin()" class="button button_primary">Log in</a>
<a href="javascript:;" ng-click="qrcodeLogin()" class="button button_default">Switch Account</a>
</div>
`
const profile = new Profile()
const bridge = new Bridge({ profile} )
test('switch account', async t => {
const browser = await launch()
const page = await browser.newPage()
await page.setContent(SWITCH_ACCOUNT_HTML)
const clicked = await bridge.clickSwitchAccount(page)
await page.close()
await browser.close()
t.equal(clicked, true, 'should click the switch account button')
})
test('switch account', async t => {
const browser = await launch()
const page = await browser.newPage()
await page.setContent('<h1>ok</h1>')
const clicked = await bridge.clickSwitchAccount(page)
await page.close()
await browser.close()
t.equal(clicked, false, 'should no button found')
})
})
......@@ -24,6 +24,7 @@ import {
Browser,
Cookie,
Dialog,
ElementHandle,
launch,
Page,
} from 'puppeteer'
......@@ -643,46 +644,49 @@ export class Bridge extends EventEmitter {
})
}
public async clickSwitchAccount(): Promise<boolean> {
public async clickSwitchAccount(page: Page = this.page): Promise<boolean> {
log.verbose('PuppetWebBridge', 'clickSwitchAccount()')
// https://github.com/GoogleChrome/puppeteer/issues/537#issuecomment-334918553
async function waitForXpath(page: Page, xpath: string) {
const resultsHandle = await (page as any).evaluateHandle(xpathInner => {
const results = [] as any
const query = document.evaluate(xpathInner, document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
async function listXpath(thePage: Page, xpath: string): Promise<ElementHandle[]> {
const nodeHandleList = await (thePage as any).evaluateHandle(xpathInner => {
const nodeList: Node[] = []
const query = document.evaluate(xpathInner, document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null)
for (let i = 0, length = query.snapshotLength; i < length; ++i) {
results.push(query.snapshotItem(i));
nodeList.push(query.snapshotItem(i))
}
return results;
}, xpath);
const properties = await resultsHandle.getProperties();
const result = [] as any
const releasePromises = [] as any
return nodeList
}, xpath)
const properties = await nodeHandleList.getProperties()
const elementHandleList: ElementHandle[] = []
const releasePromises: Promise<void>[] = []
for (const property of properties.values()) {
const element = property.asElement();
const element = property.asElement()
if (element)
result.push(element);
elementHandleList.push(element)
else
releasePromises.push(property.dispose());
releasePromises.push(property.dispose())
}
await Promise.all(releasePromises);
return result;
await Promise.all(releasePromises)
return elementHandleList
}
const XPATH_SELECTOR = `//div[contains(@class,'association') and contains(@class,'show')]/a[@ng-click='qrcodeLogin()']`
// const DOM_
try {
// const button = await this.page.$(XPATH_SELECTOR)
const [button] = await waitForXpath(this.page, XPATH_SELECTOR)
await button.click()
// const button = await this.driver.driver.findElement(By.xpath(
// "//div[contains(@class,'association') and contains(@class,'show')]/a[@ng-click='qrcodeLogin()']"))
log.silly('PuppetWebBridge', 'clickSwitchAccount() clicked!')
return true
const [button] = await listXpath(page, XPATH_SELECTOR)
if (button) {
await button.click()
log.silly('PuppetWebBridge', 'clickSwitchAccount() clicked!')
return true
} else {
log.silly('PuppetWebBridge', 'clickSwitchAccount() button not found')
return false
}
} catch (e) {
log.silly('PuppetWebBridge', 'clickSwitchAccount() button not found: %s', e)
return false
log.silly('PuppetWebBridge', 'clickSwitchAccount() exception: %s', e)
throw e
}
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册