i trying connect mysql database in cucumber-js stepdefinition file, using node-mysql, via connection method defined in world.js. unfortunately not connect , return query results, nor can show me error message me figure out going wrong.
i have used cucumber-jvm in past, new javascript , node.js. have created standalone js file same method present (to mimic world.js), , called test script (to mimic stepdefinitions.js); connects , returns record happily, down misunderstanding of context within javascript in general, or within way cucumber-js uses world, perhaps...
stepdefinitons.js:
'use strict'; var importstepdefinitions = function() { this.world = require('../support/world.js').world; this.given(/^i have clean magento database$/, function (callback) { this.connecttodatabase(); //this.cleardatabase(); callback(); }); this.when(/^i import test data file "([^"]*)"$/, function (testdatafile, callback) { //this.importtestfile(); callback.pending(); }); this.then(/^an attribute set created called "([^"]*)"$/, function (attributesetname, callback) { //this.validateattributeset(); callback.pending(); }); }; module.exports = importstepdefinitions; world.js:
'use strict'; var world = function world(callback) { var mysql = require('mysql'); var host = 'localhost'; var user = 'user'; var password = 'password'; var database = 'magento'; var connection = mysql.createconnection({ host: host, user: user, password: password, database: database }); this.connecttodatabase = function () { connection.connect(function(err){ if(err) { console.error("connection error: " + err.stack); return; } console.log("connection state 1 = ", connection); }); connection.query('select * admin_user', function (err, rows, fields) { if (err) { console.error("query error: " + err.stack); return; } console.log("rows ====== ", rows[0]); recorddetail = rows[0]; }); console.log("connection state 2 = ", connection); connection.end(); }; this.cleardatabase = function(scriptname) { //run import script in sql starting fresh magento database }; this.importtestfile = function(testfilename) { //need call import.php file location }; this.validateattributeset = function(attributesetname) { //use magento object (?) check attribute set exists expected }; callback(); }; module.exports.world = world; when execute code nothing connection.connect(), neither error or log, no record logged out connection.query(), , connection @ 'connection state 2 =' shows (right @ bottom) _connectcalled value of true, state of disconnected , threadid of null:
connection state 2 = { domain: { domain: null, _events: { error: [function: handlescenarioexception] }, _maxlisteners: undefined, members: [], id: 'domain-1437457225974' }, _events: {}, _maxlisteners: undefined, config: { host: 'localhost', port: 3306, localaddress: undefined, socketpath: undefined, user: 'user', password: 'password', database: 'magento', connecttimeout: 10000, insecureauth: false, supportbignumbers: false, bignumberstrings: false, datestrings: false, debug: undefined, trace: true, stringifyobjects: false, timezone: 'local', flags: '', queryformat: undefined, pool: undefined, ssl: false, multiplestatements: false, typecast: true, maxpacketsize: 0, charsetnumber: 33, clientflags: 455631 }, _socket: { _connecting: true, _haderror: false, _handle: { fd: -1, reading: false, owner: [circular], onread: [function: onread], onconnection: null, writequeuesize: 0 }, _parent: null, _host: 'localhost', _readablestate: { objectmode: false, highwatermark: 16384, buffer: [], length: 0, pipes: null, pipescount: 0, flowing: true, ended: false, endemitted: false, reading: false, sync: true, needreadable: false, emittedreadable: false, readablelistening: false, defaultencoding: 'utf8', ranout: false, awaitdrain: 0, readingmore: false, decoder: null, encoding: null, resumescheduled: true }, readable: false, domain: { domain: null, _events: [object], _maxlisteners: undefined, members: [], id: 'domain-1437457225974' }, _events: { end: [object], finish: [function: onsocketfinish], _socketend: [function: onsocketend], data: [function], error: [function], connect: [object], timeout: [object] }, _maxlisteners: undefined, _writablestate: { objectmode: false, highwatermark: 16384, needdrain: false, ending: false, ended: false, finished: false, decodestrings: false, defaultencoding: 'utf8', length: 0, writing: false, corked: 0, sync: true, bufferprocessing: false, onwrite: [function], writecb: null, writelen: 0, bufferedrequest: null, lastbufferedrequest: null, pendingcb: 0, prefinished: false, erroremitted: false }, writable: true, allowhalfopen: false, destroyed: false, bytesread: 0, _bytesdispatched: 0, _pendingdata: null, _pendingencoding: '', _idletimeout: 10000, _idlenext: { _idlenext: [circular], _idleprev: [circular] }, _idleprev: { _idlenext: [circular], _idleprev: [circular] }, _idlestart: 2735419 }, _protocol: { domain: { domain: null, _events: [object], _maxlisteners: undefined, members: [], id: 'domain-1437457225974' }, _events: { data: [function], end: [object], handshake: [function], unhandlederror: [function], drain: [function], enqueue: [function] }, _maxlisteners: undefined, readable: true, writable: true, _config: { host: 'localhost', port: 3306, localaddress: undefined, socketpath: undefined, user: 'user', password: 'password', database: 'magento', connecttimeout: 10000, insecureauth: false, supportbignumbers: false, bignumberstrings: false, datestrings: false, debug: undefined, trace: true, stringifyobjects: false, timezone: 'local', flags: '', queryformat: undefined, pool: undefined, ssl: false, multiplestatements: false, typecast: true, maxpacketsize: 0, charsetnumber: 33, clientflags: 455631 }, _connection: [circular], _callback: null, _fatalerror: null, _quitsequence: null, _handshakesequence: { domain: [object], _events: [object], _maxlisteners: undefined, _callback: [object], _callsite: [error], _ended: false, _timeout: undefined, _idlenext: null, _idleprev: null, _idlestart: null, _idletimeout: undefined, _repeat: null, _config: [object], _handshakeinitializationpacket: null }, _handshaked: false, _ended: false, _destroyed: false, _queue: [ [object], [object] ], _handshakeinitializationpacket: null, _parser: { _supportbignumbers: false, _buffer: <buffer >, _longpacketbuffers: [], _offset: 0, _packetend: null, _packetheader: null, _packetoffset: null, _onerror: [function], _onpacket: [function], _nextpacketnumber: 0, _encoding: 'utf-8', _paused: false } }, _connectcalled: true, state: 'disconnected', threadid: null }
ok, believe have found solution, posting here else struggling in similar manner. please feel free post better explanation of why , happening here if have one!
i had not implemented callback properly, detailed cucumber.js documentation; need pass function being called, , use signify when called function has completed:
stepdefinitions.js:
... this.given(/^i have clean magento database$/, function (callback) { //callback passed function this.connecttodatabase(callback); ... world.js:
... //callback passed function this.connecttodatabase = function(callback) { var host = 'localhost'; var user = 'user'; var password = 'password'; var database = 'magento'; var connection = mysql.createconnection({ host: host, user: user, password: password, database: database }); connection.connect(function(err){ if(err) { console.error("connection error: " + err.stack); return; } }); connection.query('select * admin_user', function (err, rows) { if (err) { console.error("query error: " + err.stack); } else { console.log(rows[0]); } }); /*callback used signify end of function, ensuring actions completed before rest of scenario steps completed cucumber.js*/ connection.end(callback); }; ....
Comments
Post a Comment