|
| 1 | +<!DOCTYPE html> |
| 2 | +<html> |
| 3 | + <head> |
| 4 | + <title>TEST CASE FOR COOKIES API</title> |
| 5 | + </head> |
| 6 | + <body> |
| 7 | + <h1>Hello World!</h1> |
| 8 | + We are using node.js |
| 9 | + <script> |
| 10 | + var gui = require('nw.gui'); |
| 11 | + var win = gui.Window.get( window.open('http://115.com')); |
| 12 | + var results = new Array(); |
| 13 | + var changed = false; |
| 14 | + // action 1: retrieve(get), retrieveAll(getAll) set |
| 15 | + // action 2: set |
| 16 | + // action 3: set and onChanged |
| 17 | + var action = gui.App.argv[0]; |
| 18 | + |
| 19 | + //retrieveAll(".115.com") |
| 20 | + function retrieveAll(domain) { |
| 21 | + win.cookies.getAll({ 'domain': domain}, function(cookies) { |
| 22 | + console.log(cookies); |
| 23 | + if (cookies != null && cookies != undefined) |
| 24 | + results.push(true); |
| 25 | + else |
| 26 | + results.push(false); |
| 27 | + // for (var i = 0; i < cookies.length; i++) { |
| 28 | + // console.log("name: " + cookies[i].name + "\n" + "path: " + cookies[i].path + "\n" + "domain: " + cookies[i].domain + "\n" + "session: " + cookies[i].session.toString() + "\n" + "value: " + cookies[i].value + " " + "\n"); |
| 29 | + // } |
| 30 | + }) |
| 31 | + } |
| 32 | + |
| 33 | + //remove("http://www.115.com","115_lang") |
| 34 | + function remove(url, name) { |
| 35 | + win.cookies.remove({ 'url': url, 'name': name }); |
| 36 | + } |
| 37 | + |
| 38 | + //set('.115.com',"http://www.115.com","115_lang","en") |
| 39 | + function set(domain, url, name, value) { |
| 40 | + win.cookies.set({ 'domain': domain, 'url': url, 'name': name, 'value': value, 'expirationDate': 9999999999}); |
| 41 | + } |
| 42 | + |
| 43 | + //retrieve("http://www.115.com","115_lang") |
| 44 | + function retrieve(url, name){ |
| 45 | + win.cookies.get({"url":url, "name": name},function (cookie){ |
| 46 | + console.log(cookie); |
| 47 | + if (cookie != null && cookie != undefined) { |
| 48 | + results.push(true); |
| 49 | + } else { |
| 50 | + results.push(false); |
| 51 | + } |
| 52 | + }); |
| 53 | + } |
| 54 | + |
| 55 | + win.cookies.onChanged.addListener(function() { |
| 56 | + changed = true; |
| 57 | + }); |
| 58 | + |
| 59 | + if (action == '1') { |
| 60 | + win.window.onload = function() { |
| 61 | + retrieve("http://www.115.com","115_lang");//get true |
| 62 | + retrieveAll(".115.com");//getAll true |
| 63 | + results.push(changed); |
| 64 | + win.close(); |
| 65 | + // true, true |
| 66 | + setTimeout(function() { |
| 67 | + var client = require('../../nw_test_app').createClient({ |
| 68 | + argv: gui.App.argv, |
| 69 | + data: results |
| 70 | + }); |
| 71 | + }, 1000); |
| 72 | + } |
| 73 | + } else if (action == '2') { |
| 74 | + win.close(); |
| 75 | + set('.115.com',"http://www.115.com","115_lang","en");//set true |
| 76 | + win = gui.Window.get(window.open('http://115.com')); |
| 77 | + win.window.onload = function() { |
| 78 | + if (win.window.document.title != "115 Cloud drive-Your files in the cloud-Easy&Safe") |
| 79 | + results.push(false); |
| 80 | + else |
| 81 | + results.push(true); |
| 82 | + results.push(changed); |
| 83 | + win.close(); |
| 84 | + setTimeout(function() { |
| 85 | + var client = require('../../nw_test_app').createClient({ |
| 86 | + argv: gui.App.argv, |
| 87 | + data: results |
| 88 | + }); |
| 89 | + }, 2000) |
| 90 | + } |
| 91 | + } else if (action == '3') { |
| 92 | + win.close(); |
| 93 | + set('.115.com',"http://www.115.com","115_lang","zh");//set true |
| 94 | + win = gui.Window.get(window.open('http://115.com')); |
| 95 | + win.window.onload = function() { |
| 96 | + if (win.window.document.title == "115 Cloud drive-Your files in the cloud-Easy&Safe") |
| 97 | + results.push(false); |
| 98 | + else |
| 99 | + results.push(true); |
| 100 | + setTimeout(function() { |
| 101 | + win.close(); |
| 102 | + results.push(changed); |
| 103 | + }, 1000); |
| 104 | + // true |
| 105 | + setTimeout(function() { |
| 106 | + var client = require('../../nw_test_app').createClient({ |
| 107 | + argv: gui.App.argv, |
| 108 | + data: results |
| 109 | + }); |
| 110 | + }, 3000); |
| 111 | + } |
| 112 | + } else if (action == '4') { |
| 113 | + remove("http://www.115.com","115_lang"); |
| 114 | + results.push(changed); |
| 115 | + retrieve("http://www.115.com","115_lang");//get false |
| 116 | + |
| 117 | + //false, true |
| 118 | + setTimeout(function() { |
| 119 | + var client = require('../../nw_test_app').createClient({ |
| 120 | + argv: gui.App.argv, |
| 121 | + data: results |
| 122 | + }); |
| 123 | + }, 1000); |
| 124 | + } |
| 125 | + |
| 126 | + |
| 127 | + </script> |
| 128 | + </body> |
| 129 | +</html> |
0 commit comments