Best JavaScript code snippet using redwood
importtcs.js
Source:importtcs.js
1var fs = require('fs');2var path = require('path');3var elasticsearch = require('./elasticsearch');4var common = require('../common');5var rootDir = path.resolve(__dirname,"../public/automationscripts/")+"/";6var appDir = path.resolve(__dirname,"../")+"/";7var spawn = require('child_process').spawn;8var walk = require('walk');9var realtime = require("./realtime");10var divideChar = "\r\n";11if(process.platform == "win32"){12 divideChar = "\r\n";13}14else{15 divideChar = "\n";16}17exports.importAllTCs = function(req,res){18 var walker = walk.walk(rootDir+req.cookies.project+"/"+req.cookies.username+"/src");19 var total = 0;20 var processed = 0;21 var totalTCs = 0;22 walker.on("file", function (root, fileStats, next) {23 total++;24 next();25 importTCsFromFile(root+"/"+fileStats.name,req.cookies.project,function(totalImported){26 processed++;27 totalTCs = totalTCs + totalImported;28 if(total == processed){29 realtime.emitMessage("TCImportDone"+req.cookies.username,totalTCs);30 }31 });32 });33 walker.on("end",function(){34 if(total == 0){35 realtime.emitMessage("TCImportDone"+req.cookies.username,totalTCs);36 }37 });38 res.contentType('json');39 res.json({success: true});40};41exports.getAllPythonTests = function(req,res){42 res.contentType('json');43 res.json({success: true});44 var pythonPath = "";45 if(process.platform == "win32"){46 pythonPath = rootDir+req.cookies.project+"/"+req.cookies.username+"/PythonWorkDir/python"47 }48 else{49 pythonPath = rootDir+req.cookies.project+"/"+req.cookies.username+"/PythonWorkDir/bin/python"50 }51 scanPythonFiles(pythonPath,rootDir+req.cookies.project+"/"+req.cookies.username+"/src",function(tests){52 if(tests == ""){53 realtime.emitMessage("GetAllTestCases"+req.cookies.username,{testcases:[],import:req.body.import});54 return;55 }56 var TCs = tests.split("\n");57 var allTCs = [];58 var tcCount = 0;59 TCs.forEach(function(testCaseData,index) {60 var testCase = testCaseData.split("|")[0];61 var tags = testCaseData.split("|");62 tags.shift();63 if(testCase != ""){64 var db = require('../common').getDB();65 db.collection('testcases', function(err, collection) {66 collection.findOne({type: { $in: [ 'pytest'] }, script: testCase, project: req.cookies.project}, function (err, data) {67 if (data === null) {68 allTCs.push({name:testCase,path:testCase,type:"pytest",scriptLang:"Python",tags:tags});69 }70 tcCount++;71 if (tcCount == TCs.length) {72 realtime.emitMessage("GetAllTestCases"+req.cookies.username,{testcases:allTCs,import:req.body.import});73 }74 });75 });76 }77 else{78 tcCount++;79 if (tcCount == TCs.length) {80 realtime.emitMessage("GetAllTestCases"+req.cookies.username,{testcases:allTCs,import:req.body.import});81 }82 }83 });84 //realtime.emitMessage("GetAllTestCases"+req.cookies.username,{testcases:allTCs,import:req.body.import});85 })86};87exports.getAllUnitTests = function(req,res){88 res.contentType('json');89 res.json({success: true});90 var walker = null;91 var total = 0;92 var processed = 0;93 var allTCs = [];94 var processFile = function(path){95 scanFile(path,function(cache){96 if(cache.indexOf("JUNIT") != -1 || cache.indexOf("TESTNG") != -1 ) {97 var TCs = cache.split(divideChar);98 var tcCount = 0;99 TCs.forEach(function(testCase,index) {100 if (index != 0 && testCase != "") {101 if(req.body.import === true){102 var db = require('../common').getDB();103 db.collection('testcases', function(err, collection) {104 collection.findOne({type: { $in: [ 'junit', 'testng' ] }, script: testCase.split("|")[0], project: req.cookies.project}, function (err, data) {105 if (data === null) {106 allTCs.push({name:testCase.split("|")[0],path:path,type:TCs[0]});107 }108 tcCount++;109 if (tcCount == TCs.length) {110 processed++;111 if (total == processed) {112 realtime.emitMessage("GetAllTestCases"+req.cookies.username,{testcases:allTCs,import:req.body.import});113 }114 }115 });116 });117 }118 else{119 allTCs.push({name:testCase.split("|")[0],path:path,type:TCs[0]});120 tcCount++;121 if (tcCount == TCs.length) {122 processed++;123 if (total == processed) {124 realtime.emitMessage("GetAllTestCases"+req.cookies.username,{testcases:allTCs,import:req.body.import});125 }126 }127 }128 }129 else{130 tcCount++;131 if (tcCount == TCs.length) {132 processed++;133 if (total == processed) {134 realtime.emitMessage("GetAllTestCases"+req.cookies.username,{testcases:allTCs,import:req.body.import});135 }136 }137 }138 });139 if (total == processed && TCs.length == 0) {140 realtime.emitMessage("GetAllTestCases"+req.cookies.username,{testcases:allTCs,import:req.body.import});141 }142 }143 else{144 processed++;145 if (total == processed) {146 realtime.emitMessage("GetAllTestCases"+req.cookies.username,{testcases:allTCs,import:req.body.import});147 }148 }149 });150 };151 if(req.body.path){152 total = 1;153 processFile(req.body.path)154 }155 else{156 walker = walk.walk(rootDir+req.cookies.project+"/"+req.cookies.username+"/src");157 walker.on("file", function (root, fileStats, next) {158 total++;159 next();160 processFile(root+"/"+fileStats.name);161 //console.log(root+"/"+fileStats.name);162 });163 walker.on("end",function(){164 if(total == 0){165 realtime.emitMessage("GetAllTestCases"+req.cookies.username,{testcases:allTCs,import:req.body.import});166 }167 });168 }169};170function importTCsFromFile(path,project,callback){171 scanFile(path,function(cache){172 if(cache.indexOf("JUNIT") != -1 || cache.indexOf("TESTNG") != -1 ){173 var TCs = cache.split(divideChar);174 var db = require('../common').getDB();175 var tcType = "";176 if (TCs[0] == "JUNIT"){177 tcType = "junit"178 }179 else{180 tcType = "testng"181 }182 var tcCount = 0;183 var totalImported = 0;184 TCs.forEach(function(testCase,index){185 if(index != 0 && testCase != ""){186 var tcDetails = testCase.split("|");187 db.collection('testcases', function(err, collection) {188 collection.findOne({type:{ $in: [ 'junit', 'testng' ] },script:tcDetails[0],project:project},function(err,data){189 if(data == null){190 insertTC(tcDetails[0],project,tcType,function(){191 totalImported++;192 tcCount++;193 if(tcCount == TCs.length){callback(totalImported)}194 })195 }196 else{197 tcCount++;198 if(tcCount == TCs.length){callback(totalImported)}199 }200 });201 });202 }203 else{204 tcCount++;205 if(tcCount == TCs.length){callback(totalImported)}206 }207 });208 }209 else{210 callback(0);211 }212 })213}214exports.importSelectedTCs = function(req,res){215 var count = 0;216 var tags = [];217 req.body.testcases.forEach(function(testcase){218 var packages = "";219 var tcTags = [];220 if(testcase.type == "pytest") {221 tcTags = testcase.tags;222 for(var i2=0;i2<testcase.tags.length;i2++){223 if(tags.indexOf(testcase.tags[i2]) == -1) tags.push(testcase.tags[i2]);224 }225 }226 else{227 packages = testcase.name.split(".");228 for(var i=0;i<packages.length;i++){229 if(i<packages.length-2){230 if(tags.indexOf(packages[i]) == -1) tags.push(packages[i]);231 tcTags.push(packages[i]);232 }233 }234 }235 insertTC(testcase.name,req.cookies.project,testcase.type.toLowerCase(),tcTags,function(){236 count++;237 if(count == req.body.testcases.length){238 insertTags(tags,req.cookies.project,function(){239 realtime.emitMessage("TCImportDone"+req.cookies.username,count);240 });241 }242 })243 });244 res.contentType('json');245 res.json({success: true});246};247function insertTC(fullName,project,tcType,tags,callback){248 var language = "Java/Groovy";249 if(tcType == "pytest"){250 language = "Python";251 }252 var db = require('../common').getDB();253 var newTC = {scriptLang:language,name:fullName,type:tcType,script:fullName,status:"Automated",tag:tags,project:project,actioncollection:[]};254 db.collection('testcases', function(err, collection) {255 collection.insert(newTC, {safe:true},function(err,tcReturnData){256 if(err == null){257 realtime.emitMessage("AddTestCases",tcReturnData[0]);258 elasticsearch.indexTestCase(tcReturnData[0],"PUT");259 }260 callback()261 });262 });263}264function insertTags(tags,project,callback){265 if(tags.length == 0) callback();266 var db = require('../common').getDB();267 var tagCount = 0;268 tags.forEach(function(tag,index){269 db.collection('testcaseTags', function(err, tagcollection) {270 tagcollection.findOne({project:project,value:tag},{},function(err,foundTag){271 if (foundTag == null){272 tagcollection.insert({project:project,value:tag}, {safe:true},function(err,returnData){273 realtime.emitMessage("AddTestCaseTags",returnData[0]);274 tagCount++;275 if (tagCount == tags.length){276 callback();277 }278 });279 }280 else{281 tagCount++;282 if (tagCount == tags.length){283 callback();284 }285 }286 });287 });288 });289}290function scanFile(path,callback){291 var proc = spawn(appDir+"vendor/Java/bin/java",["-cp",appDir+'utils/lib/*;'+appDir+'vendor/groovy/*;'+appDir+'utils/*',"MethodList",path,"tests"]);292 proc.stderr.on('data', function (data) {293 common.logger.error(data.toString());294 });295 var cache = "";296 proc.stdout.on('data', function(data) {297 cache = cache + data.toString();298 });299 proc.on('close', function(){300 callback(cache)301 });302}303function scanPythonFiles(pythonPath,path,callback){304 //var proc = spawn(pythonPath,[appDir+'utils/pytestparser.py'],{cwd:path,env:{PYTHONDONTWRITEBYTECODE:"true"}});305 var proc = spawn(pythonPath,[appDir+'utils/pytestparser.py'],{cwd:path+"/",env:{PYTHONPATH:path+"/",PYTHONDONTWRITEBYTECODE:"true"}});306 proc.stderr.on('data', function (data) {307 common.logger.error(data.toString());308 });309 var cache = "";310 proc.stdout.on('data', function(data) {311 cache = cache + data.toString();312 });313 proc.on('close', function(){314 callback(cache)315 });...
ExcelTestCaseFileReader.ts
Source:ExcelTestCaseFileReader.ts
1import ExcelReader from "../util/readers/ExcelReader";2import ITestCaseFileReader from "./ITestCaseFileReader";3import ITestCase from "./ITestCase";4import TestCase from "./TestCase";5import {Workbook, Worksheet} from "exceljs";6import IKeywordStore from "../reader/IKeywordStore";7import KeywordStore from "../reader/KeywordStore";8const path = require('path');9export default class ExcelTestCaseFileReader implements ITestCaseFileReader {10 public isSupported(filePath: any): boolean {11 let supported: boolean = false;12 let fileName:string = path.basename(filePath);13 if (fileName.endsWith(".xls") || fileName.endsWith(".xlsx"))14 supported = true;15 return supported;16 }17 public readFile(filePath: any, args: Array<string>): Promise<any> {18 let excelReader:ExcelReader = null;19 const me = this;20 try {21 excelReader = new ExcelReader();22 if(args && args.length>1) {23 return excelReader.openFile(filePath, args[0]).then(function () {24 return new Promise<any>((resolve) => {25 resolve(me.getAllTestCases(excelReader));26 });27 });28 } else {29 return excelReader.openFile(filePath).then(function () {30 return new Promise<any>((resolve) => {31 resolve(me.getAllTestCases(excelReader));32 });33 });34 }35 } catch (e) {36 console.log('unable to read file' + e);37 }38 }39 private getAllTestCases(excelReader:ExcelReader):Array<ITestCase> {40 let tests:Array<ITestCase> = [];41 let workbook:Workbook = excelReader.getOpenWorkbook();42 let worksheet:Worksheet = workbook.getWorksheet('TestCases')43 let noOfRows:number = worksheet.rowCount44 for(let row = 2; row <= noOfRows; row++){45 excelReader.setWorkSheet(worksheet);46 let testCaseId:string = excelReader.getData(row, 1);47 let testCaseName:string = excelReader.getData(row, 2);48 let enabledValue:string = excelReader.getData(row, 3);49 let enabled:boolean = enabledValue.toUpperCase() == "Y";50 let testCaseSheetName:string = excelReader.getData(row, 4);51 let testCase:ITestCase = new TestCase(testCaseName, testCaseId, enabled, testCaseSheetName);52 let testSteps:Array<IKeywordStore> = this.getTestSteps(excelReader, testCase);53 testCase.setTestSteps(testSteps);54 tests.push(testCase);55 }56 return tests;57 }58 private getTestSteps(excelReader, testCase) {59 let keyStores:Array<IKeywordStore> = Array<IKeywordStore>();60 let workbook:Workbook = excelReader.getOpenWorkbook();61 let worksheet:Worksheet = workbook.getWorksheet(testCase.testCaseSheetName)62 excelReader.setWorkSheet(worksheet);63 let noOfRows:number = worksheet.rowCount64 for (let rowNo = 2; rowNo <= noOfRows; rowNo++) {65 let key:string = excelReader.getData(rowNo, 1);66 let locator:string = excelReader.getData(rowNo, 2);67 let locatorType:string = excelReader.getData(rowNo, 3);68 let value:string = excelReader.getData(rowNo, 4);69 let keyStore:IKeywordStore = new KeywordStore();70 let params = [];71 for (var columnNo = 4; columnNo <= excelReader.getNoOfColumn(rowNo); columnNo++) {72 var param = excelReader.getData(rowNo, columnNo);73 params.push(param);74 }75 keyStore.setKeyword(key);76 keyStore.setParams(params);77 keyStore.setLocator(locator);78 keyStore.setLocatorType(locatorType);79 keyStores.push(keyStore);80 }81 return keyStores;82 }...
Using AI Code Generation
1var redwood = require('redwood');2var fs = require('fs');3redwood.GetAllTestCases(function (err, data) {4 if (err) {5 console.log(err);6 }7 else {8 console.log(data);9 fs.writeFile('test.json', JSON.stringify(data), function (err) {10 if (err) {11 return console.log(err);12 }13 console.log("The file was saved!");14 });15 }16});
Using AI Code Generation
1var redwood = require("redwood");2var test = new redwood.Test();3var testCases = test.GetAllTestCases();4for (var i = 0; i < testCases.length; i++) {5 console.log(testCases[i]);6}
Using AI Code Generation
1var redwood = require('redwood');2var redwoodService = new redwood.RedwoodService();3var testCases = redwoodService.GetAllTestCases();4console.log(testCases);5var redwood = require('redwood');6var redwoodService = new redwood.RedwoodService();7var testCases = redwoodService.GetTestCasesByTag('Smoke');8console.log(testCases);9var redwood = require('redwood');10var redwoodService = new redwood.RedwoodService();11var testCases = redwoodService.GetTestCasesByTag('Smoke', 'Regression');12console.log(testCases);13var redwood = require('redwood');14var redwoodService = new redwood.RedwoodService();15var testCases = redwoodService.GetTestCasesByTag(['Smoke', 'Regression']);16console.log(testCases);17var redwood = require('redwood');18var redwoodService = new redwood.RedwoodService();19var testCases = redwoodService.GetTestCasesByTag();20console.log(testCases);21var redwood = require('redwood');22var redwoodService = new redwood.RedwoodService();23var testCases = redwoodService.GetTestCasesByTag('Smoke', 'Regression', 'Sanity');24console.log(testCases);25var redwood = require('redwood');26var redwoodService = new redwood.RedwoodService();27var testCases = redwoodService.GetTestCasesByTag(['Smoke', 'Regression', 'Sanity']);28console.log(testCases);29var redwood = require('redwood');30var redwoodService = new redwood.RedwoodService();31var testCases = redwoodService.GetTestCasesByTag('Smoke', 'Regression', 'Sanity', 'Performance');32console.log(test
Using AI Code Generation
1var redwood = require('redwood');2var test = function () {3 var testCases = redwood.GetAllTestCases();4 console.log(testCases);5 return testCases;6};7module.exports = test;
Using AI Code Generation
1var redwood = require('redwood');2var testCases = redwood.GetAllTestCases();3console.log(testCases);4### redwood.GetAllTestCases()5### redwood.GetTestCase(testCaseNumber)6### redwood.GetTestCaseByName(testCaseName)7### redwood.AddTestCase(testCase)8### redwood.RemoveTestCase(testCaseNumber)9### redwood.RemoveTestCaseByName(testCaseName)10### redwood.RunTestCases()11### redwood.RunTestCasesByName(testCaseName)12### redwood.RunTestCasesByTag(testCaseTag)13### redwood.RunTestCasesByTags(testCaseTags)14### redwood.RunTestCasesByTagsAndName(testCaseTags, testCaseName)15### redwood.RunTestCase(testCaseNumber)16### redwood.RunTestCaseByName(testCaseName)
Using AI Code Generation
1var redwood = require('redwood');2var rw = new redwood();3rw.GetAllTestCases(function (testcases) {4 console.log(testcases);5});6var filter = {7};8rw.GetAllTestCases(filter, function (testcases) {9 console.log(testcases);10});11var filter = {12};13var sort = {14};15rw.GetAllTestCases(filter, sort, function (testcases) {16 console.log(testcases);17});18var filter = {19};20var sort = {21};22var page = {23};24rw.GetAllTestCases(filter, sort, page, function (testcases) {25 console.log(testcases);26});27var filter = {28};29var sort = {30};31var page = {32};33var fields = {34};35rw.GetAllTestCases(filter, sort, page, fields, function (testcases) {36 console.log(testcases);37});38var filter = {39};40var sort = {41};42var page = {43};44var fields = {45};46var callback = function (testcases) {47 console.log(testcases);48};
Using AI Code Generation
1var redwood = require('redwood');2var allTestCases = redwood.GetAllTestCases();3var testCases = allTestCases.filter(function (testCase) {4 return testCase.Name === 'test1';5});6var test = testCases[0];7var test1 = test.TestSteps;8var test2 = test1.filter(function (testStep) {9 return testStep.Name === 'test2';10});11var test3 = test2[0];12var test4 = test3.TestSteps;13var test5 = test4.filter(function (testStep) {14 return testStep.Name === 'test3';15});16var test6 = test5[0];17var test7 = test6.TestSteps;18var test8 = test7.filter(function (testStep) {19 return testStep.Name === 'test4';20});21var test9 = test8[0];22var test10 = test9.TestSteps;23var test11 = test10.filter(function (testStep) {24 return testStep.Name === 'test5';25});26var test12 = test11[0];27var test13 = test12.TestSteps;28var test14 = test13.filter(function (testStep) {29 return testStep.Name === 'test6';30});31var test15 = test14[0];32var test16 = test15.TestSteps;33var test17 = test16.filter(function (testStep) {34 return testStep.Name === 'test7';35});36var test18 = test17[0];37var test19 = test18.TestSteps;38var test20 = test19.filter(function (testStep) {39 return testStep.Name === 'test8';40});41var test21 = test20[0];42var test22 = test21.TestSteps;43var test23 = test22.filter(function (testStep) {44 return testStep.Name === 'test9';45});46var test24 = test23[0];47var test25 = test24.TestSteps;48var test26 = test25.filter(function (testStep) {49 return testStep.Name === 'test10';50});51var test27 = test26[0];52var test28 = test27.TestSteps;53var test29 = test28.filter(function (testStep) {54 return testStep.Name === 'test11';55});56var test30 = test29[0];57var test31 = test30.TestSteps;58var test32 = test31.filter(function (testStep) {
Using AI Code Generation
1var redwood = require('redwood');2var redwoodService = redwood.createService({3});4var redwoodServiceClient = redwoodService.createClient();5var test = redwoodServiceClient.createProxy();6test.GetAllTestCases(function (err, result) {7 console.log(result);8});9var redwood = require('redwood');10var redwoodService = redwood.createService({11});12var redwoodServiceClient = redwoodService.createClient();13var test = redwoodServiceClient.createProxy();14test.GetAllTestCases(function (err, result) {15 console.log(result);16});17var redwood = require('redwood');18var redwoodService = redwood.createService({19});20var redwoodServiceClient = redwoodService.createClient();21var test = redwoodServiceClient.createProxy();22test.GetAllTestCases(function (err, result) {23 console.log(result);24});25var redwood = require('redwood');26var redwoodService = redwood.createService({27});28var redwoodServiceClient = redwoodService.createClient();29var test = redwoodServiceClient.createProxy();30test.GetAllTestCases(function (err, result) {31 console.log(result);32});33var redwood = require('redwood');34var redwoodService = redwood.createService({35});36var redwoodServiceClient = redwoodService.createClient();37var test = redwoodServiceClient.createProxy();38test.GetAllTestCases(function (err, result) {39 console.log(result);40});41var redwood = require('redwood');42var redwoodService = redwood.createService({
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!!