Skip to content

Commit 19a6b32

Browse files
committed
добавлено поведенческое тестирование
1 parent 3d18e6f commit 19a6b32

7 files changed

Lines changed: 341 additions & 6 deletions

File tree

fixprice_api/manager.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
from typing import Any, Literal
2-
import os
32
from dataclasses import dataclass, field
43
from collections import defaultdict
54
from human_requests import HumanBrowser, HumanContext, HumanPage
@@ -9,8 +8,7 @@
98
WaitHeader,
109
WaitSource,
1110
)
12-
from camoufox import AsyncCamoufox
13-
from playwright.async_api import TimeoutError as PWTimeoutError
11+
from camoufox import AsyncCamoufox, DefaultAddons
1412

1513
from human_requests import ApiParent, api_child_field
1614
from .endpoints.catalog import ClassCatalog
@@ -27,6 +25,8 @@ class FixPriceAPI(ApiParent):
2725
"""Время ожидания ответа от сервера в миллисекундах."""
2826
headless: bool = True
2927
"""Запускать браузер в headless режиме?"""
28+
test_mode: bool = False
29+
"""Режим тестирования предполагает более глубокий _warmup который не требуется для обычного использования"""
3030
proxy: str | dict | Proxy | None = field(default_factory=Proxy.from_env)
3131
"""Прокси-сервер для всех запросов (если нужен). По умолчанию берет из окружения (если есть).
3232
Принимает как формат Playwright, так и строчный формат."""
@@ -48,6 +48,8 @@ class FixPriceAPI(ApiParent):
4848

4949
unstandard_headers: dict[str, str] = field(init=False, repr=False)
5050
"""Список нестандартных заголовков пойманных при инициализации"""
51+
unstandard_urls: dict[str, list[str]] = field(init=False, repr=False)
52+
"""Список нестандартных заголовков пойманных при инициализации"""
5153

5254
Geolocation: ClassGeolocation = api_child_field(ClassGeolocation)
5355
"""API для работы с геолокацией."""
@@ -70,8 +72,11 @@ async def _warmup(self) -> None:
7072
br = await AsyncCamoufox(
7173
headless=self.headless,
7274
proxy=px.as_dict(),
75+
humanize=True,
7376
**self.browser_opts,
7477
block_images=True,
78+
i_know_what_im_doing=True,
79+
exclude_addons=[DefaultAddons.UBO]
7580
).start()
7681

7782
self.session = HumanBrowser.replace(br)
@@ -97,6 +102,17 @@ async def _warmup(self) -> None:
97102
timeout_ms=self.timeout_ms,
98103
)
99104

105+
if self.test_mode:
106+
btn = self.page.locator("div.selected-city > div.buttons > button.button.normal").first
107+
await btn.wait_for(state="visible", timeout=self.timeout_ms)
108+
await btn.click(timeout=self.timeout_ms)
109+
110+
await self.page.locator("a.link.product-category").first.click()
111+
await self.page.wait_for_selector(
112+
selector="div.page-content", timeout=self.timeout_ms, state="visible"
113+
)
114+
await self.page.wait_for_load_state("load")
115+
100116
await self.page.goto(
101117
self.CATALOG_URL, wait_until="networkidle"
102118
) # ускорение сети, таким образом пропускаем OPTION pre-fetch
@@ -116,6 +132,7 @@ async def _warmup(self) -> None:
116132

117133
# Преобразуем set обратно в list
118134
self.unstandard_headers = {k: list(v)[0] for k, v in result.items()}
135+
self.unstandard_urls = result_sniffer["request"]
119136

120137
async def __aexit__(self, *exc):
121138
"""Выход из контекстного менеджера с закрытием сессии."""

tests/__snapshots__/ProductService.info.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@
132132
"title": "Вариант"
133133
}
134134
],
135-
"count": 13,
135+
"count": 8,
136136
"price": "59.00",
137137
"dimensions": {
138138
"width": 60,
@@ -179,7 +179,8 @@
179179
"access-control-allow-credentials": "true",
180180
"access-control-max-age": "3600",
181181
"server": "Unit/1.34.2",
182-
"date": "Sat, 14 Mar 2026 22:32:33 GMT",
182+
"date": "Thu, 19 Mar 2026 07:31:18 GMT",
183+
"connection": "close",
183184
"transfer-encoding": "chunked"
184185
}
185186
}

tests/__snapshots__/ProductService.info.schema.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@
7777
"cache-control": {
7878
"type": "string"
7979
},
80+
"connection": {
81+
"type": "string"
82+
},
8083
"content-type": {
8184
"type": "string"
8285
},
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
{
2+
"https://api.fix-price.com/buyer/v1/config/ecom.product.special.price.date.show": {
3+
"x-language": [
4+
"ru"
5+
],
6+
"x-city": [
7+
"3"
8+
],
9+
"x-key": [
10+
"L3d3S25BdzhGam9JejFiNEQvOWJUQT09OjprTkM1TmpoOHk0OWtveVhJUlBwTEVBPT0=:8dbc25f87a4aba4652e55cbf64d136f0"
11+
],
12+
"x-logged": [
13+
""
14+
],
15+
"x-client-route": [
16+
"/catalog"
17+
]
18+
},
19+
"https://api.fix-price.com/buyer/v1/banner/suitable?type=1&path=https:%2F%2Ffix-price.com%2Fcatalog": {
20+
"x-language": [
21+
"ru"
22+
],
23+
"x-city": [
24+
"3"
25+
],
26+
"x-key": [
27+
"L3d3S25BdzhGam9JejFiNEQvOWJUQT09OjprTkM1TmpoOHk0OWtveVhJUlBwTEVBPT0=:8dbc25f87a4aba4652e55cbf64d136f0"
28+
],
29+
"x-logged": [
30+
""
31+
],
32+
"x-client-route": [
33+
"/catalog"
34+
]
35+
},
36+
"https://api.fix-price.com/buyer/v2/geo/ip": {
37+
"x-language": [
38+
"ru"
39+
],
40+
"x-city": [
41+
"3"
42+
],
43+
"x-key": [
44+
"L3d3S25BdzhGam9JejFiNEQvOWJUQT09OjprTkM1TmpoOHk0OWtveVhJUlBwTEVBPT0=:8dbc25f87a4aba4652e55cbf64d136f0"
45+
],
46+
"x-logged": [
47+
""
48+
],
49+
"x-client-route": [
50+
"/catalog"
51+
]
52+
},
53+
"https://api.fix-price.com/buyer/v1/cookie-config": {
54+
"x-language": [
55+
"ru"
56+
],
57+
"x-city": [
58+
"3"
59+
],
60+
"x-key": [
61+
"L3d3S25BdzhGam9JejFiNEQvOWJUQT09OjprTkM1TmpoOHk0OWtveVhJUlBwTEVBPT0=:8dbc25f87a4aba4652e55cbf64d136f0"
62+
],
63+
"x-logged": [
64+
""
65+
],
66+
"x-client-route": [
67+
"/catalog"
68+
]
69+
},
70+
"https://api.fix-price.com/buyer/v2/geo/ip?utm_referrer=https%3a%2f%2ffix-price.com%2f": {
71+
"x-language": [
72+
"ru"
73+
],
74+
"x-city": [
75+
"3"
76+
],
77+
"x-key": [
78+
"L3d3S25BdzhGam9JejFiNEQvOWJUQT09OjprTkM1TmpoOHk0OWtveVhJUlBwTEVBPT0=:8dbc25f87a4aba4652e55cbf64d136f0"
79+
],
80+
"x-logged": [
81+
""
82+
],
83+
"x-client-route": [
84+
"/catalog"
85+
]
86+
}
87+
}

0 commit comments

Comments
 (0)