forked from jeanphix/Ghost.py
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.py
executable file
·334 lines (281 loc) · 12.8 KB
/
run.py
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import unittest
import logging
from ghost import GhostTestCase, Ghost
from app import app
PORT = 5000
base_url = 'http://localhost:%s/' % PORT
class GhostTest(GhostTestCase):
port = PORT
display = False
log_level = logging.INFO
@classmethod
def create_app(cls):
return app
def test_open(self):
page, resources = self.ghost.open(base_url)
self.assertEqual(page.url, base_url)
self.assertTrue("Test page" in self.ghost.content)
def test_page_with_no_cache_headers(self):
page, resources = self.ghost.open("%sno-cache" % base_url)
self.assertIsNotNone(page.content)
self.assertIn("cache for me", page.content)
def test_http_status(self):
page, resources = self.ghost.open("%sprotected" % base_url)
self.assertEqual(resources[0].http_status, 403)
page, resources = self.ghost.open("%s404" % base_url)
self.assertEqual(page.http_status, 404)
def test_evaluate(self):
self.ghost.open(base_url)
self.assertEqual(self.ghost.evaluate("x='ghost'; x;")[0], 'ghost')
def test_external_api(self):
page, resources = self.ghost.open("%smootools" % base_url)
self.assertEqual(len(resources), 2)
self.assertEqual(type(self.ghost.evaluate("document.id('list')")[0]),
dict)
def test_extra_resource_content(self):
page, resources = self.ghost.open("%smootools" % base_url)
self.assertIn('MooTools: the javascript framework',
resources[1].content)
def test_extra_resource_binaries(self):
page, resources = self.ghost.open("%simage" % base_url)
self.assertEqual(resources[1].content.__class__.__name__,
'QByteArray')
def test_wait_for_selector(self):
page, resources = self.ghost.open("%smootools" % base_url)
success, resources = self.ghost.click("#button")
success, resources = self.ghost\
.wait_for_selector("#list li:nth-child(2)")
self.assertEqual(resources[0].url, "%sitems.json" % base_url)
def test_settimeout(self):
page, resources = self.ghost.open("%ssettimeout" % base_url)
result, _ = self.ghost.evaluate("document.getElementById('result').innerHTML")
self.assertEqual(result, 'Bad')
self.ghost.sleep(4)
result, _ = self.ghost.evaluate("document.getElementById('result').innerHTML")
self.assertEqual(result, 'Good')
def test_wait_for_text(self):
page, resources = self.ghost.open("%smootools" % base_url)
self.ghost.click("#button")
success, resources = self.ghost.wait_for_text("second item")
self.assertEqual(resources[0].url, "%sitems.json" % base_url)
def test_wait_for_timeout(self):
self.ghost.open("%s" % base_url)
self.assertRaises(Exception, self.ghost.wait_for_text, "undefined")
def test_fill(self):
self.ghost.open("%sform" % base_url)
values = {
'text': 'Here is a sample text.',
'email': '[email protected]',
'textarea': 'Here is a sample text.\nWith several lines.',
'checkbox': True,
'selectbox': 'two',
"radio": "first choice"
}
self.ghost.fill('#contact-form', values)
for field in ['text', 'email', 'textarea', 'selectbox']:
value, resssources = self.ghost\
.evaluate('document.getElementById("%s").value' % field)
self.assertEqual(value, values[field])
value, resources = self.ghost.evaluate(
'document.getElementById("checkbox").checked')
self.assertEqual(value, True)
value, resources = self.ghost.evaluate(
'document.getElementById("radio-first").checked')
self.assertEqual(value, True)
value, resources = self.ghost.evaluate(
'document.getElementById("radio-second").checked')
self.assertEqual(value, False)
def test_fill_checkbox(self):
self.ghost.open("%sform" % base_url)
def test_form_submission(self):
self.ghost.open("%sform" % base_url)
values = {
'text': 'Here is a sample text.',
}
self.ghost.fill('#contact-form', values)
page, resources = self.ghost.fire_on('#contact-form', 'submit',
expect_loading=True)
self.assertIn('form successfully posted', self.ghost.content)
def test_global_exists(self):
self.ghost.open("%s" % base_url)
self.assertTrue(self.ghost.global_exists('myGlobal'))
def test_resource_headers(self):
page, resources = self.ghost.open(base_url)
self.assertEqual(page.headers['Content-Type'], 'text/html; charset=utf-8')
def test_click_link(self):
page, resources = self.ghost.open("%s" % base_url)
page, resources = self.ghost.click('a', expect_loading=True)
self.assertEqual(page.url, "%sform" % base_url)
def test_cookies(self):
self.ghost.open("%scookie" % base_url)
self.assertEqual(len(self.ghost.cookies), 1)
def test_delete_cookies(self):
self.ghost.open("%scookie" % base_url)
self.ghost.delete_cookies()
self.assertEqual(len(self.ghost.cookies), 0)
def test_save_load_cookies(self):
self.ghost.delete_cookies()
self.ghost.open("%sset/cookie" % base_url)
self.ghost.save_cookies('testcookie.txt')
self.ghost.delete_cookies()
self.ghost.load_cookies('testcookie.txt')
self.ghost.open("%sget/cookie" % base_url)
self.assertTrue( 'OK' in self.ghost.content )
def test_wait_for_alert(self):
self.ghost.open("%salert" % base_url)
self.ghost.click('#alert-button')
msg, resources = self.ghost.wait_for_alert()
self.assertEqual(msg, 'this is an alert')
def test_confirm(self):
self.ghost.open("%salert" % base_url)
with Ghost.confirm():
self.ghost.click('#confirm-button')
msg, resources = self.ghost.wait_for_alert()
self.assertEqual(msg, 'you confirmed!')
def test_no_confirm(self):
self.ghost.open("%salert" % base_url)
with Ghost.confirm(False):
self.ghost.click('#confirm-button')
msg, resources = self.ghost.wait_for_alert()
self.assertEqual(msg, 'you denied!')
def test_confirm_callback(self):
self.ghost.open("%salert" % base_url)
with Ghost.confirm(callback=lambda: False):
self.ghost.click('#confirm-button')
msg, resources = self.ghost.wait_for_alert()
self.assertEqual(msg, 'you denied!')
def test_prompt(self):
self.ghost.open("%salert" % base_url)
with Ghost.prompt('my value'):
self.ghost.click('#prompt-button')
value, resources = self.ghost.evaluate('promptValue')
self.assertEqual(value, 'my value')
def test_prompt_callback(self):
self.ghost.open("%salert" % base_url)
with Ghost.prompt(callback=lambda: 'another value'):
self.ghost.click('#prompt-button')
value, resources = self.ghost.evaluate('promptValue')
self.assertEqual(value, 'another value')
def test_popup_messages_collection(self):
self.ghost.open("%salert" % base_url, default_popup_response=True)
self.ghost.click('#confirm-button')
self.assertIn('this is a confirm', self.ghost.popup_messages)
self.ghost.click('#prompt-button')
self.assertIn('Prompt ?', self.ghost.popup_messages)
self.ghost.click('#alert-button')
self.assertIn('this is an alert', self.ghost.popup_messages)
def test_prompt_default_value_true(self):
self.ghost.open("%salert" % base_url, default_popup_response=True)
self.ghost.click('#confirm-button')
msg, resources = self.ghost.wait_for_alert()
self.assertEqual(msg, 'you confirmed!')
def test_prompt_default_value_false(self):
self.ghost.open("%salert" % base_url, default_popup_response=False)
self.ghost.click('#confirm-button')
msg, resources = self.ghost.wait_for_alert()
self.assertEqual(msg, 'you denied!')
def test_capture_to(self):
self.ghost.open(base_url)
self.ghost.capture_to('test.png')
self.assertTrue(os.path.isfile('test.png'))
os.remove('test.png')
def test_region_for_selector(self):
self.ghost.open(base_url)
x1, y1, x2, y2 = self.ghost.region_for_selector('h1')
self.assertEqual(x1, 8)
self.assertEqual(y1, 21)
self.assertEqual(x2, 791)
def test_capture_selector_to(self):
self.ghost.open(base_url)
self.ghost.capture_to('test.png', selector='h1')
self.assertTrue(os.path.isfile('test.png'))
os.remove('test.png')
def test_set_field_value_checkbox_true(self):
self.ghost.open("%sform" % base_url)
self.ghost.set_field_value('[name=checkbox]', True)
value, resssources = self.ghost.evaluate(
'document.getElementById("checkbox").checked')
self.assertEqual(value, True)
def test_set_field_value_checkbox_false(self):
self.ghost.open("%sform" % base_url)
self.ghost.set_field_value('[name=checkbox]', False)
value, resssources = self.ghost.evaluate(
'document.getElementById("checkbox").checked')
self.assertEqual(value, False)
def test_set_field_value_checkbox_multiple(self):
self.ghost.open("%sform" % base_url)
self.ghost.set_field_value('[name=multiple-checkbox]',
'second choice')
value, resources = self.ghost.evaluate(
'document.getElementById("multiple-checkbox-first").checked')
self.assertEqual(value, False)
value, resources = self.ghost.evaluate(
'document.getElementById("multiple-checkbox-second").checked')
self.assertEqual(value, True)
def test_set_field_value_email(self):
expected = '[email protected]'
self.ghost.open("%sform" % base_url)
self.ghost.set_field_value('[name=email]', expected)
value, resssources = self.ghost\
.evaluate('document.getElementById("email").value')
self.assertEqual(value, expected)
def test_set_field_value_text(self):
expected = 'sample text'
self.ghost.open("%sform" % base_url)
self.ghost.set_field_value('[name=text]', expected)
value, resssources = self.ghost\
.evaluate('document.getElementById("text").value')
self.assertEqual(value, expected)
def test_set_field_value_radio(self):
self.ghost.open("%sform" % base_url)
self.ghost.set_field_value('[name=radio]',
'first choice')
value, resources = self.ghost.evaluate(
'document.getElementById("radio-first").checked')
self.assertEqual(value, True)
value, resources = self.ghost.evaluate(
'document.getElementById("radio-second").checked')
self.assertEqual(value, False)
def test_set_field_value_textarea(self):
expected = 'sample text\nanother line'
self.ghost.open("%sform" % base_url)
self.ghost.set_field_value('[name=textarea]', expected)
value, resssources = self.ghost\
.evaluate('document.getElementById("textarea").value')
self.assertEqual(value, expected)
def test_set_simple_file_field(self):
self.ghost.open("%supload" % base_url)
self.ghost.set_field_value('[name=simple-file]',
os.path.join(os.path.dirname(__file__), 'static', 'blackhat.jpg'))
page, resources = self.ghost.fire_on('form', 'submit',
expect_loading=True)
file_path = os.path.join(
os.path.dirname(__file__), 'uploaded_blackhat.jpg')
self.assertTrue(os.path.isfile(file_path))
os.remove(file_path)
def test_basic_http_auth_success(self):
page, resources = self.ghost.open("%sbasic-auth" % base_url,
auth=('admin', 'secret'))
self.assertEqual(page.http_status, 200)
def test_basic_http_auth_error(self):
page, resources = self.ghost.open("%sbasic-auth" % base_url,
auth=('admin', 'wrongsecret'))
self.assertEqual(page.http_status, 401)
def test_unsupported_content(self):
page, resources = self.ghost.open("%ssend-file" % base_url)
foo = open(os.path.join(os.path.dirname(__file__), 'static',
'foo.tar.gz'), 'r').read(1024)
self.assertEqual(resources[0].content, foo)
def test_url_with_hash(self):
page, resources = self.ghost.open("%surl-hash" % base_url)
self.assertIsNotNone(page)
self.assertTrue("Test page" in self.ghost.content)
def test_url_with_hash_header(self):
page, resources = self.ghost.open("%surl-hash-header" % base_url)
self.assertIsNotNone(page)
self.assertTrue("Welcome" in self.ghost.content)
if __name__ == '__main__':
unittest.main()