Newer
Older
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
"""
Check search page at /search/<int>?q URL.
MemeGenerator
Ruizhe Deng rxd@umich.edu
"""
import re
import bs4
def test_search(client):
"""Verify expected links present in / URL.
Note: 'client' is a fixture fuction that provides a Flask test server
interface with a clean database. It is implemented in conftest.py and
reused by many tests. Docs: https://docs.pytest.org/en/latest/fixture.html
"""
response = client.get("/search/1?q=umich")
assert response.status_code == 200
soup = bs4.BeautifulSoup(response.data, "html.parser")
srcs = [x.get("src") for x in soup.find_all("img")]
btns = [x.get("value") for x in soup.find_all("input")]
# check if there are 20 images in the page
assert len(srcs) == 20
# check if page has next and prev button
assert 'next' in btns
assert 'prev' in btns
def test_next_and_prev_page(client):
response = client.get("/search/1?q=umich")
assert response.status_code == 200 or response.status_code == 304
soup = bs4.BeautifulSoup(response.data, "html.parser")
links = [x.get("action") for x in soup.find_all("form")]
# check if next page in the links
assert '/search/2' in links
# go to next page
response = client.get("/search/2?q=umich")
assert response.status_code == 200
# go to prev page
response = client.get("/search/1?q=umich")
assert response.status_code == 200 or response.status_code == 304