forked from JASONews/headlessBrowser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_hb.py
172 lines (133 loc) · 5.88 KB
/
test_hb.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
import pytest
from hb_pi import HeadlessBrowser
import os
import sys
from subprocess import call
class TestHeadlessbrowser:
def test_files_exist_URL(self):
"""
test if all the files (HAR file, screenshot, html and json file)
generated for a URL
"""
hb = HeadlessBrowser()
call(["rm", "-rf", "har", "htmls", "screenshots", "hb_results.json"])
URL = "www.google.com"
hb.run(url=URL)
if URL[-1] == "/":
path = URL.split('/')[-2]
else:
path = URL.split('/')[-1]
URL = URL.split('/')[0]
# assert if har file is generated
assert True == os.path.exists('./har/')
length = len([name for name in os.listdir('./har/') if name.endswith(".har") and name.startswith(URL)])
assert length == 1
# assert if html page is generated
assert True == os.path.exists('./htmls/')
length = len([name for name in os.listdir('./htmls/') if name.endswith(".html")])
assert length == 1
file_name = './htmls/' + path + '.html'
assert os.path.isfile(file_name)
assert os.stat(file_name).st_size != 0
# assert if sceenshot is generated
assert True == os.path.exists('./screenshots/')
length = len([name for name in os.listdir('./screenshots/') if name.endswith(".png")])
assert length == 1
file_name = './screenshots/' + path + '.png'
assert os.path.isfile(file_name)
assert os.stat(file_name).st_size != 0
# assert if json file is generated
assert True == os.path.exists('./hb_results.json')
assert os.stat('./hb_results.json').st_size != 0
call(["rm", "-rf", "har", "htmls", "screenshots", "hb_results.json"])
def test_files_exist_URLs_file(self):
"""
test if _get_http_request(args...) returns failure
for an invalid url.
"""
hb = HeadlessBrowser()
url_file = 'input_list.txt'
call(["rm", "-rf", "har", "htmls", "screenshots", "hb_results.json"])
hb.run(input_file=url_file)
# assert if /har folder is created
assert True == os.path.exists('./har/')
#assert if /htmls folder is created
assert True == os.path.exists('./htmls/')
# assert if /screenshots is craeted
assert True == os.path.exists('./screenshots/')
#assert if json file is generated
assert True == os.path.exists('./hb_results.json')
num_urls = sum(1 for line in open(url_file))
length = len([name for name in os.listdir('./har/')])
assert length == num_urls
length = len([name for name in os.listdir('./htmls/')])
assert length == num_urls
length = len([name for name in os.listdir('./screenshots/')])
assert length == num_urls
urls = open(url_file, "r")
tag = "1"
for URL in urls:
URL = URL.strip()
index, url = URL.split(",")[0], URL.split(",")[1]
url = url.strip()
f_name = str(index) + "-" + tag
if url[-1] == "/":
path = url.split('/')[-2]
else:
path = url.split('/')[-1]
url = url.split('/')[0]
# assert if har file is generated for URL
length = len([name for name in os.listdir('./har/') if name.endswith(".har") and name.startswith(url)])
assert length == 1
# assert if html page is generated for URL
file_name = './htmls/' + f_name + '.html'
assert os.path.isfile(file_name)
assert os.stat(file_name).st_size != 0
# assert if sceenshot is generated for URL
file_name = './screenshots/' + f_name + '.png'
assert os.path.isfile(file_name)
assert os.stat(file_name).st_size != 0
call(["rm", "-rf", "har", "htmls", "screenshots", "hb_results.json"])
urls.close()
def test_files_exist_URLs_list(self):
"""
test if _get_http_request(args...) returns failure
for an invalid url.
"""
hb = HeadlessBrowser()
url_list = ['www.google.com', 'www.facebook.com/lovee1805', 'www.matt.com']
call(["rm", "-rf", "har", "htmls", "screenshots", "hb_results.json"])
hb.run(input_list=url_list)
# assert if /har folder is created
assert True == os.path.exists('./har/')
#assert if /htmls folder is created
assert True == os.path.exists('./htmls/')
# assert if /screenshots is craeted
assert True == os.path.exists('./screenshots/')
#assert if json file is generated
assert True == os.path.exists('./hb_results.json')
num_urls = len(url_list)
length = len([name for name in os.listdir('./har/')])
assert length == num_urls
length = len([name for name in os.listdir('./htmls/')])
assert length == num_urls
length = len([name for name in os.listdir('./screenshots/')])
assert length == num_urls
for URL in url_list:
if URL[-1] == "/":
path = URL.split('/')[-2]
else:
path = URL.split('/')[-1]
url = URL.split('/')[0]
# assert if har file is generated for URL
length = len([name for name in os.listdir('./har/') if name.endswith(".har") and name.startswith(url)])
assert length == 1
# assert if html page is generated for URL
file_name = './htmls/' + path + '.html'
assert os.path.isfile(file_name)
assert os.stat(file_name).st_size != 0
# assert if sceenshot is generated for URL
file_name = './screenshots/' + path + '.png'
assert os.path.isfile(file_name)
assert os.stat(file_name).st_size != 0
call(["rm", "-rf", "har", "htmls", "screenshots", "hb_results.json"])