Botium in a Nutshell - Botium Core Demo

node v8.17.0
version: 1.0.0
endpointsharetweet
The first part contains some setup work and the simulator for our chatbot. The chatbot is a rather simple one: * when telling him "show me buttons" or "give me buttons", it will reply with some buttons to click * when telling him "show me a picture" or "picture", it will reply with a picture * when telling him "show me a card" or "card", it will show you a card * in any other case, it just echos back what is sent The details of the implementation doesn't really matter for this example. What's important is that there is some kind of chatbot and Botium is connected to it, period.
const Mocha = require("mocha") const mochaReporter = require("mocha-unfunk-reporter") const BotDriver = require("botium-core").BotDriver const answers = [ { input: ['buttons', 'show me buttons', 'show me some buttons', 'give me buttons'], output: { messageText: 'Here are some buttons', buttons: [ { text: 'First Button' }, { text: 'Second Button' } ] } }, { input: ['picture', 'show me a picture', 'give me a picture'], output: { messageText: 'Here is a picture', media: [ { altText: 'Botium Logo', mediaUri: 'http://www.botium.at/img/logo.png' } ] } }, { input: ['card', 'show me a card', 'give me a card'], output: { messageText: 'Here is a card', cards: [ { text: 'Botium is great!', image: { mediaUri: 'http://www.botium.at/img/logo.png' }, buttons: [ { text: 'First Button' }, { text: 'Second Button' } ] } ] } } ] const bot = ({ queueBotSays }) => ({ UserSays (msg) { const template = answers.find((a) => a.input.indexOf(msg.messageText) >= 0) if (template) setTimeout(() => queueBotSays(Object.assign({}, { sender: 'bot', sourceData: msg }, template.output)), 0) else setTimeout(() => queueBotSays({ sender: 'bot', sourceData: msg, messageText: 'You said: ' + msg.messageText }), 0) } })
Here are the convo and utterances files. In real life, those would be read from a filesystem folder or a git repository.
const convos = [ `bot should say hello The file "hello.utterances.txt" contains a couple of synonyms for saying hello and is referenced with "UTT_HELLO": all possible synonyms are sent to the bot, and any synonym from the list is allowed as answer #me UTT_HELLO #bot UTT_HELLO `, `bot should show buttons The bot should present the user some buttons, and Botium validates the button text #me show me some buttons #bot Here are some buttons BUTTONS First Button|Second Button `, `bot should show a picture The bot should present the user a picture, and Botium validates the picture name #me show me a picture #bot Here is a picture MEDIA logo.png `, `bot should show a card The bot should present the user a card, a simple UI element showing text, picture and buttons #me show me a card #bot Here is a card MEDIA logo.png BUTTONS First Button|Second Button ` ]
const utterances = [ `UTT_HELLO hello hi howdy` ]
Now the BotDriver is initialized.
const driver = new BotDriver() .setCapability('PROJECTNAME', 'Botium Sample') .setCapability('CONTAINERMODE', bot)
Now we are compiling the convos and utterances from above. We are "expanding" them to make a separate convo out of each input utterance.
const compiler = driver.BuildCompiler() convos.forEach(c => compiler.Compile(c, 'SCRIPTING_FORMAT_TXT', 'SCRIPTING_TYPE_CONVO')) utterances.forEach(c => compiler.Compile(c, 'SCRIPTING_FORMAT_TXT', 'SCRIPTING_TYPE_UTTERANCES')) compiler.ExpandConvos() compiler.convos
The last part is to actually run the convos. In this example, a Mocha test suite is generated. With Botium CLI, Botium Box and Botium Bindings you don't have to do this yourself.
mochaReporter.option('style', 'none'); const mocha = new Mocha({ reporter: 'mocha-unfunk-reporter' }) const suite = Mocha.Suite.create(mocha.suite, 'Botium') suite.beforeAll((done) => { driver.Build() .then((container) => { suite.container = container done() }) .catch(done) }) suite.beforeEach((done) => { suite.container.Start().then(() => done()).catch(done) }) suite.afterEach((done) => { suite.container.Stop().then(() => done()).catch(done) }) suite.afterAll((done) => { suite.container.Clean().then(() => done()).catch(done) }) compiler.convos.forEach((convo) => { suite.addTest(new Mocha.Test(convo.header.name, (done) => { convo.Run(suite.container) .then(() => { //console.log(convo.header.name + ' ready, calling done function.') done() }) .catch((err) => { //console.log(convo.header.name + ' failed: ' + err) done(err) }) })) }) const runner = mocha.run() console.log('waiting for test results ...')
This was the Botium Core Demo. The last text block should now show the test results in a list.
Loading…

no comments

    sign in to comment