79 lines
3.2 KiB
Python
79 lines
3.2 KiB
Python
"""Модуль interactive_dropdown_list_component содержит класс для работы с интерактивными выпадающими списками,
|
||
позволяющими сделать выбор нескольких элементов.
|
||
|
||
Класс InteractiveDropdownList наследует базовый функционал BaseComponent и добавляет
|
||
методы для взаимодействия с интерактивными выпадающими списками на странице.
|
||
"""
|
||
|
||
from playwright.sync_api import Page, Locator, expect
|
||
from tools.logger import get_logger
|
||
from components.base_component import BaseComponent
|
||
|
||
logger = get_logger("INTERACTIVE_DROPDOWN_LIST")
|
||
|
||
class InteractiveDropdownList(BaseComponent):
|
||
"""Класс для работы с выпадающими списками.
|
||
|
||
Наследует функциональность BaseElement и добавляет специфичные
|
||
методы для выбора и проверки элементов списка.
|
||
"""
|
||
|
||
def __init__(self, page: Page) -> None:
|
||
"""Инициализирует компонент интерактивного выпадающего списка.
|
||
|
||
Args:
|
||
page: Экземпляр страницы Playwright.
|
||
"""
|
||
|
||
super().__init__(page)
|
||
|
||
# Действия:
|
||
def get_checkbox_locator(self, text: str) -> Locator:
|
||
"""Возвращает локатор чек-бокса для элемента списка с указанным текстом.
|
||
|
||
Args:
|
||
text (str): Текст элемента для выбора.
|
||
"""
|
||
|
||
checkbox_locator = self.get_locator('div.v-list__tile__title').get_by_text(text). \
|
||
locator("../..").locator("//input[@role='checkbox']")
|
||
expect(checkbox_locator).to_be_visible(), \
|
||
f"Checkbox for dropdown list item with text {text} is missing"
|
||
return checkbox_locator
|
||
|
||
def deselect_item_with_text(self, text: str) -> None:
|
||
"""Выбирает элемент списка по указанному тексту.
|
||
|
||
Args:
|
||
text (str): Текст элемента для выбора.
|
||
"""
|
||
|
||
self.get_checkbox_locator(text).uncheck(force=True)
|
||
|
||
def select_item_with_text(self, text: str) -> None:
|
||
"""Выбирает элемент списка по указанному тексту.
|
||
|
||
Args:
|
||
text (str): Текст элемента для выбора.
|
||
"""
|
||
self.get_checkbox_locator(text).check(force=True)
|
||
|
||
def get_selected_items(self, locator: str|Locator) -> list[str]:
|
||
"""Возвращает список отмеченных элементов."""
|
||
|
||
selected_items = []
|
||
|
||
list_locator = self.get_locator(locator)
|
||
|
||
items = list_locator.get_by_role("listitem").all()
|
||
|
||
for item in items:
|
||
if item.get_by_role("checkbox").is_checked():
|
||
item_text = item.text_content().strip()
|
||
if item_text:
|
||
selected_items.append(item_text)
|
||
|
||
return selected_items
|
||
|
||
# Проверки:
|