navigation.js
5.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*eslint strict:0*/
var server = require('webserver').create();
var service = server.listen(8092, function(request, response) {
var path = request.url.split('/');
response.statusCode = 200;
switch (path[1]){
case "indexscript":
response.setHeader('Content-type', 'text/html');
response.write('<a href="/link">a link</a>');
response.write('<form action="/form" method="POST"><input type="submit" /></form>');
response.write('<script src="/script"></script>');
response.close();
break;
case "indexscript2":
response.setHeader('Content-type', 'text/html');
response.write('<a href="/link">a link</a>');
response.write('<form action="/form" method="POST"><input type="submit" /></form>');
response.write('<script src="/script2"></script>');
response.close();
break;
case "form": case "link": case "":
response.setHeader('Content-type', 'text/html');
response.write('<a href="/link">a link</a>');
response.write('<form action="/form" method="POST"><input type="submit" /></form>');
response.close();
break;
case "script": //never close connexion
break;
case "script2": //partial response never close connexion
response.setHeader('Content-type', 'text/javascript');
response.write('var a=2;');
break;
case "longScript":
response.setHeader('Content-type', 'text/html');
response.write('<html><body><script>for(;;);</script></body></html>');
response.close();
break;
}
});
var resourceTimeout = function resourceTimeout (request) {
casper.test.pass('resource.timeout matched');
};
var stopScript = function stopScript (webpage, message) {
webpage.stopJavaScript();
casper.test.pass('remote.longRunningScript matched ' + message);
return true;
};
var closeService = function closeService(message) {
casper.test.begin(message, 0, function(test) {
casper.start('http://localhost:8092/').run(function() {
test.done();
server.close();
});
});
};
casper.test.begin('Link Navigation updates response', 2, function(test) {
casper.start('http://localhost:8092/', function(response) {
casper.click('a');
casper.then(function(response) {
test.assertUrlMatch(
/\/link$/,
'URL matches anchor href'
);
test.assertEquals(
response.url,
casper.page.url,
'response is consistent with the internal page'
);
});
}).run(function() {
test.done();
});
});
casper.test.begin('Form Submittal updates the response', 2, function(test) {
casper.start('http://localhost:8092/', function(response) {
casper.fill('form', {}, true);
casper.then(function(response) {
test.assertUrlMatch(
/\/form$/,
'URL matches form action'
);
test.assertEquals(
response.url,
casper.page.url,
'response is consistent with the internal page'
);
});
}).run(function() {
test.done();
});
});
if (phantom.casperEngine === 'slimerjs' && utils.ltVersion(slimer.version, '0.10.0')){
closeService('No resourceTimeout and longRunningScript functionality');
} else {
casper.test.begin('Catch resourceTimeout on partial response', 2, function(test) {
casper.on("resource.timeout",resourceTimeout);
casper.page.settings.resourceTimeout = 1000;
casper.start('http://localhost:8092/indexscript2', function(response) {
delete casper.page.settings.resourceTimeout;
test.pass('unable to load page on time');
casper.removeListener("resource.timeout", resourceTimeout);
}).run(function() {
test.done();
});
});
casper.test.begin('Catch resourceTimeout on No response', 2, function(test) {
casper.on("resource.timeout",resourceTimeout);
casper.page.settings.resourceTimeout = 1000;
casper.start('http://localhost:8092/indexscript', function(response) {
delete casper.page.settings.resourceTimeout;
test.pass('unable to load page on time');
casper.removeListener("resource.timeout", resourceTimeout);
}).run(function() {
test.done();
});
});
if (phantom.casperEngine === 'phantomjs') {
closeService('No longRunningScript functionality');
} else {
casper.test.begin('Catch longRunningScript', 2, function(test) {
casper.on("remote.longRunningScript", stopScript);
casper.start('http://localhost:8092/longScript', function(response) {
test.pass('unable to load page on time because of script');
casper.removeListener("remote.longRunningScript", stopScript);
}).run(function() {
test.done();
server.close();
});
});
}
}