forked from psf/requests-html
-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathtest_requests_html.py
80 lines (52 loc) · 1.76 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
import os
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)
def test_file_get():
r = get()
assert r.status_code == 200
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')
def test_attrs():
r = get()
about = r.html.find('#about', first=True)
assert 'aria-haspopup' in about.attrs
assert len(about.attrs['class']) == 2
def test_links():
r = get()
about = r.html.find('#about', first=True)
len(about.links) == 6
len(about.absolute_links) == 6
def test_search():
r = get()
style = r.html.search('Python is a {} language')[0]
assert style == 'programming'
def test_xpath():
r = get()
html = r.html.xpath('/html', first=True)
assert 'no-js' in html.attrs['class']
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)
def test_links():
assert not("#test".startswith('#') and True) is False
assert not("test".startswith('#') and True) is True
assert not("#test".startswith('#') and False) is True
assert not("test".startswith('#') and False) is True
if __name__ == '__main__':
test_html_loading()