|
7 | 7 |
|
8 | 8 | import pytest |
9 | 9 | from scrapy import Spider, Request |
10 | | -from scrapy.http.response.html import HtmlResponse |
11 | 10 |
|
12 | 11 | from playwright.async_api import Page |
13 | 12 | from scrapy_playwright.page import PageMethod |
@@ -80,53 +79,31 @@ async def test_mixed(self): |
80 | 79 |
|
81 | 80 | @allow_windows |
82 | 81 | async def test_page_method_navigation(self): |
| 82 | + """A PageMethod that navigates to a different page must produce a response |
| 83 | + whose headers and status match the final page's body, not the initial one. |
| 84 | + """ |
83 | 85 | async with make_handler({"PLAYWRIGHT_BROWSER_TYPE": self.browser_type}) as handler: |
84 | 86 | req = Request( |
85 | 87 | url=self.static_server.urljoin("/index.html"), |
86 | 88 | meta={ |
87 | 89 | "playwright": True, |
88 | | - "playwright_page_methods": [PageMethod("click", "a.lorem_ipsum")], |
| 90 | + "playwright_page_methods": [PageMethod("click", "a.json")], |
89 | 91 | }, |
90 | 92 | ) |
91 | 93 | resp = await handler._download_request(req, Spider("foo")) |
92 | 94 |
|
93 | | - assert isinstance(resp, HtmlResponse) |
94 | | - assert resp.request is req |
95 | | - assert resp.url == self.static_server.urljoin("/lorem_ipsum.html") |
96 | | - assert resp.status == 200 |
97 | | - assert "playwright" in resp.flags |
98 | | - assert resp.css("title::text").get() == "Lorem Ipsum" |
99 | | - text = resp.css("p::text").get() |
100 | | - assert text == "Lorem ipsum dolor sit amet, consectetur adipiscing elit." |
101 | | - |
102 | | - @allow_windows |
103 | | - async def test_page_method_navigation_headers_match_body(self): |
104 | | - """A PageMethod that navigates to a different page must produce a response |
105 | | - whose headers and status match the final page's body, not the initial one. |
106 | | - """ |
107 | | - async with make_handler({"PLAYWRIGHT_BROWSER_TYPE": self.browser_type}) as handler: |
108 | | - with StaticMockServer() as server: |
109 | | - req = Request( |
110 | | - url=server.urljoin("/index.html"), |
111 | | - meta={ |
112 | | - "playwright": True, |
113 | | - "playwright_page_methods": [PageMethod("click", "a.json")], |
114 | | - }, |
115 | | - ) |
116 | | - resp = await handler._download_request(req, Spider("foo")) |
117 | | - |
118 | | - assert resp.request is req |
119 | | - assert resp.url == server.urljoin("/data/quotes1.json") |
120 | | - assert resp.status == 200 |
121 | | - assert "playwright" in resp.flags |
122 | | - # headers must match the final (JSON) page, not the initial HTML page |
123 | | - assert resp.headers.get("Content-Type", b"").startswith(b"application/json") |
124 | | - # parse body and verify it's JSON, not HTML |
125 | | - body = json.loads(resp.css("pre::text").get()) |
126 | | - assert isinstance(body, dict) |
127 | | - assert isinstance(body.get("quotes"), list) |
128 | | - assert body.get("has_next") is True |
129 | | - assert body.get("page") == 1 |
| 95 | + assert resp.request is req |
| 96 | + assert resp.url == self.static_server.urljoin("/data/quotes1.json") |
| 97 | + assert resp.status == 200 |
| 98 | + assert "playwright" in resp.flags |
| 99 | + # headers must match the final (JSON) page, not the initial HTML page |
| 100 | + assert resp.headers.get("Content-Type", b"").startswith(b"application/json") |
| 101 | + # parse body and verify it's JSON, not HTML |
| 102 | + body = json.loads(resp.css("pre::text").get()) |
| 103 | + assert isinstance(body, dict) |
| 104 | + assert isinstance(body.get("quotes"), list) |
| 105 | + assert body.get("has_next") is True |
| 106 | + assert body.get("page") == 1 |
130 | 107 |
|
131 | 108 | @allow_windows |
132 | 109 | async def test_page_method_infinite_scroll(self): |
|
0 commit comments