forked from psf/requests-html
-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathtest_requests_html.py
169 lines (123 loc) · 3.59 KB
/
test_requests_html.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
import os
import pytest
from requests_html import HTMLSession, HTML
from requests_file import FileAdapter
session = HTMLSession()
session.mount('file://', FileAdapter())
def get():
path = os.path.sep.join((os.path.dirname(os.path.abspath(__file__)), 'python.html'))
url = 'file://{}'.format(path)
return session.get(url)
@pytest.mark.ok
def test_file_get():
r = get()
assert r.status_code == 200
@pytest.mark.ok
def test_class_seperation():
r = get()
about = r.html.find('#about', first=True)
assert len(about.attrs['class']) == 2
@pytest.mark.ok
def test_css_selector():
r = get()
about = r.html.find('#about', first=True)
for menu_item in (
'About', 'Applications', 'Quotes', 'Getting Started', 'Help',
'Python Brochure'
):
assert menu_item in about.text.split('\n')
assert menu_item in about.full_text.split('\n')
@pytest.mark.ok
def test_containing():
r = get()
python = r.html.find(containing='python')
assert len(python) == 191
@pytest.mark.ok
def test_attrs():
r = get()
about = r.html.find('#about', first=True)
assert 'aria-haspopup' in about.attrs
assert len(about.attrs['class']) == 2
@pytest.mark.ok
def test_links():
r = get()
about = r.html.find('#about', first=True)
assert len(about.links) == 6
assert len(about.absolute_links) == 6
@pytest.mark.ok
def test_search():
r = get()
style = r.html.search('Python is a {} language')[0]
assert style == 'programming'
@pytest.mark.ok
def test_xpath():
r = get()
html = r.html.xpath('/html', first=True)
assert 'no-js' in html.attrs['class']
a_hrefs = r.html.xpath('//a/@href')
assert '#site-map' in a_hrefs
@pytest.mark.ok
def test_html_loading():
doc = """<a href='https://httpbin.org'>"""
html = HTML(html=doc)
assert 'https://httpbin.org' in html.links
assert isinstance(html.raw_html, bytes)
assert isinstance(html.html, str)
@pytest.mark.ok
def test_anchor_links():
r = get()
r.html.skip_anchors = False
assert '#site-map' in r.html.links
@pytest.mark.render
def test_render():
r = get()
script = """
() => {
return {
width: document.documentElement.clientWidth,
height: document.documentElement.clientHeight,
deviceScaleFactor: window.devicePixelRatio,
}
}
"""
val = r.html.render(script=script)
for value in ('width', 'height', 'deviceScaleFactor'):
assert value in val
about = r.html.find('#about', first=True)
assert len(about.links) == 6
@pytest.mark.render
def test_bare_render():
doc = """<a href='https://httpbin.org'>"""
html = HTML(html=doc)
script = """
() => {
return {
width: document.documentElement.clientWidth,
height: document.documentElement.clientHeight,
deviceScaleFactor: window.devicePixelRatio,
}
}
"""
val = html.render(script=script, reload=False)
for value in ('width', 'height', 'deviceScaleFactor'):
assert value in val
assert html.find('html')
assert 'https://httpbin.org' in html.links
@pytest.mark.render
def test_bare_js_eval():
doc = """
<!DOCTYPE html>
<html>
<body>
<div id="replace">This gets replaced</div>
<script type="text/javascript">
document.getElementById("replace").innerHTML = "yolo";
</script>
</body>
</html>
"""
html = HTML(html=doc)
html.render()
assert html.find('#replace', first=True).text == 'yolo'
if __name__ == '__main__':
test_containing()