Best JavaScript code snippet using redwood
CIExecution.js
Source:CIExecution.js
1var argv = require('optimist')2 .usage('Usage: $0 --name [name] --user [username] --testset [testset name] --machines [hostname1:threads:baseState,hostname2:threads:baseState] --emails [email1@host.com,email2@host.com] --pullLatest [true] --retryCount [1] --variables [name=value,name2=value2] --tags [tag1,tag2] --project [projectname] --ignoreScreenshots [true,false]')3 .demand(['name','testset','project','user'])4 .argv;5var common = require('../common');6var app = require('../common');7var xml = require('xml-writer');8var fs = require('fs');9var db;10var ObjectID = require('mongodb').ObjectID;11var http = require("http");12common.initLogger("cli");13common.parseConfig(function(){14 var execution = {};15 execution.project = argv.project;16 execution.user = argv.user;17 execution.name = argv.name;18 execution.status = "Ready To Run";19 execution.locked = true;20 execution.ignoreStatus = false;21 execution.ignoreAfterState = false;22 execution.lastRunDate = null;23 execution.testsetname = argv.testset;24 execution.pullLatest = argv.pullLatest;25 if(argv.tags){26 execution.tag = argv.tags.split(",");27 }28 if(argv.emails){29 execution.emails = argv.emails.split(",");30 }31 if(argv.retryCount){32 execution.retryCount = argv.retryCount;33 }34 else{35 execution.retryCount = "0";36 }37 if(argv.ignoreScreenshots){38 if(argv.ignoreScreenshots === "true"){39 execution.ignoreScreenshots = true;40 }41 else{42 execution.ignoreScreenshots = false;43 }44 }45 else{46 execution.ignoreScreenshots = false;47 }48 execution._id = new ObjectID().toString();49 common.initDB(common.Config.DBPort,function(){50 db = app.getDB();51 PullLatest(execution,function(){52 formatMachines(function(machines){53 execution.machines = machines;54 execution.templates = [];55 if(argv.cloudTemplate){56 execution.templates.push({name:argv.cloudTemplate.split(":")[0],result:"",description:"",instances:parseInt(argv.cloudTemplate.split(":")[1]),threads:parseInt(argv.cloudTemplate.split(":")[2])})57 }58 formatTestSet(argv.testset,argv.project,function(testsetID){59 execution.testset = testsetID.toString();60 saveExecutionTestCases(testsetID,execution._id,execution.retryCount,function(testcases){61 if(argv.variables){62 formatVariables(argv.variables.split(","),argv.project,function(variables){63 execution.variables = variables;64 SaveAndRunExecution(execution,testcases,function(){65 })66 })67 }68 else{69 execution.variables = [];70 SaveAndRunExecution(execution,testcases,function(){71 })72 }73 });74 });75 });76 });77 });78});79function saveExecutionTestCases(testsetID,executionID,retryCount,callback){80 var testcases = [];81 db.collection('testsets', function(err, testSetCollection) {82 db.collection('executiontestcases', function(err, ExeTCCollection) {83 //console.log(testsetID);84 testSetCollection.findOne({_id:db.bson_serializer.ObjectID(testsetID.toString())}, {testcases:1}, function(err, dbtestcases) {85 dbtestcases.testcases.forEach(function(testcase,index){86 db.collection('testcases', function(err, tcCollection) {87 tcCollection.findOne({_id:db.bson_serializer.ObjectID(testcase._id.toString())},{},function(err,dbtestcase){88 if(!dbtestcase){89 if(index+1 == dbtestcases.testcases.length){90 callback(testcases);91 }92 return93 }94 if(dbtestcase.tcData && dbtestcase.tcData.length > 0){95 var ddTCCount = 0;96 dbtestcase.tcData.forEach(function(row,rowIndex){97 var insertTC = {executionID:executionID,retryCount:retryCount,name:dbtestcase.name,tag:testcase.tag,status:"Not Run",testcaseID:testcase._id.toString(),_id: new ObjectID().toString()};98 insertTC.rowIndex = rowIndex+1;99 insertTC.name = insertTC.name +"_"+(rowIndex+1);100 insertTC.tcData = row;101 testcases.push(insertTC);102 ExeTCCollection.insert(insertTC, {safe:true},function(err,returnData){103 ddTCCount++;104 if(ddTCCount == dbtestcase.tcData.length && index+1 == dbtestcases.testcases.length){105 callback(testcases);106 }107 });108 })109 }110 else{111 var insertTC = {executionID:executionID,retryCount:retryCount,name:dbtestcase.name,tag:testcase.tag,status:"Not Run",testcaseID:testcase._id.toString(),_id: new ObjectID().toString()};112 testcases.push(insertTC);113 ExeTCCollection.insert(insertTC, {safe:true},function(err,returnData){114 if(index+1 == dbtestcases.testcases.length){115 callback(testcases);116 }117 });118 }119 });120 });121 });122 });123 });124 });125}126function PullLatest(execution,callback){127 if(execution.pullLatest !== "true"){128 callback();129 return130 }131 var options = {132 hostname: "localhost",133 port: common.Config.AppServerPort,134 path: '/scripts/pull',135 method: 'POST',136 agent:false,137 headers: {138 'Content-Type': 'application/json',139 'Cookie': 'username='+execution.user+";project="+execution.project140 }141 };142 var req = http.request(options, function(res) {143 res.setEncoding('utf8');144 res.on('data', function (chunk) {145 console.log('BODY: ' + chunk);146 callback();147 });148 });149 req.on('error', function(e) {150 console.log('problem with request: ' + e.message);151 });152 // write data to request body153 req.write(JSON.stringify({}));154 req.end();155}156function StartExecution(execution,testcases,callback){157 var options = {158 hostname: "localhost",159 port: common.Config.AppServerPort,160 path: '/executionengine/startexecution',161 method: 'POST',162 agent:false,163 headers: {164 'Content-Type': 'application/json',165 'Cookie': 'username='+execution.user+";project="+execution.project166 }167 };168 var req = http.request(options, function(res) {169 res.setEncoding('utf8');170 res.on('data', function (chunk) {171 console.log('BODY: ' + chunk);172 var msg = JSON.parse(chunk);173 if(msg.error){174 console.log("Unable to continue with execution: "+msg.error);175 process.exit(1);176 }177 callback();178 });179 });180 req.on('error', function(e) {181 console.log('problem with request: ' + e.message);182 });183 // write data to request body184 req.write(JSON.stringify({emails:execution.emails,ignoreAfterState:false,ignoreStatus:execution.ignoreStatus,ignoreScreenshots:execution.ignoreScreenshots,testcases:testcases,variables:execution.variables,executionID:execution._id,machines:execution.machines,templates:execution.templates}));185 req.end();186}187function SaveExecution(execution,callback){188 var options = {189 hostname: "localhost",190 port: common.Config.AppServerPort,191 path: '/executions/'+execution._id,192 method: 'POST',193 agent:false,194 headers: {195 'Content-Type': 'application/json',196 'Cookie': 'username='+execution.user+";project="+execution.project197 }198 };199 var req = http.request(options, function(res) {200 res.setEncoding('utf8');201 res.on('data', function (chunk) {202 console.log('BODY: ' + chunk);203 callback();204 });205 });206 req.on('error', function(e) {207 console.log('problem with request: ' + e.message);208 });209 // write data to request body210 req.write(JSON.stringify(execution));211 req.end();212}213function MonitorExecution(execution,callback){214 var getStatus = function(callback){215 db.collection('executions', function(err, collection) {216 collection.findOne({_id:execution._id}, {status:1}, function(err, dbexecution) {217 callback(dbexecution.status);218 });219 });220 };221 var verifyStatus = function(status){222 if(status == "Ready To Run"){223 setTimeout(function(){callback()},10000);224 }else{225 setTimeout(function(){getStatus(verifyStatus)},10000)226 }227 };228 setTimeout(function(){getStatus(verifyStatus)},20000)229}230function GenerateReport_old(cliexecution,callback){231 var xw = new xml();232 xw.startDocument();233 xw.startElement('testng-results');234 db.collection('executions', function(err, collection) {235 collection.findOne({_id:cliexecution._id}, {}, function(err, execution) {236 xw.writeAttribute('skipped', execution.notRun.toString());237 xw.writeAttribute('failed', execution.failed.toString());238 xw.writeAttribute('passed', execution.passed.toString());239 xw.writeAttribute('total', execution.total.toString());240 xw.startElement('suite');241 xw.writeAttribute('name',execution.name);242 xw.writeAttribute('duration-ms',execution.runtime.toString());243 xw.writeAttribute('started-at',execution.lastRunDate.toString());244 xw.writeAttribute('finished-at','0');245 xw.startElement('groups');246 xw.endElement();247 xw.startElement('test');248 xw.writeAttribute('name',execution.name);249 xw.writeAttribute('duration-ms',execution.runtime.toString());250 xw.writeAttribute('started-at',execution.lastRunDate.toString());251 xw.writeAttribute('finished-at','0');252 xw.startElement('class');253 xw.writeAttribute('name',execution.name);254 db.collection('executiontestcases', function(err, collection) {255 collection.find({executionID:execution._id}, {}, function(err, cursor) {256 cursor.each(function(err, testcase) {257 if(testcase == null) {258 xw.endElement();259 xw.endElement();260 xw.endElement();261 xw.endElement();262 xw.endDocument();263 //console.log(xw.toString());264 fs.writeFile("result.xml",xw.toString(),function(){265 callback(0);266 });267 return;268 }269 //console.log(testcase);270 xw.startElement('test-method');271 xw.writeAttribute('name',testcase.name);272 xw.writeAttribute('signature',testcase.name);273 xw.writeAttribute('duration',testcase.runtime.toString());274 xw.writeAttribute('started-at',testcase.startdate.toString());275 xw.writeAttribute('finished-at',testcase.enddate.toString());276 if (testcase.result == "Passed"){277 xw.writeAttribute('status',"PASS");278 xw.endElement();279 }280 else if (testcase.result == "Failed"){281 xw.writeAttribute('status',"FAIL");282 xw.startElement('exception');283 xw.startElement('message');284 xw.writeCData(testcase.error);285 xw.endElement();286 xw.startElement('full-stacktrace');287 xw.writeCData(testcase.trace);288 xw.endElement();289 xw.endElement();290 xw.endElement();291 }292 else{293 xw.writeAttribute('status',"SKIPPED");294 xw.endElement();295 }296 });297 })298 });299 });300 });301}302function GenerateReport(cliexecution,callback){303 var xw = new xml();304 xw.startDocument();305 xw.startElement('testsuite');306 db.collection('executions', function(err, collection) {307 collection.findOne({_id:cliexecution._id}, {}, function(err, execution) {308 console.log(execution);309 xw.writeAttribute('errors', "0");310 xw.writeAttribute('failures', execution.failed.toString());311 xw.writeAttribute('tests', execution.total.toString());312 xw.writeAttribute('name',execution.name);313 xw.writeAttribute('time',execution.runtime.toString());314 xw.writeAttribute('timestamp',execution.lastRunDate.toString());315 xw.startElement('properties');316 xw.endElement();317 db.collection('executiontestcases', function(err, collection) {318 var failed = false;319 collection.find({executionID:execution._id}, {}, function(err, cursor) {320 cursor.each(function(err, testcase) {321 if(testcase == null) {322 xw.endElement();323 xw.endDocument();324 //console.log(xw.toString());325 fs.writeFile("result.xml",xw.toString(),function(){326 if(failed == false){327 callback(0);328 }329 else{330 callback(1);331 }332 });333 return;334 }335 //console.log(testcase);336 xw.startElement('testcase');337 xw.writeAttribute('classname',testcase.name);338 xw.writeAttribute('name',testcase.name);339 xw.writeAttribute('time',(testcase.runtime / 1000).toString());340 if (testcase.result == "Passed"){341 xw.endElement();342 }343 else if (testcase.result == "Failed"){344 failed = true;345 xw.startElement('failure');346 xw.writeAttribute('type',"Test Case Error");347 xw.writeAttribute('message',testcase.error);348 if(argv.resultURL){349 //xw.writeAttribute('name',"<a href='"+argv.resultURL+"/index.html?result="+testcase.resultID+"&project="+argv.project+"'>"+testcase.name+"</a>");350 xw.writeCData(argv.resultURL+"/index.html?result="+testcase.resultID+"&project="+argv.project+" "+testcase.trace);351 }352 else{353 xw.writeCData(testcase.trace);354 }355 xw.endElement();356 xw.endElement();357 }358 else{359 xw.writeAttribute('executed',"false");360 xw.endElement();361 }362 });363 })364 });365 });366 });367}368function SaveAndRunExecution(execution,testcases,callback){369 SaveExecution(execution,function(){370 StartExecution(execution,testcases,function(){371 MonitorExecution(execution,function(){372 GenerateReport(execution,function(exitCode){373 process.exit(exitCode);374 })375 });376 })377 })378}379function formatTestSet(testset,project,callback){380 db.collection('testsets', function(err, collection) {381 collection.findOne({name:testset,project:project}, {_id:1}, function(err, dbtestset) {382 callback(dbtestset._id);383 });384 });385}386function formatVariables(clivariables,project,callback){387 var variables = [];388 var count = 0;389 db.collection('variables', function(err, collection) {390 clivariables.forEach(function(variable){391 collection.findOne({name:variable.split("=")[0].trim(),project:project}, {}, function(err, dbvariable) {392 dbvariable.value = variable.split("=")[1];393 variables.push(dbvariable);394 count++;395 if(count == clivariables.length){396 callback(variables);397 }398 });399 });400 });401}402function formatMachines(callback){403 if(!argv.machines) callback([]);404 var machines = argv.machines.split(",");405 var result = [];406 var count = 0;407 db.collection('machines', function(err, collection) {408 machines.forEach(function(machine){409 collection.findOne({host:machine.split(":")[0]}, {}, function(err, dbmachine) {410 if(!dbmachine){411 console.log("Machine: " +machine.split(":")[0]+" not found.");412 process.exit(1)413 }414 dbmachine.threads = machine.split(":")[1];415 count++;416 if(machine.split(":").length == 3){417 db.collection('actions', function(err, actionCollection) {418 actionCollection.findOne({name:machine.split(":")[2]}, {}, function(err, action) {419 if(action){420 dbmachine.baseState = action._id;421 result.push(dbmachine);422 if(count == machines.length){423 callback(result);424 }425 }426 else{427 console.log("Suite Base state: " +machine.split(":")[2]+" not found.");428 process.exit(1)429 }430 });431 });432 }433 else{434 result.push(dbmachine);435 if(count == machines.length){436 callback(result);437 }438 }439 });440 });441 });...
queries.js
Source:queries.js
1const getPP = "SELECT * FROM privacy_policy limit 1";2const deletePrivacyPolicy = "DELETE from privacy_policy";3const insertPrivacyPolicy = "INSERT INTO privacy_policy (prpo_text) VALUES($1)";4const getTC = "SELECT * FROM terms_conditions limit 1";5const deleteTC = "DELETE FROM terms_conditions";6const insertTC = "INSERT INTO terms_conditions (teco_text) VALUES($1)";7module.exports = {8 getPP,9 deletePrivacyPolicy,10 insertPrivacyPolicy,11 getTC,12 deleteTC,13 insertTC,...
Using AI Code Generation
1var mongo = require('redwood-mongo');2mongo.insertTC('test', {a: 'b', c: 'd'}, function(err, doc) {3 if (err) {4 console.log('Error inserting document');5 return;6 }7 console.log('Document inserted successfully');8});9### insertTC(collection, doc, [options], callback)10### updateTC(collection, query, update, [options], callback)11### removeTC(collection, query, [options], callback)12### findTC(collection, query, [options], callback)
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!