gpt4 book ai didi

testing - botium webdriverio 连接器的覆盖操作

转载 作者:行者123 更新时间:2023-11-28 21:18:51 31 4
gpt4 key购买 nike

编辑:将主题更改为覆盖 botium-webdriverio-connector 上的基本功能


如何触发按钮点击?

从这个链接: https://github.com/codeforequity-at/botium-core/wiki/Botium-Scripting

它说你只需要放

#me
BUTTON btnTitle

但实际发生的是它将字面量 BUTTON btnTitle 作为文本发送给机器人

not clicking button

对于上下文,我实现了一个自定义的 WEBDRIVERIO_GETBOTMESSAGE 并发回这个

{
"sender": "bot",
"buttons": [
{
"text": "reply1"
},
{
"text": "reply2"
}
],
"cards": [],
"media": []
}

最佳答案

我决定覆盖 WEBDRIVERIO_SENDTOBOT 函数。在 botium-cli 0.0.44 上运行,可能在较新的版本上有更好的解决方案。


编辑:添加更多细节、源代码和实现

最后,我覆盖了这些函数 WEBDRIVERIO_OPENBOT WEBDRIVERIO_SENDTOBOT WEBDRIVERIO_GETBOTMESSAGE 并且还添加了一个自定义断言器来断言图库消息

botium.json

{
"botium": {
"Capabilities": {
"PROJECTNAME": "Production Test",
"CONTAINERMODE": "webdriverio",
"WEBDRIVERIO_OPTIONS": {
"desiredCapabilities": {
"browserName": "chrome"
}
},
"WEBDRIVERIO_URL": "the bot url",
"WEBDRIVERIO_OPENBOT": "./actions/open_test",
"WEBDRIVERIO_IGNOREWELCOMEMESSAGES": 1,
"WEBDRIVERIO_SENDTOBOT": "./actions/send",
"WEBDRIVERIO_GETBOTMESSAGE": "./actions/parse_response",
"WEBDRIVERIO_START_SELENIUM": true,
"WEBDRIVERIO_START_SELENIUM_OPTS": {
"drivers": {
"chrome": {
"version": "2.36"
}
}
},
"ASSERTERS": [
{
"ref": "GALLERY",
"src": "./asserters/gallery",
"global": true
}
]
}
}
}

./actions/open_test

module.exports = (container, browser) => {
return browser
.waitForVisible('#toggle-chat', 20000)
.click('#toggle-chat') // click chat button
.pause(2000)
.waitForVisible('#get-started', 20000)
.click('#get-started') // click get started, initiate the chat
.pause(2000)
}

./actions/发送


module.exports = (container, browser, msg) => {

if (msg.messageText && msg.messageText !== '' && msg.messageText.indexOf('BUTTON') !== 0) { // send text message
return browser
.waitForEnabled('#text-input', 10000)
.setValue('#text-input', msg.messageText)
.keys('Enter')
} else if (msg.messageText.indexOf('BUTTON') === 0) { // if message started with "BUTTON xxx", safe to assume the tester want to click button xxx
let buttonTitle = msg.messageText.split(' ')[1]
if (!buttonTitle) throw new Error('BUTTON invalid (1)')

return browser.waitForEnabled(`[chatmessagebuttontitle="${ buttonTitle }"]`, 10000)
.click(`[chatmessagebuttontitle="${ buttonTitle }"]`)
} else { // unhandled, send arbitary message
return browser
.waitForEnabled('#text-input', 10000)
.setValue('#text-input', 'unhandled')
.keys('Enter')
}

}

./actions/parse_response

module.exports = (container, browser, elementId) => {

const botMsg = { sender: 'bot', buttons: [], cards: [], media: [] }

/**
*
* ... parsing some html here to populate buttons, media and cards
* ... can't put all the code for security reasons, sorry
* ... here are example
*
* btw, elementId is NOT an html id attribute, so cannot query with "#" + elementId
* cannot find documentation for webdriver elementId, but this page helps
* http://v4.webdriver.io/api/protocol/elementIdElements.html
*
*/

browser.elementIdElements(elementId, '.bot-bubble')
.then(elements => elements.value)
.then(elements =>
Promise.all(
elements.map(element =>
browser.elementIdText(element.ELEMENT).then(text => text.value)
)
).then(messages => {
// i only need the first message, also this function only called for each bot message bubble, so safe to assume there is only 1 message
if (messages.length > 0) botMsg.messageText = messages[0]

// send the bot response back to botium
container.BotSays(botMsg)
})
)

}

./断言者/画廊

/**
* @typedef Card
* @property {String} image
* @property {String} title
* @property {String} subtitle
* @property {Array<String>} buttons
*
* @typedef BotMesage
* @property {Array<Card>} cards
*/

/**
* @typedef GalleryAssertStepParam
* @property {*} convo
* @property {Array<String>} args
* @property {BotMesage} botMsg
*/

module.exports = class GalleryAsserter {

/**
* this function is called whenever parse_response.js finishes
* @param {GalleryAssertStepParam} param
*/
assertConvoStep(param) {
let args = param.args
let botMsg = param.botMsg

// args needs to be an array, simple check
if (!args.concat) return Promise.reject(new Error('args for GALLERY is not an array'))
if (args.length > botMsg.cards.length) return Promise.reject(new Error('number of gallery cards doesnt match. expecting ' + args.length +'. Got ' + botMsg.cards.length))

// basic logic to check if the card that is passed from parse_response.js is equals to card that is written in test scripts
for (var i = 0; i < args.length; i++) {

// ParseGalleryString is a function that convert arbitary string to a Card object
// example of the arbitary string: "( title=some title, subtitle= some subtitle, image=image url, buttons=btn1, btn2, btn3 )"
// will be converted to JSON { title: "some title", subtitle: "some subtitle", ... }
let card = ParseGalleryString(args[i])
let testcard = botMsg.cards[i]

if (card.image !== testcard.image) return Promise.reject(new Error(`card[${i}] doesn't pass. expecting image to be ${ card.image }, got ${ testcard.image }`))
if (card.title !== testcard.title) return Promise.reject(new Error(`card[${i}] doesn't pass. expecting title to be ${ card.title }, got ${ testcard.title }`))
if (card.subtitle !== testcard.subtitle) return Promise.reject(new Error(`card[${i}] doesn't pass. expecting subtitle to be ${ card.subtitle }, got ${ testcard.subtitle }`))

if (card.buttons.length !== testcard.buttons.length) return Promise.reject(new Error(`card[${i}] doesn't pass. expecting ${ card.buttons.length }(${card.buttons.join(', ')}) buttons, got ${ testcard.buttons.length }(${testcard.buttons.join(', ')})`))
if (card.buttons.join('_') !== testcard.buttons.join('_')) return Promise.reject(new Error(`card[${i}] doesn't pass. expecting buttons to be ${ card.buttons.join(', ') }, got ${ testcard.buttons.join(', ') }`))
}

return Promise.resolve()
}

}

然后使用自定义断言器测试画廊.convo.txt

Test Gallery

#me
show me some cat breeds

#bot
here are some cat breeds

#bot
GALLERY (image=http://cat.pic/1.png, title=Alaskan Malmute, subtitle=This is actually not a cat, buttons=Pet, Give Food, Info) | (image=http://cat.pic/2.png, title=Cobra Kai, subtitle=This is actually a movie, buttons=Watch, Like, Dislike)

此设置适用于 botium-cli 版本 0.0.44尝试更新到 botium 0.0.45 但在 selenium 驱动程序上出现一些错误

关于testing - botium webdriverio 连接器的覆盖操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54645876/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com