Best JavaScript code snippet using appium-xcuitest-driver
device.js
Source:device.js
1$.ajaxSetup({ cache: false }); 2function makeId()3{4 var text = "";5 var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";6 for( var i=0; i < 30; i++ )7 text += possible.charAt(Math.floor(Math.random() * possible.length));8 return text;9}10function hideAll() {11 $('#createDevice').hide();12 $('#tree').hide();13 $('#groupTree').hide();14 $('#deleteDevice').hide();15 $('#createGroup').hide();16 $('#editGroup').hide();17 $('#editGroup button').hide();18 $('#deleteGroupSubmit').hide();19 $('#search').hide();20 $('#searchList').hide()21 $('#calendarWrapper').hide();22 $('#sidebar-nav li').css('font-weight','normal');23}24function getTree() {25 // Some logic to retrieve, or generate tree structure26 var data = {27 "level": 028 }29 var rootNode = [];30 $.ajax({31 url:'/v1/device/level/root',32 type:"POST",33 data: JSON.stringify(data),34 contentType:"application/json; charset=utf-8",35 dataType:"json",36 async:false,37 success: function(data){38 if (data.code == 0) {39 rootNode = data.result.list;40 for (var i = 0; i < rootNode.length; ++i) {41 rootNode[i].text = rootNode[i].name;42 }43 } else {44 alert("è·å设å¤å¤±è´¥");45 console.log(data);46 }47 }48 })// end of ajax49 50 var nodes = rootNode.slice();51 while (nodes.length) {52 var node = nodes.pop();53 var data = {54 "id": node.id,55 "level": node.level56 }57 $.ajax({58 url:'/v1/device/level/childs',59 type:"POST",60 data: JSON.stringify(data),61 contentType:"application/json; charset=utf-8",62 dataType:"json",63 async:false,64 success: function(data){65 if (data.code == 0) {66 if (data.result.size > 0) {67 node.nodes = data.result.list;68 for (var i = 0; i < node.nodes.length; ++i) {69 node.nodes[i].text = node.nodes[i].name;70 node.type = 1;71 }72 73 nodes = nodes.concat(node.nodes);74 } else {75 var newData = {76 "id": node.id77 } 78 $.ajax({79 url:'/v1/device/list',80 type:"GET",81 data: newData,82 contentType:"application/json; charset=utf-8",83 dataType:"json",84 async:false,85 success: function(data){86 if (data.code == 0) {87 if (data.result.size > 0) {88 node.nodes = data.result.list;89 for (var i = 0; i < node.nodes.length; ++i){90 node.nodes[i].text = node.nodes[i].name;91 node.nodes[i].type = 392 node.type = 2;93 }94 95 }96 } else {97 alert('è·å设å¤å¤±è´¥');98 console.log(data);99 }100 }101 }); // end of ajax102 }103 } else {104 // alert("è·å设å¤å¤±è´¥");105 console.log(data);106 }107 }108 })// end of ajax 109 }110 111 var trees = rootNode;112 113 return trees;114}115function getGroupTree() {116 // Some logic to retrieve, or generate tree structure117 var data = {118 "level": 0119 }120 var rootNode = [];121 $.ajax({122 url:'/v1/device/level/root',123 type:"POST",124 data: JSON.stringify(data),125 contentType:"application/json; charset=utf-8",126 dataType:"json",127 async:false,128 success: function(data){129 if (data.code == 0) {130 rootNode = data.result.list;131 for (var i = 0; i < rootNode.length; ++i) {132 rootNode[i].text = rootNode[i].name;133 }134 } else {135 alert("è·å设å¤å¤±è´¥");136 console.log(data);137 }138 }139 })// end of ajax140 141 var nodes = rootNode.slice();142 while (nodes.length) {143 var node = nodes.pop();144 var data = {145 "id": node.id,146 "level": node.level147 }148 $.ajax({149 url:'/v1/device/level/childs',150 type:"POST",151 data: JSON.stringify(data),152 contentType:"application/json; charset=utf-8",153 dataType:"json",154 async:false,155 success: function(data){156 if (data.code == 0) {157 if (data.result.size > 0) {158 node.nodes = data.result.list;159 for (var i = 0; i < node.nodes.length; ++i) {160 node.nodes[i].text = node.nodes[i].name;161 node.type = 1;162 }163 164 nodes = nodes.concat(node.nodes);165 } else {166 }167 } else {168 // alert("è·å设å¤åç»æ 失败");169 console.log(data);170 }171 }172 })// end of ajax 173 }174 175 var trees = rootNode;176 177 return trees;178}179function getAllGroups(callback, multiple) {180 multiple = multiple || false;181 $.ajax({182 url:'/v1/device/allgroups',183 type:"GET",184 dataType:"json",185 success: function(data){186 if (data.code == 0) {187 console.log('è·åææ设å¤åç»æå');188 var groups = data.result.list;189 $('#editGroup select').empty();190 $('#editGroup select').attr('multiple', multiple);191 $('#editGroup select').prepend("<option value='' selected='selected'></option>");192 for (var i = 0; i < groups.length; ++i) {193 $('#editGroup select').append($('<option>', {194 value: groups[i].id,195 text: groups[i].name,196 }).attr("comment",groups[i].comment)); 197 }198 $('#editGroup select').unbind('change');199 $('#editGroup select').change(function(){200 if (typeof callback !== "undefined")201 callback(this);202 });203 }204 else {205 console.log('è·åææ设å¤åç»å¤±è´¥');206 console.log(data);207 }208 } 209 });// end of ajax210}211$(function() {212 $('#sidebar-nav li:nth-child(1)').css('font-weight','bold');213 $('#sidebar-nav li:nth-child(1)').tab('show');214 $('#tree').treeview({data: getTree()});215 $('#groupTree').treeview({data: getGroupTree()});216 // æ·»å 设å¤217 $('#sidebar-nav li:nth-child(1)').click(function(){218 hideAll();219 $(this).css('font-weight','bold');220 $(this).tab('show');221 $('#createDevice').show();222 $('#createDevice input').show();223 $('#createDevice input').val('');224 $('#tree').show();225 $('#createDevice input').click(function(){226 $(this).tips({227 side:3,228 msg: $(this).attr('placeholder'),229 bg:'#307FC1',230 time: 1,231 x:5,232 });233 })234 $('#createDevice button').unbind('click');235 $('#createDevice button').click(function(){236 var levelSelected = $('#tree').treeview('getSelected');237 if (levelSelected.length != 1 && levelSelected.type != 2) {238 alert('请éæ©è®¾å¤æå¨çå±çº§');239 return ;240 }241 var parentId = levelSelected[0].id;242 var deviceID = $('#createDevice input:eq(0)').val();243 var name = $('#createDevice input:eq(1)').val();244 var nickName = $('#createDevice input:eq(2)').val()245 var latitude = parseFloat($('#createDevice input:eq(3)').val());246 var longitude = parseFloat($('#createDevice input:eq(4)').val());247 var beatInterval = parseInt($('#createDevice input:eq(5)').val());248 var photoInterval = parseInt($('#createDevice input:eq(6)').val());249 var resolution = parseInt($('#createDevice input:eq(7)').val());250 var mac = $('#createDevice input:eq(8)').val()251 var batteryVoltageLow = parseFloat($('#createDevice input:eq(9)').val());252 var chargeVoltageLow = parseFloat($('#createDevice input:eq(10)').val());253 var temperatureLow = parseFloat($('#createDevice input:eq(11)').val());254 var batteryVoltageHigh = parseFloat($('#createDevice input:eq(12)').val());255 var chargeVoltageHigh = parseFloat($('#createDevice input:eq(13)').val());256 var temperatureHigh = parseFloat($('#createDevice input:eq(14)').val());257 var deviceWorkBeginTime = $('#createDevice input:eq(15)').val()258 var deviceWorkEndTime = $('#createDevice input:eq(16)').val()259 // var refPicId = $('#createDevice input:eq(17)').val()260 // var refPicPath = $('#createDevice input:eq(18)').val()261 var data = {262 "parentId":parentId,263 "deviceID": deviceID,264 "name": name, 265 "nickName": nickName,266 "latitude": latitude,267 "longitude": longitude,268 "beatInterval": beatInterval,269 "resolution": resolution,270 "photoInterval": photoInterval,271 "mac": mac,272 "batteryVoltageLow": batteryVoltageLow,273 "chargeVoltageLow": chargeVoltageLow,274 "temperatureLow": temperatureLow,275 "batteryVoltageHigh": batteryVoltageHigh,276 "chargeVoltageHigh": chargeVoltageHigh,277 "temperatureHigh": temperatureHigh,278 "deviceWorkBeginTime": deviceWorkBeginTime,279 "deviceWorkEndTime": deviceWorkEndTime,280 // "refPicId": refPicId,281 // "refPicPath": refPicPath,282 };283 $.ajax({284 url:'/v1/device/create',285 type:"POST",286 data: JSON.stringify(data),287 contentType:"application/json; charset=utf-8",288 dataType:"json",289 success: function(data){290 if (data.code == 0) {291 alert('æ·»å 设å¤æå');292 $('#createDevice input').val('');293 $('#tree').treeview({data: getTree()});294 295 } else {296 alert('æ·»å 设å¤å¤±è´¥');297 console.log(data);298 }299 }300 }) // end of ajax301 });302 });303 // ç¼è¾è®¾å¤304 $('#sidebar-nav li:nth-child(2)').click(function(){305 hideAll();306 $(this).css('font-weight','bold');307 $(this).tab('show');308 $('#createDevice').show();309 $('#tree').show();310 $('#createDevice input').val('');311 $('#createDevice input').click(function(){312 $(this).tips({313 side:3,314 msg: $(this).attr('placeholder'),315 bg:'#307FC1',316 time: 1,317 x:5,318 });319 // $(this).tips({320 // side:1, //1,2,3,4 åå«ä»£è¡¨ ä¸å³ä¸å·¦321 // msg:'ä¸æ¹å¼¹åºæ¶æ¯ï¼3ç§åèªå¨æ¶å¤±ï¼é¼ æ æ¬æµ®æ¶ï¼èªå¨å»¶æ¶',//tipsçææ¬å
容322 // color:'#FFF',//æåé¢è²ï¼é»è®¤ä¸ºç½è²323 // bg:'#FD9720',//èæ¯è²ï¼é»è®¤ä¸ºçº¢è²324 // time:3,//é»è®¤ä¸º2 èªå¨å
³éæ¶é´ åä½ä¸ºç§ 0为ä¸å
³é ï¼ç¹å»æ示ä¹å¯ä»¥å
³éï¼325 // x:0,// é»è®¤ä¸º0 横åå移 æ£æ°åå³å移 è´æ°åå·¦å移326 // y:0 // é»è®¤ä¸º0 纵åå移 æ£æ°åä¸å移 è´æ°åä¸å移327 // }).tips({328 // side:2,329 // msg:'å³æ¹å¼¹åºæ¶æ¯ï¼5ç§åèªå¨æ¶å¤±ï¼é¼ æ æ¬æµ®æ¶ï¼èªå¨å»¶æ¶',330 // bg:'#AE81FF',331 // time:5332 // }).tips({333 // side:3,334 // msg:'ä¸æ¹å¼¹åºæ¶æ¯ï¼3ç§åèªå¨æ¶å¤±ï¼é¼ æ æ¬æµ®æ¶ï¼èªå¨å»¶æ¶',335 // bg:'#307FC1',336 // time:7337 // }).tips({338 // side:4,339 // msg:'å·¦æ¹å¼¹åºæ¶æ¯ï¼ä¸èªå¨æ¶å¤±ï¼é¼ æ ç¹å»æä¼æ¶å¤±',340 // time:0341 // });342 })343 $('#createDevice button').unbind('click');344 $('#createDevice button').click(function(){345 var parentId = $('#createDevice input:eq(0)').attr('parentId');346 var id = $('#createDevice input:eq(0)').attr('id');347 var name = $('#createDevice input:eq(0)').val();348 var nickName = $('#createDevice input:eq(1)').val();349 var latitude = parseFloat($('#createDevice input:eq(2)').val());350 var longitude = parseFloat($('#createDevice input:eq(3)').val());351 var beatInterval = parseInt($('#createDevice input:eq(4)').val());352 var photoInterval = parseInt($('#createDevice input:eq(5)').val());353 var mac = $('#createDevice input:eq(6)').val()354 var batteryVoltageLow = parseFloat($('#createDevice input:eq(7)').val());355 var chargeVoltageLow = parseFloat($('#createDevice input:eq(8)').val());356 var temperatureLow = parseFloat($('#createDevice input:eq(9)').val());357 var batteryVoltageHigh = parseFloat($('#createDevice input:eq(10)').val());358 var chargeVoltageHigh = parseFloat($('#createDevice input:eq(11)').val());359 var temperatureHigh = parseFloat($('#createDevice input:eq(12)').val());360 var data = {361 'parentId': parentId,362 "id": id,363 "name": name,364 'nickName': nickName,365 "latitude": latitude,366 "longitude": longitude,367 "beatInterval": beatInterval,368 "photoInterval": photoInterval,369 "mac": mac,370 "batteryVoltageLow": batteryVoltageLow,371 "chargeVoltageLow": chargeVoltageLow,372 "temperatureLow": temperatureLow,373 "batteryVoltageHigh": batteryVoltageHigh,374 "chargeVoltageHigh": chargeVoltageHigh,375 "temperatureHigh": temperatureHigh376 }377 $.ajax({378 url:'/v1/device/edit',379 type:"POST",380 data: JSON.stringify(data),381 contentType:"application/json; charset=utf-8",382 dataType:"json",383 success: function(data){384 if (data.code == 0) {385 alert('ç¼è¾è®¾å¤æå');386 } else {387 alert('ç¼è¾è®¾å¤å¤±è´¥');388 console.log(data);389 }390 }391 }) // end of ajax device/edit392 });393 $('#tree').on('nodeSelected', function(event, data) {394 if (data.type == 3) {395 var id = data.id;396 var data = {397 "id": id398 }399 $.ajax({400 url:'/v1/device/details',401 type:"get",402 data: data,403 contentType:"application/json; charset=utf-8",404 dataType:"json",405 success: function(data){406 if (data.code == 0) {407 var deviceInfo = data.result;408 $('#createDevice input:eq(0)').attr('parentId',deviceInfo.parentId);409 $('#createDevice input:eq(0)').attr('id',deviceInfo.id);410 $('#createDevice input:eq(0)').val(deviceInfo.name);411 $('#createDevice input:eq(1)').val(deviceInfo.nickName);412 $('#createDevice input:eq(2)').val(deviceInfo.latitude);413 $('#createDevice input:eq(3)').val(deviceInfo.longitude);414 $('#createDevice input:eq(4)').val(deviceInfo.beatInterval);415 $('#createDevice input:eq(5)').val(deviceInfo.photoInterval);416 $('#createDevice input:eq(6)').val(deviceInfo.mac)417 $('#createDevice input:eq(7)').val(deviceInfo.batteryVoltageLow);418 $('#createDevice input:eq(8)').val(deviceInfo.chargeVoltageLow);419 $('#createDevice input:eq(9)').val(deviceInfo.temperatureLow);420 $('#createDevice input:eq(10)').val(deviceInfo.batteryVoltageHigh);421 $('#createDevice input:eq(11)').val(deviceInfo.chargeVoltageHigh);422 $('#createDevice input:eq(12)').val(deviceInfo.temperatureHigh);423 } else {424 alert('è·å设å¤è¯¦æ
失败');425 console.log(data);426 }427 }428 }) // end of ajax429 }430 }) // end of nodeSelected event431 })432 // å é¤è®¾å¤433 $('#sidebar-nav li:nth-child(3)').click(function(){434 hideAll();435 $(this).css('font-weight','bold');436 $(this).tab('show');437 $('#tree').show();438 $('#deleteDevice').show();439 $('#deleteDevice').unbind('click');440 $('#deleteDevice').click(function(){441 if(confirm("æ¯å¦ç¡®å®å é¤è¯¥è®¾å¤ï¼")){442 var levelSelected = $('#tree').treeview('getSelected');443 if (levelSelected.length != 1 && levelSelected.type != 3) {444 alert('请éæ©è®¾å¤');445 return ;446 }447 var id = levelSelected[0].id;448 var data = {449 "id": id450 }451 $.ajax({452 url:'/v1/device/delete',453 type:"POST",454 data: JSON.stringify(data),455 contentType:"application/json; charset=utf-8",456 dataType:"json",457 success: function(data){458 if (data.code == 0) {459 alert('å é¤è®¾å¤æå');460 $('#tree').treeview({data: getTree()});461 } else {462 alert('å é¤è®¾å¤å¤±è´¥');463 console.log(data);464 }465 }466 }) // end of ajax device/delete 467 }468 });469 });470 // å建åç»471 $('#sidebar-nav li:nth-child(4)').click(function(){472 hideAll();473 $(this).css('font-weight','bold');474 $(this).tab('show');475 $('#groupTree').show();476 $('#createGroup').show();477 $('#createGroup button').unbind('click');478 $('#createGroup input').val('');479 $('#createGroup input:eq(0)').prop("readonly", false);480 $('#createGroup button').click(function(){481 var levelSelected = $('#groupTree').treeview('getSelected');482 if (levelSelected.length != 1) {483 var parentId = '';484 var parentLevel = -1;485 }else{486 var parentId = levelSelected[0].id;487 var parentLevel = levelSelected[0].level;488 }489 var name = $('#createGroup input:eq(0)').val();490 // var comment = $('#createGroup input:eq(1)').val();491 var data = {492 "name": name,493 "parentId": parentId,494 "parentLevel": parentLevel,495 // "comment": comment496 };497 $.ajax({498 url:'/v1/device/level/create',499 type:"POST",500 data: JSON.stringify(data),501 contentType:"application/json; charset=utf-8",502 dataType:"json",503 success: function(data){504 if (data.code == 0) {505 alert('å建åç»æå');506 $('#createGroup input').val('');507 $('#groupTree').treeview({data: getGroupTree()});508 } else {509 alert('å建åç»å¤±è´¥');510 console.log(data);511 }512 }513 })// end of ajax device/group/create514 });// end of button click515 }); // end of å建åç»516 // ç¼è¾åç»517 $('#sidebar-nav li:nth-child(5)').click(function(){518 hideAll();519 $(this).css('font-weight','bold');520 $(this).tab('show');521 $('#createGroup').show();522 $('#groupTree').show();523 $('#createGroup button').unbind('click');524 $('#groupTree').on('nodeSelected', function(event, data) {525 var name = data.name;526 $('#createGroup input:eq(0)').val(name);527 }) 528 $('#createGroup button').click(function() {529 var levelSelected = $('#groupTree').treeview('getSelected');530 if(levelSelected.length == 0){531 alert('请éæ©åç»');532 }else{533 var id = levelSelected[0].id;534 var level = levelSelected[0].level;535 var name = levelSelected[0].name;536 var newName = $('#createGroup input:eq(0)').val();537 var data = {538 'id' : id,539 'level': level,540 'name': name,541 'newName': newName542 };543 $.ajax({544 url:'/v1/device/level/edit',545 type:"POST",546 data: JSON.stringify(data),547 contentType:"application/json; charset=utf-8",548 dataType:"json",549 success: function(data){550 if (data.code == 0) {551 alert('ç¼è¾åç»æå');552 $('#createGroup input').val('');553 $('#groupTree').treeview({data: getGroupTree()});554 } else {555 alert('ç¼è¾åç»å¤±è´¥');556 console.log('ç¼è¾åç»å¤±è´¥');557 }558 }559 })// end of ajax560 }561 });562 });// end of ç¼è¾åç»563 // å é¤åç»564 $('#sidebar-nav li:nth-child(6)').click(function(){565 hideAll();566 $(this).css('font-weight','bold');567 $(this).tab('show');568 $('#groupTree').show();569 $('#deleteGroupSubmit').show();570 $('#deleteGroupSubmit').unbind('click').click(function(){571 if(confirm("æ¯å¦ç¡®å®å é¤è¯¥è®¾å¤åç»ï¼å é¤åå
¶ä¸ææåç»å设å¤ä¹è¢«å é¤")){572 var levelSelected = $('#groupTree').treeview('getSelected');573 if(levelSelected.length == 0){574 alert('请éæ©åç»');575 }else{576 var id = levelSelected[0].id;577 var list = [];578 list[0] = {579 'id':id580 };581 var data = {582 'list': list583 }584 $.ajax({585 url:'/v1/device/level/delete',586 type:"POST",587 data: JSON.stringify(data),588 contentType:"application/json; charset=utf-8",589 dataType:"json",590 success: function(data){591 if (data.code == 0){592 alert('å é¤åç»æå');593 $('#groupTree').treeview({data: getGroupTree()});594 }595 else {596 alert('å é¤åç»å¤±è´¥');597 console.log(data);598 }599 }600 }); // end of ajax601 }602 } 603 });604 });// end of å é¤ åç»605 // 管çåç»606 $('#sidebar-nav li:nth-child(7)').click(function(){607 hideAll();608 $(this).css('font-weight','bold');609 $(this).tab('show');610 $('#editGroup').show();611 $('#tree').show();612 $('#tree').treeview({data: getTree(), multiSelect: true});613 614 function setManageGroup(ele) {615 var selectedNodes = $('#tree').treeview('getSelected');616 for (var i = 0; i < selectedNodes.length; ++i) {617 $('#tree').treeview('unselectNode',selectedNodes[i]);618 }619 var data = {620 'id':$(ele).val()621 }622 $.ajax({623 url:'/v1/device/group/listmembers',624 type:"GET",625 data: data,626 contentType:"application/json; charset=utf-8",627 dataType:"json",628 success: function(data){629 if (data.code == 0) {630 var nodesUnselected = $('#tree').treeview('getUnselected');631 var devicesList = data.result.list;632 for (var j = 0; j < devicesList.length; ++j) {633 for (var i = 0; i < nodesUnselected.length; ++i) {634 if (nodesUnselected[i].id == devicesList[j].id)635 $('#tree').treeview('selectNode', [nodesUnselected[i].nodeId]);636 }637 }638 } else {639 alert('è·ååç»æå失败');640 console.log(data);641 }642 }643 }) // end of device/group/listmembers644 }645 getAllGroups(setManageGroup);646 $('#editGroup button').show();647 $('#editGroup button').unbind('click');648 $('#editGroup button').click(function(){649 var id = $('#editGroup select').val();650 var list = [];651 var selectedDevices = $('#tree').treeview('getSelected');652 for (var i = 0; i < selectedDevices.length; ++i) {653 if (selectedDevices[i].type == 3) {654 list.push({"deviceId":selectedDevices[i].id});655 }656 }657 var data = {658 'id':id,659 'list': list660 }661 $.ajax({662 url:'/v1/device/group/setmembers',663 type:"POST",664 data: JSON.stringify(data),665 contentType:"application/json; charset=utf-8",666 dataType:"json",667 success: function(data){668 if (data.code == 0){669 alert('设置åç»æåæå');670 } else {671 alert('设置åç»æå失败');672 console.log(data);673 }674 }675 }) // end of ajax device/group/setmembers676 });677 });678 $('#sidebar-nav li:nth-child(8)').click(function(){679 hideAll();680 $(this).css('font-weight','bold');681 $(this).tab('show');682 $('#search').show();683 $('#search button').unbind('click').click(function(){684 var search = $('#search input:eq(0)').val();685 if(search == ""){686 alert("请è¾å
¥æ¨è¦æ¥è¯¢çå
容ï¼");687 }else{688 var data = {689 "search" : search,690 }691 $.ajax({692 url:'/v1/device/info/search',693 type:"POST",694 data: JSON.stringify(data),695 contentType:"application/json; charset=utf-8",696 dataType:"json",697 success: function(data){698 if (data.code == 0){699 $('#searchList').show();700 product = data.result.deviceProductList;701 $('#deviceProductListSearch').hide();702 $('#deviceProductListSearch tbody').empty();703 if(product.length > 0){704 $('#deviceProductListSearch').show();705 706 for (var i = 0; i < product.length; ++i) {707 $('#deviceProductListSearch tbody').append('<tr><td>'+product[i].id+'</td><td>' + product[i].deviceName + '</td><td>'+product[i].deviceManufactorName708 +'</td><td>'+product[i].deviceMeid+'</td></tr>'); 709 }710 }711 if(product.length == 0)712 {713 $('#searchList').hide();714 alert('æç´¢ä¸å°ç¸å
³ä¿¡æ¯ï¼è¯·éæ°è¾å
¥');715 }716 $('#search input:eq(0)').val("") 717 } else {718 alert('æç´¢ä¸å°ç¸å
³ä¿¡æ¯ï¼è¯·éæ°è¾å
¥');719 $('#search input:eq(0)').val("")720 console.log(data);721 }722 }723 })724 } 725 });726 });727 // 设å¤æ¤é²è®¾ç½®728 $('#sidebar-nav li:nth-child(9)').click(function(){729 hideAll();730 $(this).css('font-weight','bold');731 $(this).tab('show');732 $('#tree').show();733 $('#tree').treeview({data: getTree()});734 $('#calendarWrapper').show();735 $('#calendar').fullCalendar('destroy');736 var i = 0;737 function renderCalendar() {738 $('#calendar').fullCalendar({739 header: {740 left: 'prev,next today',741 center: 'title',742 right: 'agendaWeek'743 },744 defaultDate: '2016-05-12',745 timezone: false,746 selectable: true,747 editable: false,748 eventLimit: true, // allow "more" link when too many events749 loading: function(bool) {750 $('#loading').toggle(bool);751 },752 defaultView:'agendaWeek',753 eventRender: function(event, el) {754 // render the timezone offset below the event title755 if (event.start.hasZone()) {756 el.find('.fc-title').after(757 $('<div class="tzo"/>').text(event.start.format('Z'))758 );759 }760 },761 dayClick: function(date) {762 console.log('dayClick', date.format());763 },764 select: function (start, end, jsEvent, view) {765 var eventData = {766 id: i++,767 start: start,768 end: end,769 editable: true770 };771 $('#calendar').fullCalendar('renderEvent', eventData, true);772 $("#calendar").fullCalendar("unselect");773 },774 selectOverlap: function(event) {775 return false;776 },777 eventClick: function(calEvent, jsEvent, view) {778 $('#calendar').fullCalendar('removeEvents', calEvent.id);779 },780 });781 $('#calendar .fc-toolbar').remove();782 $('#calendar th:eq(1)').text('Sun');783 $('#calendar th:eq(2)').text('Mon');784 $('#calendar th:eq(3)').text('Tue');785 $('#calendar th:eq(4)').text('Wed');786 $('#calendar th:eq(5)').text('Thu');787 $('#calendar th:eq(6)').text('Fri');788 $('#calendar th:eq(7)').text('Sat');789 }790 renderCalendar();791 $('#tree').unbind('nodeSelected');792 $('#tree').on('nodeSelected', function(event, data) {793 var id, level, type;794 var id = data.id;795 if (data.type == 3) {796 type = 0;797 } else {798 type = 1;799 level = data.level;800 }801 var data = {802 "id": id,803 'level': level,804 'type': type805 }806 $.ajax({807 url:'/v1/disalert/details',808 type:"get",809 data: data,810 contentType:"application/json; charset=utf-8",811 dataType:"json",812 success: function(data){813 if (data.code == 0) {814 var list = data.result.list;815 var dateEvents = [];816 var dayOfWeek = [8, 9, 10, 11, 12, 13, 14];817 for (var i = 0; i < list.length; ++i) {818 dateEvents[i] = {};819 dateEvents[i].start = new Date(2016, 4, dayOfWeek[list[i].dayOfWeek], list[i].startHour, list[i].startMinute);820 dateEvents[i].end = new Date(2016, 4, dayOfWeek[list[i].dayOfWeek], list[i].endHour, list[i].endMinute);821 dateEvents[i].id= i++;822 dateEvents[i].title='';823 }824 $('#calendar').fullCalendar('addEventSource',dateEvents);825 } else {826 alert('è·å设å¤æ¤é²ä¿¡æ¯å¤±è´¥');827 console.log(data);828 }829 }830 }) // end of ajax831 }) // end of nodeSelected event832 $('#calendarWrapper button:eq(-1)').unbind('click');833 $('#calendarWrapper button:eq(-1)').click(function(){834 var levelSelected = $('#tree').treeview('getSelected');835 if (levelSelected.length != 1) {836 alert('请éæ©è®¾å¤');837 return ;838 }839 var id = levelSelected[0].id, type, level;840 if (levelSelected[0].type != 3) {841 type = 1;842 level = levelSelected.level;843 } else {844 type = 0;845 }846 var events = $('#calendar').fullCalendar('clientEvents');847 var list = [];848 for (var i = 0; i < events; ++i) {849 list[i] = {};850 list[i].dayOfWeek = events[i].start.weekday();851 list[i].startHour = events[i].start.hour();852 list[i].startMinute = events[i].start.minutes();853 list[i].endHour = events[i].end.weekday();854 list[i].endMinute = events[i].end.weekday();855 }856 var data = {857 'id': id,858 'level': level,859 'type': type,860 'list': list,861 };862 $.ajax({863 url:'/v1/disalert/set',864 type:"POST",865 data: JSON.stringify(data),866 contentType:"application/json; charset=utf-8",867 dataType:"json",868 async:false,869 success: function(data){870 if (data.code == 0) {871 alert('设å¤æ¤é²æå');872 } else {873 alert('设å¤æ¤é²å¤±è´¥');874 console.log(data);875 }876 }877 }) // end of ajax878 });879 });...
device.test.js
Source:device.test.js
...39 it('should return permissions', async () => {40 await getPermissionsDevices().set(withAdmin)41 });42 it('admin have permissions on device', async () => {43 await createDevice(device).set(withNormal)44 let res = await getPermissionsDevices().set(withAdmin)45 let scopes = res.body.find(p => p.resource == device.id).scopes46 chai.expect(scopes).members(['devices:view', 'devices:update', 'devices:delete', 'devices-data:create', 'devices-data:view']);47 });48 it('admin have permissions on private device', async () => {49 await createDevice({ ...device, visibility: 'private' }).set(withNormal)50 let res = await getPermissionsDevices().set(withAdmin)51 let scopes = res.body.find(p => p.resource == device.id).scopes52 chai.expect(scopes).members(['devices:view', 'devices:update', 'devices:delete', 'devices-data:create', 'devices-data:view']);53 });54 it('normal user have permissions on own device', async () => {55 await createDevice(device).set(withNormal)56 let res = await getPermissionsDevices().set(withNormal)57 let scopes = res.body.find(p => p.resource == device.id).scopes58 chai.expect(scopes).members(['devices:view', 'devices:update', 'devices:delete', 'devices-data:create', 'devices-data:view']);59 });60 it('normal user can see public device and add data', async () => {61 await createDevice(device).set(withAdmin)62 let res = await getPermissionsDevices().set(withNormal)63 let scopes = res.body.find(p => p.resource == device.id).scopes64 chai.expect(scopes).members(['devices:view', 'devices-data:view', 'devices-data:create']);65 });66 it('normal user cannot see private device', async () => {67 await createDevice({ ...device, visibility: 'private' }).set(withAdmin)68 let res = await getPermissionsDevices().set(withNormal)69 res.status.should.satisfy((s) => {70 switch (s) {71 case 200:72 return !res.body.map(s => s.id).includes(device.id);73 case 403:74 return true;75 default:76 return false;77 }78 });79 });80 });81 describe('Get Devices', () => {82 it('admin can get devices', async () => {83 await createDevice(device).set(withAdmin)84 let res = await getDevices().set(withAdmin)85 chai.expect(res.body.map(s => s.id)).to.include(device.id);86 });87 it('admin can see private devices', async () => {88 await createDevice({ ...device, visibility: 'private' }).set(withNormal)89 let res = await getDevices().set(withAdmin)90 chai.expect(res.body.map(s => s.id)).to.include(device.id);91 });92 it('normal user can see public devices', async () => {93 await createDevice(device).set(withAdmin)94 let res = await getDevices().set(withNormal)95 chai.expect(res.body.map(s => s.id)).to.include(device.id);96 });97 it('normal user can see own devices', async () => {98 await createDevice({ ...device, visibility: 'private' }).set(withNormal)99 let res = await getDevices().set(withNormal)100 chai.expect(res.body.map(s => s.id)).to.include(device.id);101 });102 it('normal user CANNOT see private devices', async () => {103 await createDevice({ ...device, visibility: 'private' }).set(withAdmin)104 let res = await getDevices().set(withNormal)105 res.status.should.satisfy((s) => {106 switch (s) {107 case 200:108 return !res.body.map(s => s.id).includes(device.id);109 case 403:110 return true;111 default:112 return false;113 }114 });115 });116 it('Limit devices', async () => {117 await createDevice(device).set(withAdmin)118 let res = await chai.request(baseUrl).get(`/devices?limit=1`).set(withAdmin)119 chai.expect(res.body.length).to.equal(1);120 });121 it('Offset devices', async () => {122 await createDevice(device).set(withAdmin)123 await createDevice({...device, id: 'test2'}).set(withAdmin)124 let res = await chai.request(baseUrl).get(`/devices?limit=1&offset=1`).set(withAdmin)125 chai.expect(res.body.length).to.equal(1);126 await deleteDevice('test2').set(withAdmin)127 });128 });129 describe('Create device', () => {130 it('device is created by admin', async () => {131 let res = await createDevice(device).set(withAdmin)132 res.should.have.status(204);133 });134 it('device is created by normal user', async () => {135 let res = await createDevice(device).set(withNormal)136 res.should.have.status(204);137 });138 it('device with the same id is rejected', async () => {139 await createDevice(device).set(withAdmin)140 let res = await createDevice(device).set(withNormal)141 res.should.have.status(422);142 });143 it('device with invalid data is rejected', async () => {144 let wrong = Object.assign({}, device)145 delete (wrong.id)146 let res = await createDevice(wrong).set(withAdmin)147 res.should.have.status(400);148 });149 it('device with the invalid id is rejected', async () => {150 await createDevice(device).set(withAdmin)151 let res = await createDevice({...device, id: '='}).set(withNormal)152 res.should.have.status(400);153 });154 });155 describe('Get a Single Device', () => {156 it('retrieved device has all the correct values', async () => {157 const resC = await createDevice(device).set(withAdmin);158 resC.should.have.status(204);159 let res = await getDevice(device.id).set(withAdmin)160 res.should.have.status(200);161 res.body.should.be.a('object');162 //all fields of original device should be here163 res.body.should.deep.include(device);164 //read-only fields should be here165 res.body.should.have.property('date_created');166 res.body.should.have.property('date_modified');167 res.body.should.have.property('owner').eql("cdupont");168 });169 it('non existent id is rejected', async () => {170 let res = await getDevice('this-id-does-not-exist').set(withAdmin)171 res.should.have.status(404);172 });173 it('admin can see private device', async () => {174 await createDevice({ ...device, visibility: 'private' }).set(withNormal)175 let res = await getDevice(device.id).set(withAdmin)176 res.body.should.have.property('id').eql(device.id);177 });178 it('normal user can see public device', async () => {179 await createDevice(device).set(withAdmin)180 let res = await getDevice(device.id).set(withNormal)181 res.body.should.have.property('id').eql(device.id);182 });183 it('normal user can see own device', async () => {184 await createDevice({ ...device, visibility: 'private' }).set(withNormal)185 let res = await getDevice(device.id).set(withNormal)186 res.body.should.have.property('id').eql(device.id);187 });188 it('normal user CANNOT see private device', async () => {189 await createDevice({ ...device, visibility: 'private' }).set(withAdmin)190 let res = await getDevice(device.id).set(withNormal)191 res.should.have.status(403);192 });193 });194 describe('Insert Name', () => {195 it('name field should be updated', async () => {196 await createDevice(device).set(withAdmin)197 let res = await setDeviceAttr(device.id, "name", "SEN1").set(withAdmin)198 res.should.have.status(204);199 let res2 = await getDevice(device.id).set(withAdmin);200 res2.body.should.have.property('name').eql('SEN1');201 });202 it('normal user can update own device', async () => {203 await createDevice(device).set(withNormal)204 let res = await setDeviceAttr(device.id, "name", "SEN1").set(withNormal)205 let res2 = await getDevice(device.id).set(withNormal);206 res2.body.should.have.property('name').eql('SEN1');207 });208 it('normal user CANNOT update device that he does not own', async () => {209 await createDevice(device).set(withAdmin)210 let res = await setDeviceAttr(device.id, "name", "SEN1").set(withNormal)211 res.should.have.status(403);212 });213 });214 describe('Insert Location', () => {215 it('Location field should be updated', async () => {216 await createDevice(device).set(withAdmin)217 let res = await setDeviceLocation(device.id, { latitude: 5.36, longitude: 4.0083 }).set(withAdmin)218 res.should.have.status(204);219 let res2 = await getDevice(device.id).set(withAdmin)220 res2.body.should.have.property('location').eql({ "latitude": 5.36, "longitude": 4.0083 });221 });222 });223 describe('Insert Gateway', () => {224 it('gateway field should be updated', async () => {225 await createDevice(device).set(withAdmin)226 let res = await setDeviceAttr(device.id, "gateway_id", "GW1").set(withAdmin)227 res.should.have.status(204);228 let res2 = await getDevice(device.id).set(withAdmin);229 res2.body.should.have.property('gateway_id').eql('GW1');230 });231 });232 describe('Change owner', () => {233 it('owner should be updated', async () => {234 await createDevice(device).set(withNormal)235 let res = await setDeviceAttr(device.id, "owner", "cdupont").set(withAdmin)236 res.should.have.status(204);237 let res2 = await getDevice(device.id).set(withAdmin);238 res2.body.should.have.property('owner').eql('cdupont');239 let res3 = await getPermissionsDevices().set(withNormal)240 let scopes = res3.body.find(p => p.resource == device.id).scopes241 chai.expect(scopes).members(['devices:view', 'devices-data:create', 'devices-data:view']);242 });243 });244 describe('Remove Device', () => {245 it('admin can remove own device', async () => {246 await createDevice(device).set(withAdmin)247 let res = await deleteDevice(device.id).set(withAdmin)248 res.should.have.status(204);249 });250 it('admin can remove other device', async () => {251 await createDevice(device).set(withNormal)252 let res = await deleteDevice(device.id).set(withAdmin)253 res.should.have.status(204);254 });255 it('normal user can remove own device', async () => {256 await createDevice(device).set(withNormal)257 let res = await deleteDevice(device.id).set(withNormal)258 res.should.have.status(204);259 });260 it('normal user CANNOT remove device owned by other', async () => {261 await createDevice(device).set(withAdmin)262 let res = await deleteDevice(device.id).set(withNormal)263 res.should.have.status(403);264 });265 });...
sensor.test.js
Source:sensor.test.js
...39 }40 });41 describe('Get Sensors', () => {42 it('sensors are returned in an array', async () => {43 await createDevice(device).set(withAdmin)44 let res = await getSensors().set(withAdmin)45 res.should.have.status(200);46 chai.expect(res.body.map(m => m.id)).to.have.members(device.sensors.map(m => m.id));47 });48 it('normal user CANNOT see private sensors', async () => {49 await createDevice({ ...device, visibility: 'private' }).set(withAdmin)50 let res = await getSensors().set(withNormal)51 res.should.have.status(403);52 });53 });54 describe('Create sensor', () => {55 it('admin can create a sensor', async () => {56 await createDevice(device).set(withAdmin)57 let res = await createSensor(sensor).set(withAdmin)58 res.should.have.status(204);59 });60 it('normal user can create a sensor on his own device', async () => {61 await createDevice(device).set(withNormal)62 let res = await createSensor(sensor).set(withNormal)63 res.should.have.status(204);64 });65 it('normal user CANNOT create a sensor on a device owned by other', async () => {66 await createDevice(device).set(withAdmin)67 let res = await createSensor(sensor).set(withNormal)68 res.should.have.status(403);69 });70 });71 describe('Get a single Sensor', async () => {72 it('retrieved sensor values are correct', async () => {73 await createDevice(device).set(withAdmin)74 let res = await getSensor(sensor.id).set(withAdmin)75 res.should.have.status(200);76 //all fields of original device should be here77 res.body.should.deep.include(sensor);78 });79 });80 describe('Update Name of a Sensor', () => {81 it('name of sensor is updated', async () => {82 await createDevice(device).set(withAdmin)83 let res = await putSensorAttr(sensor.id, "name", "ss1").set(withAdmin)84 res.should.have.status(204);85 let res2 = await getSensor(sensor.id).set(withAdmin)86 res2.body.should.have.property('name').eql('ss1');87 });88 it('normal user CANNOT update attribute of device that he does not own', async () => {89 await createDevice(device).set(withAdmin)90 let res = await putSensorAttr(sensor.id, "name", "ss1").set(withNormal)91 res.should.have.status(403);92 });93 });94 describe('Update quantity kind of a Sensor', () => {95 it('quantity kind is updated', async () => {96 await createDevice(device).set(withAdmin)97 let res = await putSensorAttr(sensor.id, "quantity_kind", "Temperature").set(withAdmin)98 res.should.have.status(204);99 let res2 = await getSensor(sensor.id).set(withAdmin)100 res2.body.should.have.property('quantity_kind').eql('Temperature');101 });102 });103 describe('Update sensing device', () => {104 it('sensing device is updated', async () => {105 await createDevice(device).set(withAdmin)106 let res = await putSensorAttr(sensor.id, "sensor_kind", "Thermometer").set(withAdmin)107 res.should.have.status(204);108 let res2 = await getSensor(sensor.id).set(withAdmin)109 res2.body.should.have.property('sensor_kind').eql('Thermometer');110 });111 });112 describe('Update unit', () => {113 it('unit should be updated', async () => {114 await createDevice(device).set(withAdmin)115 let res = await putSensorAttr(sensor.id, "unit", "DegreeCelcius").set(withAdmin)116 res.should.have.status(204);117 let res2 = await getSensor(sensor.id).set(withAdmin)118 res2.body.should.have.property('unit').eql('DegreeCelcius');119 });120 });121 describe('push sensor value', () => {122 it('string value is pushed', async () => {123 await createDevice(device).set(withAdmin)124 let res = await pushSensorValue(sensor.id, { "value": "25.6", "timestamp": "2016-06-08T18:20:27.873Z" })125 res.should.have.status(204);126 let res2 = await getSensor(sensor.id).set(withAdmin)127 res2.body.value.should.deep.include({ "value": "25.6", "timestamp": "2016-06-08T18:20:27Z" });128 res2.body.value.should.have.property('date_received');129 });130 it('normal user can push on public device', async () => {131 await createDevice(device).set(withAdmin)132 let res = await pushSensorValue(sensor.id, { "value": "25.6", "timestamp": "2016-06-08T18:20:27.873Z" })133 res.should.have.status(204);134 });135 it('normal user CANNOT push on private device', async () => {136 await createDevice({ ...device, visibility: 'private' }).set(withAdmin)137 let res = await pushSensorValue(sensor.id, { "value": "25.6", "timestamp": "2016-06-08T18:20:27Z" }).set(withNormal)138 res.should.have.status(403);139 });140 it('number value is pushed', async () => {141 await createDevice(device).set(withAdmin)142 let res = await pushSensorValue(sensor.id, { "value": 25.6 })143 res.should.have.status(204);144 let res2 = await getSensor(sensor.id).set(withAdmin)145 res2.body.value.should.deep.include({ "value": 25.6 });146 });147 it('string value is pushed', async () => {148 await createDevice(device).set(withAdmin)149 let res = await pushSensorValue(sensor.id, { "value": "A" })150 res.should.have.status(204);151 let res2 = await getSensor(sensor.id).set(withAdmin)152 res2.body.value.should.deep.include({ "value": "A" });153 });154 it('boolean value is pushed', async () => {155 await createDevice(device).set(withAdmin)156 let res = await pushSensorValue(sensor.id, { "value": true })157 res.should.have.status(204);158 let res2 = await getSensor(sensor.id).set(withAdmin)159 res2.body.value.should.deep.include({ "value": true });160 });161 it('array value is pushed', async () => {162 await createDevice(device).set(withAdmin)163 let res = await pushSensorValue(sensor.id, { "value": [true] })164 res.should.have.status(204);165 let res2 = await getSensor(sensor.id).set(withAdmin)166 res2.body.value.should.deep.include({ "value": [true] });167 });168 it('object value is pushed', async () => {169 await createDevice(device).set(withAdmin)170 let res = await pushSensorValue(sensor.id, { "value": { a: 1, b: "2" } })171 res.should.have.status(204);172 let res2 = await getSensor(sensor.id).set(withAdmin)173 res2.body.value.should.deep.include({ "value": { a: 1, b: "2" } });174 });175 it('plain/text number value is pushed', async () => {176 await createDevice(device).set(withAdmin)177 let res = await pushSensorValuePlain(sensor.id, "26.6")178 res.should.have.status(204);179 let res2 = await getSensor(sensor.id).set(withAdmin)180 res2.body.value.should.deep.include({ "value": "26.6" });181 });182 });...
01-sample-tests.js
Source:01-sample-tests.js
...94 expect(app.listMostPopularApps).to.have.length(1);95 });96 });97 describe('Device tests', function() {98 it('expect createDevice() with invalid hostname to throw', function() {99 expect(() => result.createDevice(42, [])).to.throw();100 expect(() => result.createDevice('', [])).to.throw();101 expect(() => result.createDevice('123456789012345678901234567890123', [])).to.throw();102 });103 it('expect createDevice() with invalid app to throw', function() {104 expect(() => result.createDevice('pesho', 7)).to.throw();105 expect(() => result.createDevice('pesho', [7])).to.throw();106 expect(() => result.createDevice('pesho', ['gosho'])).to.throw();107 expect(() => result.createDevice('pesho', [{name: 'gosho'}])).to.throw();108 const app = result.createApp('app', 'description', 1, 1);109 expect(() => result.createDevice('pesho', [app, {name: 'gosho'}])).to.throw();110 expect(() => result.createDevice('pesho', [{name: 'gosho'}, app])).to.throw();111 });112 it('expect Device object to have valid properties', function() {113 const app = result.createDevice('Peshoo', []);114 expect(app.hostname).to.equal('Peshoo');115 expect(app.apps).to.be.eql([]);116 });117 it('expect Device object to have all of its methods', function() {118 const device = result.createDevice('Peshoo', []);119 expect(device.search).to.be.a('function');120 expect(device.search).to.have.length(1);121 expect(device.install).to.be.a('function');122 expect(device.install).to.have.length(1);123 expect(device.uninstall).to.be.a('function');124 expect(device.uninstall).to.have.length(1);125 expect(device.listInstalled).to.be.a('function');126 expect(device.listInstalled).to.have.length(0);127 expect(device.update).to.be.a('function');128 expect(device.update).to.have.length(0);129 });130 });131 });132});
device.server.test.js
Source:device.server.test.js
...5 env = jsvAndSchemas.env,6 schema = jsvAndSchemas.schemas['device.json'];7suite('#json.schemas.device', function () {8 test('valid', function () {9 var result = env.validate(helper.createDevice(), schema);10 expect(result.errors).to.be.empty(result);11 });12 test('name missing', function () {13 var device = helper.createDevice();14 delete device.name;15 helper.assertSinglePropertyFailure(env, device, schema, "name", "required");16 });17 test('itemReference missing', function () {18 var device = helper.createDevice();19 delete device.itemReference;20 helper.assertSinglePropertyFailure(env, device, schema, "itemReference", "required");21 });22 test('hasActiveCriticalAlarms missing', function () {23 var device = helper.createDevice();24 delete device.hasActiveCriticalAlarms;25 helper.assertSinglePropertyFailure(env, device, schema, "hasActiveCriticalAlarms", "required");26 });27 test('description missing', function () {28 var device = helper.createDevice();29 delete device.description;30 helper.assertSinglePropertyFailure(env, device, schema, "description", "required");31 });32 test('address missing', function () {33 var device = helper.createDevice();34 delete device.address;35 helper.assertSinglePropertyFailure(env, device, schema, "address", "required");36 });37 test('equipmentModels missing', function () {38 var device = helper.createDevice();39 delete device.equipmentModels;40 helper.assertSinglePropertyFailure(env, device, schema, "equipmentModels", "required");41 });42 test('equipmentModels must have 1 entry', function () {43 var device = helper.createDevice();44 device.equipmentModels.pop();45 helper.assertSinglePropertyFailure(env, device, schema, "equipmentModels", "minItems");46 });47 test('isOnline missing', function () {48 var device = helper.createDevice();49 delete device.isOnline;50 helper.assertSinglePropertyFailure(env, device, schema, "isOnline", "required");51 });52 test('hasMtgConnected missing', function () {53 var device = helper.createDevice();54 delete device.hasMtgConnected;55 helper.assertSinglePropertyFailure(env, device, schema, "hasMtgConnected", "required");56 });57 test('name wrong type', function () {58 var device = helper.createDevice();59 device.name = {};60 helper.assertSinglePropertyFailure(env, device, schema, "name", "type");61 });62 test('itemReference wrong type', function () {63 var device = helper.createDevice();64 device.itemReference = {};65 helper.assertSinglePropertyFailure(env, device, schema, "itemReference", "type");66 });67 test('hasActiveCriticalAlarms wrong type', function () {68 var device = helper.createDevice();69 device.hasActiveCriticalAlarms = {};70 helper.assertSinglePropertyFailure(env, device, schema, "hasActiveCriticalAlarms", "type");71 });72 test('description wrong type', function () {73 var device = helper.createDevice();74 device.description = {};75 helper.assertSinglePropertyFailure(env, device, schema, "description", "type");76 });77 test('address wrong type', function () {78 var device = helper.createDevice();79 device.address = {};80 helper.assertSinglePropertyFailure(env, device, schema, "address", "type");81 });82 test('isOnline wrong type', function () {83 var device = helper.createDevice();84 device.isOnline = {};85 helper.assertSinglePropertyFailure(env, device, schema, "isOnline", "type");86 });...
test_device_manager.js
Source:test_device_manager.js
...8 beforeEach(function() {9 this.devMgr = new DeviceManager();10 });11 it('should create device', function () {12 assert.notEqual(this.devMgr.createDevice("123456", devTypes.DEV_TYPE_LORAMOTE), undefined);13 assert.equal(this.devMgr.getDeviceNb(), 1);14 });15 it('should remove device', function () {16 this.devMgr.createDevice("123456", devTypes.DEV_TYPE_LORAMOTE);17 this.devMgr.createDevice("78945688888", devTypes.DEV_TYPE_LORAMOTE);18 assert.equal(this.devMgr.getDeviceNb(), 2);19 this.devMgr.removeDevice("123456");20 assert.equal(this.devMgr.getDeviceNb(), 1);21 });22 it('should remove device silently if device is not there', function () {23 this.devMgr.createDevice("123456", devTypes.DEV_TYPE_LORAMOTE);24 this.devMgr.createDevice("78945688888", devTypes.DEV_TYPE_LORAMOTE);25 assert.equal(this.devMgr.getDeviceNb(), 2);26 this.devMgr.removeDevice("8888888");27 assert.equal(this.devMgr.getDeviceNb(), 2);28 });29 it('should create LoRaMote device', function () {30 var dev = this.devMgr.createDevice("123456", devTypes.DEV_TYPE_LORAMOTE);31 assert.equal(dev.getType(), devTypes.DEV_TYPE_LORAMOTE);32 });33 it('should create Nucleo device', function () {34 var dev = this.devMgr.createDevice("123456", devTypes.DEV_TYPE_NUCLEO);35 assert.equal(dev.getType(), devTypes.DEV_TYPE_NUCLEO);36 });37 it('should error when creating a device already there', function () {38 this.devMgr.createDevice("123456", devTypes.DEV_TYPE_NUCLEO);39 assert.throws(() => this.devMgr.createDevice("123456", devTypes.DEV_TYPE_NUCLEO), "Device with this EUI already exists");40 });41 it('should retrieve devices from eui', function () {42 this.devMgr.createDevice("123456", devTypes.DEV_TYPE_NUCLEO);43 this.devMgr.createDevice("789123", devTypes.DEV_TYPE_LORAMOTE);44 this.devMgr.createDevice("456789", devTypes.DEV_TYPE_LORAMOTE);45 assert.notEqual(this.devMgr.findDevice("789123"), undefined);46 assert.equal(this.devMgr.findDevice("888888"), undefined);47 });48 });...
AddDevice.js
Source:AddDevice.js
...11 this.state = {12 teams: []13 };14 }15 createDevice(device) {16 this.props.actions.createDevice(device);17 }18 componentDidMount() {19 const data = FirebaseApi.getDatabaseValues('teams');20 data.on('value', function (snap) {21 const teams = [];22 snap.forEach(function (itemSnap) {23 const team = itemSnap.val();24 team.key = itemSnap.key;25 teams.push(team);26 });27 this.setState({teams: teams});28 }.bind(this));29 }30 render() {...
sensors.js
Source:sensors.js
1const { request } = require('graphql-request')2const expect = require('chai').expect3async function createDevice(uri) {4 it("create a device", async function() {5 const createQuery = `6 mutation {7 createDevice(device: {8 name: "camera-1",9 description: "Raspberry Pi 3b with POE and V2 camera",10 part_number: "wf-pi03-b1",11 device_type: PI3,12 tag_id: "tag-team-94",13 serial_number: "123456",14 mac_address: ["a1:b2:c1:d1:22:44"]15 }) {16 device_id17 name18 description19 part_number20 device_type21 tag_id22 serial_number23 mac_address24 }25 }26 `27 var response = await request(uri, createQuery)28 expect(response).to.not.equal(null)29 expect(response.createDevice.device_id).to.not.equal(null)30 expect(response.createDevice.name).to.equal("camera-1")31 expect(response.createDevice.tag_id).to.equal("tag-team-94")32 expect(response.createDevice.serial_number).to.equal("123456")33 })34}35exports.all = async function(uri) {36 await createDevice(uri)...
Using AI Code Generation
1const { remote } = require('webdriverio');2const opts = {3 capabilities: {4 }5};6(async () => {7 const client = await remote(opts);8 await client.createDevice({9 });10 await client.deleteSession();11})();12const { remote } = require('webdriverio');13const opts = {14 capabilities: {15 }16};17(async () => {18 const client = await remote(opts);19 await client.createDevice({20 });21 await client.deleteSession();22})();23const { remote } = require('webdriverio');24const opts = {25 capabilities: {
Using AI Code Generation
1var webdriverio = require('webdriverio');2var options = {3 desiredCapabilities: {4 }5};6var client = webdriverio.remote(options);7client.init()8 .then(function() {9 return client.createDevice('iPhone 6', 'iOS', '10.2', 'a1b2c3d4e5f6g7h8i9j0', 'KLMNOPQRST', 'iPhone Developer');10 })11 .then(function() {12 return client.end();13 })14 .catch(function(err) {15 console.log(err);16 });17commands.createDevice = async function(name, platform, version, udid, orgId, signingId) {18 ];19 return await this.proxyCommand('/wda/devices', 'POST', args);20};21commands.proxyCommand = async function(url, method, body) {22 var res = await request({23 headers: {24 }25 });26};27commands.createSession = async function(desiredCapabilities, requiredCapabilities, capabilities, 28 shouldStartSession) {29 if (shouldStartSession) {30 var res = await this.proxyCommand('/session', 'POST', {31 });32 }
Using AI Code Generation
1const { createDevice } = require('appium-xcuitest-driver');2const createDevice = createDevice({3});4const { createSession } = require('appium-xcuitest-driver');5const createSession = createSession({6});7const { deleteSession } = require('appium-xcuitest-driver');8const deleteSession = deleteSession({9});10const { getSessions } = require('appium-xcuitest-driver');11const getSessions = getSessions({12});13const { getSession } = require('appium-xcuitest-driver');14const getSession = getSession({15});16const { executeCommand } = require('appium-xcuitest-driver');17const executeCommand = executeCommand({18});
Using AI Code Generation
1var driver = require('appium-xcuitest-driver');2var xcode = require('appium-xcode');3var path = require('path');4var server = require('appium').server;5var port = 4723;6var caps = {7 app: path.resolve(__dirname, 'UICatalog.app')8};9var appium = server(port, caps);10appium.start(function(err) {11 if (err) {12 console.log('Appium server start failed!');13 return;14 }15 console.log('Appium server start successfully!');16 console.log('Appium server listen on port: ' + port);17 xcode.getVersion(function(err, version) {18 console.log('Xcode version is: ' + version);19 driver.createDevice(caps, function(err, device) {20 console.log('device is: ' + device);21 device.runApp(function(err, result) {22 console.log('run app result is: ' + result);23 });24 });25 });26});27device is: {"udid":"d6e4e8d0e6ca1b6e4f6c2b8d1e6c0f9b2b6e0e6c","deviceName":"iPhone 6","platformName":"iOS","platformVersion":"10.2","app":"/Users/yanzhen/Documents/Work/2017/02/20170220/UICatalog.app"}
Using AI Code Generation
1const wd = require('wd');2const chai = require('chai');3const chaiAsPromised = require('chai-as-promised');4chai.use(chaiAsPromised);5const expect = chai.expect;6const assert = chai.assert;7const should = chai.should();8const PORT = 4723;9const driver = wd.promiseChainRemote('localhost', PORT);10const desiredCapabilities = {11};12describe('test createDevice method', function () {13 this.timeout(0);14 before(async () => {15 await driver.init(desiredCapabilities);16 });17 after(async () => {18 await driver.quit();19 });20 it('should create a new real device', async () => {21 await driver.execute('mobile:createDevice', {22 });23 });24});
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!!