Advertisement
Guest User

Untitled

a guest
Nov 4th, 2016
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.99 KB | None | 0 0
  1. var fs = require('fs');
  2. var request = require('request');
  3. var inputJSONdata = fs.readFileSync('C:/Users/SESA435429/Documents/Prathamesh_kulkarni/excelsheetinfo.xlsx');
  4. console.log(inputJSONdata);
  5.  
  6. var inputAttribute = JSON.parse(inputJSONdata);
  7. var userGivenIP = inputAttribute.IPv6Address
  8. var username=inputAttribute.Username;
  9. var password=inputAttribute.password;
  10. var MacAddress = inputAttribute.MACAddress;
  11. var path = inputAttribute.URIvalues;
  12. var GET_Flag;
  13. var PUT_Flag;
  14. var Compare_Flag;
  15. var Invalid_Data_Flag;
  16. if('proceed' == inputAttribute.Home)
  17. {
  18. var jsonContent; //Json object after parsing GET request body.
  19. var getIntoLoop; // Enter into the loop after getting data through web service.
  20. var util = require('util');
  21. var dateFormat = require('dateformat');
  22. var day=dateFormat("yyyy-mm-dd_h.MM.ss");
  23. var log_file = fs.createWriteStream('C:/Users/SESA391442/URI_Testing_Tool/LightWebApp/js/tests/express/Uri_test/T-Case-1_LOG[ '+day+' ].log', {flags : 'w'});
  24.  
  25. log_stdout = process.stdout;
  26. console.print = function(d) { //
  27. log_file.write(util.format(d) );
  28. log_stdout.write(util.format(d));
  29. };
  30. if(typeof require !== 'undefined') XLSX = require('xlsx');
  31. var workbook = XLSX.readFile(path); // reading from Excel file.
  32. var URI = workbook.SheetNames[0];
  33. var address_of_cell_1 = 'A'+'1';
  34. var address_of_cell_2 = 'A'+'2';
  35. var address_of_cell_3 = 'A'+'3';
  36. /* Get worksheet */
  37. var worksheet = workbook.Sheets[URI];
  38.  
  39. /* Find desired cell */
  40. var desired_cell_1 = worksheet[address_of_cell_1];
  41. var desired_cell_2 = worksheet[address_of_cell_2];
  42. var desired_cell_3 = worksheet[address_of_cell_3];
  43. /* Get the value */
  44.  
  45. var desired_value_1 = desired_cell_1.v; // value-1 URI
  46.  
  47. var desired_value_2 = desired_cell_2.v; // value-2 KEYS
  48.  
  49. var desired_value_3 = desired_cell_3.v; // value-3 VALUES
  50.  
  51. var uri_attributes = new Array();
  52.  
  53. // this will return an array with strings "1", "2", etc.
  54. uri_attributes = desired_value_2.split(";");
  55. uri_attributes_values = desired_value_3.split(";");
  56.  
  57.  
  58. var i = 0;
  59. var stringJSONTemp = "";
  60.  
  61. for (; i < uri_attributes.length; i++ )
  62. {
  63.  
  64. stringJSONTemp += uri_attributes[i]+': '+uri_attributes_values[i];
  65. if(i == uri_attributes.length-1)
  66. {
  67.  
  68. }
  69. else{
  70. stringJSONTemp += ', ';
  71. }
  72. }
  73. console.print('REQUEST...n');
  74. console.print('PUT : /rs/IPConfiguration;NetIfName=eth0;ProtocolType=IPv4n');
  75. console.print("PUT Parameters: "+stringJSONTemp);
  76. console.print('RESPONSE...n');
  77. request({
  78. method: 'PUT',
  79. uri: 'http://'+username+':'+ password + '@'+userGivenIP+desired_value_1,
  80. headers:
  81. {
  82. 'content-type': 'application/json'
  83. },
  84. body: JSON.stringify(JSON.parse("{"+stringJSONTemp+"}")),
  85. },
  86. function (error, response, body)
  87. {
  88. if(error)
  89. {
  90. console.print(error);
  91. }
  92. else
  93. {
  94. console.print("Response Code of PUT request: "+response.statusCode);
  95. console.print("n*******************************************************************************************************n");
  96. PUT_Flag = true;
  97. fs.readFile('C:/Users/SESA391442/URI_Testing_tool/LightWebApp/js/tests/express/sniffing/glanceTestCaseFile.js', 'utf8', function (err,data)
  98. {
  99. if (err)
  100. {
  101. return console.log(err);
  102. }
  103. var result = data.replace(/"PUT_Result":""/g, '"PUT_Result":"pass"'); // changing the ip address of DUT dualserver.ini file
  104. fs.writeFile('C:/Users/SESA391442/URI_Testing_tool/LightWebApp/js/tests/express/sniffing/glanceTestCaseFile.js', result, 'utf8', function (err)
  105. {
  106. if (err) return console.log(err);
  107. });
  108.  
  109. });
  110. }
  111.  
  112.  
  113. }
  114. )
  115.  
  116. setTimeout(function()
  117. {
  118. console.print('nREQUEST...n');
  119. console.print('GET : /rs/IPConfiguration;NetIfName=eth0;ProtocolType=IPv4n');
  120. console.print('nRESPONSE...n');
  121. var options = {
  122. url: 'http://'+username+':'+ password + '@'+userGivenIP+desired_value_1,
  123. method: 'GET',
  124. }
  125. request(options, function (error, response, body)
  126. {
  127. if(error)
  128. {
  129. console.print("error:"+error);
  130. }
  131.  
  132. if(response.statusCode !== 200)
  133. {
  134. console.print('Invalid Status Code Returned:', response.statusCode);
  135. }
  136.  
  137. if (!error && response.statusCode == 200)
  138. {
  139. // Define to JSON type
  140. var jsonContent = JSON.parse(body);
  141. getIntoLoop = true ;
  142. console.print("n Response statusCode :"+response.statusCode);
  143. console.print("nBody :"+body);
  144. console.print("n*******************************************************************************************************n");
  145. GET_Flag = true
  146. fs.readFile('C:/Users/SESA391442/URI_Testing_tool/LightWebApp/js/tests/express/sniffing/glanceTestCaseFile.js', 'utf8', function (err,data)
  147. {
  148. if (err)
  149. {
  150. return console.log(err);
  151. }
  152. var result = data.replace(/"GET_Result":""/g, '"GET_Result":"pass"'); // changing the ip address of DUT dualserver.ini file
  153. fs.writeFile('C:/Users/SESA391442/URI_Testing_tool/LightWebApp/js/tests/express/sniffing/glanceTestCaseFile.js', result, 'utf8', function (err)
  154. {
  155. if (err) return console.log(err);
  156. });
  157.  
  158. });
  159. }
  160. k = 0;
  161. var attrib;
  162. var val;
  163. for (; k < uri_attributes.length; k++ )
  164. {
  165. key = uri_attributes[k].replace(/['"]+/g, '');
  166. val = uri_attributes_values[k].replace(/['"]+/g, '');
  167. //console.log("jsonContent.uri_attributes[k]:", eval("jsonContent." + key));
  168. if (eval("jsonContent." + key) == val)
  169. {
  170. if(k == uri_attributes.length-1)
  171. {
  172. console.print('ncomparing values of PUT and GET request: n' );
  173. console.print('attributes matched to the earlier PUT requestn' );
  174. console.print('comparing is successfuln' );
  175. console.print("n*******************************************************************************************************n");
  176. console.print("n");
  177. Compare_Flag = true
  178. setTimeout(function()
  179. {
  180. fs.readFile('C:/Users/SESA391442/URI_Testing_tool/LightWebApp/js/tests/express/sniffing/glanceTestCaseFile.js', 'utf8', function (err,data)
  181. {
  182. if (err)
  183. {
  184. return console.log(err);
  185. }
  186. var result = data.replace(/"CMPR_Result":""/g, '"CMPR_Result":"pass"'); // changing the ip address of DUT dualserver.ini file
  187. fs.writeFile('C:/Users/SESA391442/URI_Testing_tool/LightWebApp/js/tests/express/sniffing/glanceTestCaseFile.js', result, 'utf8', function (err)
  188. {
  189. if (err) return console.log(err);
  190. });
  191.  
  192. });
  193. }, 3000)
  194. }
  195.  
  196. }
  197. else
  198. {
  199. console.print('n attributes did not match to the earlier PUT request n' );
  200. }
  201. }
  202.  
  203.  
  204. });
  205.  
  206. }, 30000)
  207. setTimeout(function()
  208. {
  209. console.print('sending INVALID data in JSON....n');
  210. var j = 0;
  211. for (; j < uri_attributes.length-3; j++ )
  212. {
  213.  
  214. stringJSONTemp += uri_attributes[j]+': '+uri_attributes_values[j];
  215. if(j == uri_attributes.length-4)
  216. {
  217.  
  218. }
  219. else{
  220. stringJSONTemp += ', ';
  221. }
  222. }
  223. request({
  224. method: 'PUT',
  225. uri: 'http://'+username+':'+ password + '@'+userGivenIP+desired_value_1,
  226. headers:
  227. {
  228. 'content-type': 'application/json'
  229. },
  230. //body: JSON.stringify(JSON.parse("{"+stringJSONTemp+"}")),
  231. body: JSON.stringify({
  232.   "NetIfName":  "eth0",
  233.   "AddressOrigin":  "Static",
  234.   "IPv4Address":  "192.168.1.100",
  235.   "SubnetMask":  "255.255.255.0",
  236.   "GatewayAddress":  "192.168.1.254"
  237. })
  238. },
  239. function (error, response, body)
  240. {
  241. if(error)
  242. {
  243. console.log(error);
  244. }
  245. if(response.statusCode !== 200)
  246. {
  247. console.print('nInvalid Status Code Returned while sending invalid values through JSON: '+response.statusCode);
  248. console.print('nresponse Status Code: '+response.statusCode);
  249. Invalid_Data_Flag = true
  250. fs.readFile('C:/Users/SESA391442/URI_Testing_tool/LightWebApp/js/tests/express/sniffing/glanceTestCaseFile.js', 'utf8', function (err,data)
  251. {
  252. if (err)
  253. {
  254. return console.log(err);
  255. }
  256. var result = data.replace(/"INV_Result":""/g, '"INV_Result":"pass"'); // changing the ip address of DUT dualserver.ini file
  257. fs.writeFile('C:/Users/SESA391442/URI_Testing_tool/LightWebApp/js/tests/express/sniffing/glanceTestCaseFile.js', result, 'utf8', function (err)
  258. {
  259. if (err) return console.log(err);
  260. });
  261.  
  262. });
  263. }
  264.  
  265. console.print("nbody: "+body);
  266. console.print("nn*******************************************************************************************************n");
  267.  
  268.  
  269. }
  270. )
  271. }, 40000)
  272. }
  273. setTimeout(function()
  274. {
  275. if((true == GET_Flag) && (true == PUT_Flag) && (true == Invalid_Data_Flag) &&(true == Compare_Flag))
  276. {
  277. console.print('nTESTCASE is PASSED');
  278. console.print('n');
  279. var fs = require('fs')
  280. fs.readFile('C:/Users/SESA391442/URI_Testing_tool/LightWebApp/js/tests/express/sniffing/glanceTestCaseFile.js', 'utf8', function (err,data)
  281. {
  282. if (err)
  283. {
  284. return console.log(err);
  285. }
  286. var result = data.replace(/"TOTAL_Result":""/g, '"TOTAL_Result":"pass"'); // changing the ip address of DUT dualserver.ini file
  287. fs.writeFile('C:/Users/SESA391442/URI_Testing_tool/LightWebApp/js/tests/express/sniffing/glanceTestCaseFile.js', result, 'utf8', function (err)
  288. {
  289. if (err) return console.log(err);
  290. });
  291.  
  292. });
  293. }
  294. }, 50000)
  295.  
  296. This code has to done dynamically. I am not getting how to resolve this. Any help please.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement