|
| 1 | +from selenium.common.exceptions import NoSuchElementException |
| 2 | +from widgetastic.utils import ParametrizedLocator |
| 3 | +from widgetastic.widget import Table |
| 4 | +from widgetastic.widget import TextInput |
| 5 | +from widgetastic.widget import Widget |
| 6 | + |
| 7 | +from .select import Select |
| 8 | + |
| 9 | + |
| 10 | +class BaseCalendarMonth: |
| 11 | + """Represents calendar month component. |
| 12 | +
|
| 13 | + https://www.patternfly.org/v4/components/calendar-month |
| 14 | + """ |
| 15 | + |
| 16 | + CALENDAR_HEADER = ".//div[@class='pf-c-calendar-month__header']" |
| 17 | + MONTH_SELECT_LOCATOR = ( |
| 18 | + f"{CALENDAR_HEADER}//div[contains(@class, 'header-month')]" |
| 19 | + f"//div[@data-ouia-component-type='PF4/Select']" |
| 20 | + ) |
| 21 | + YEAR_INPUT_LOCATOR = f"{CALENDAR_HEADER}//div[contains(@class, 'header-year')]/input" |
| 22 | + |
| 23 | + PREV_BUTTON_LOCATOR = f"{CALENDAR_HEADER}//div[contains(@class, 'prev-month')]" |
| 24 | + NEXT_BUTTON_LOCATOR = f"{CALENDAR_HEADER}//div[contains(@class, 'next-month')]" |
| 25 | + |
| 26 | + TABLE = ".//table" |
| 27 | + SELECTED_DATE_LOCATOR = f"{TABLE}/tbody//td[contains(@class, 'pf-m-selected')]" |
| 28 | + |
| 29 | + def prev(self): |
| 30 | + return self.browser.click(self.PREV_BUTTON_LOCATOR) |
| 31 | + |
| 32 | + def next(self): |
| 33 | + return self.browser.click(self.NEXT_BUTTON_LOCATOR) |
| 34 | + |
| 35 | + @property |
| 36 | + def year(self): |
| 37 | + el = self.browser.element(self.YEAR_INPUT_LOCATOR) |
| 38 | + return el.get_attribute("value") |
| 39 | + |
| 40 | + @year.setter |
| 41 | + def year(self, value): |
| 42 | + year_input_widget = TextInput(self, locator=self.YEAR_INPUT_LOCATOR) |
| 43 | + year_input_widget.fill(value) |
| 44 | + |
| 45 | + @property |
| 46 | + def month(self): |
| 47 | + el = self.browser.element(self.MONTH_SELECT_LOCATOR) |
| 48 | + return self.browser.text(el) |
| 49 | + |
| 50 | + @month.setter |
| 51 | + def month(self, value): |
| 52 | + month_select_widget = Select(self, locator=self.MONTH_SELECT_LOCATOR) |
| 53 | + month_select_widget.item_select(value) |
| 54 | + |
| 55 | + @property |
| 56 | + def day(self): |
| 57 | + try: |
| 58 | + el = self.browser.element(self.SELECTED_DATE_LOCATOR) |
| 59 | + except NoSuchElementException: |
| 60 | + return "" |
| 61 | + return self.browser.text(el) |
| 62 | + |
| 63 | + @day.setter |
| 64 | + def day(self, value): |
| 65 | + table_widget = Table(self, locator=self.TABLE) |
| 66 | + for row in table_widget: |
| 67 | + for _, col in row: |
| 68 | + if col.text == value: |
| 69 | + col.click() |
| 70 | + return |
| 71 | + |
| 72 | + def read(self): |
| 73 | + """Returns the currently selected date in format DD MONTH YYYY.""" |
| 74 | + return f"{self.day} {self.month} {self.year}" |
| 75 | + |
| 76 | + def fill(self, items): |
| 77 | + """Fills a Calendar with all items. |
| 78 | + Example dictionary: {'day': '22', 'month': 'November', 'year': '2023'}" |
| 79 | +
|
| 80 | + Args: |
| 81 | + items: A dictionary containing what items to select (True) or deselect (False) |
| 82 | + """ |
| 83 | + if type(items) is not dict: |
| 84 | + raise TypeError("Fill value has to be dictionary type. ") |
| 85 | + if "day" in items: |
| 86 | + self.day = items["day"] |
| 87 | + if "month" in items: |
| 88 | + self.month = items["month"] |
| 89 | + if "year" in items: |
| 90 | + self.year = items["year"] |
| 91 | + |
| 92 | + |
| 93 | +class CalendarMonth(BaseCalendarMonth, Widget): |
| 94 | + ROOT = ParametrizedLocator("{@locator}") |
| 95 | + |
| 96 | + def __init__(self, parent, locator, logger=None): |
| 97 | + super().__init__(parent, logger=logger) |
| 98 | + self.locator = locator |
0 commit comments