|
| 1 | +# |
| 2 | +# Copyright 2024 Splunk Inc. |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | +# |
| 16 | +from selenium.webdriver.common.by import By |
| 17 | + |
| 18 | +from ..base_component import Selector |
| 19 | +from .base_control import BaseControl |
| 20 | +from .button import Button |
| 21 | +from .checkbox import Checkbox |
| 22 | +from .textbox import TextBox |
| 23 | + |
| 24 | +class CheckboxGroup(BaseControl): |
| 25 | + """ |
| 26 | + Entity_Component : CheckboxGroup |
| 27 | +
|
| 28 | + This class represents a group of checkboxes, allowing for expanding groups, |
| 29 | + selecting/deselecting checkboxes, and setting or retrieving values. |
| 30 | + """ |
| 31 | + |
| 32 | + def __init__(self, browser, container) -> None: |
| 33 | + """ |
| 34 | + Initializes the CheckboxGroup. |
| 35 | +
|
| 36 | + Args: |
| 37 | + browser (Browser): The browser instance to interact with. |
| 38 | + container (Selector): The container element that holds the checkbox group. |
| 39 | + """ |
| 40 | + super().__init__(browser, container) |
| 41 | + |
| 42 | + def is_group_expanded(self, group_name: str) -> bool: |
| 43 | + """ |
| 44 | + Checks if the specified group is expanded. |
| 45 | +
|
| 46 | + Args: |
| 47 | + group_name (str): The name of the checkbox group to check. |
| 48 | +
|
| 49 | + Returns: |
| 50 | + bool: True if the group is expanded, False otherwise. |
| 51 | + """ |
| 52 | + self.elements.update({ |
| 53 | + f"{group_name}_button": Selector( |
| 54 | + by=By.XPATH, |
| 55 | + select=self.elements.get("container").select + f'//span[text()="{group_name}"]/ancestor::button' |
| 56 | + ) |
| 57 | + }) |
| 58 | + return getattr(self, f"{group_name}_button").get_attribute("aria-expanded") == "true" |
| 59 | + |
| 60 | + def select_checkbox_and_set_value(self, checkbox_name: str, checkbox_value: str) -> None: |
| 61 | + """ |
| 62 | + Selects a checkbox and sets a value for the checkbox. |
| 63 | +
|
| 64 | + Args: |
| 65 | + checkbox_name (str): The name of the checkbox to select. |
| 66 | + checkbox_value (str): The value to set for the checkbox. |
| 67 | + """ |
| 68 | + Checkbox( |
| 69 | + self.browser, |
| 70 | + Selector( |
| 71 | + by=By.XPATH, |
| 72 | + select=self.elements.get("container").select + f"//div[@data-test-field='{checkbox_name}']/parent::div" |
| 73 | + ) |
| 74 | + ).check() |
| 75 | + TextBox( |
| 76 | + self.browser, |
| 77 | + Selector( |
| 78 | + by=By.XPATH, |
| 79 | + select=self.elements.get("container").select + f"//div[@data-test-field='{checkbox_name}' and @data-test='number']" |
| 80 | + ) |
| 81 | + ).set_value(checkbox_value) |
| 82 | + |
| 83 | + def select(self, group_name: str, checkbox_name: str, checkbox_value: str) -> None: |
| 84 | + """ |
| 85 | + Expands a group and selects a checkbox, then sets the specified value. |
| 86 | +
|
| 87 | + Args: |
| 88 | + group_name (str): The name of the group to expand. |
| 89 | + checkbox_name (str): The name of the checkbox to select. |
| 90 | + checkbox_value (str): The value to set for the checkbox. |
| 91 | + """ |
| 92 | + self.expand_group(group_name) |
| 93 | + self.select_checkbox_and_set_value(checkbox_name=checkbox_name, checkbox_value=checkbox_value) |
| 94 | + |
| 95 | + def deselect(self, group_name: str, checkbox_name: str) -> None: |
| 96 | + """ |
| 97 | + Expands a group and deselects the specified checkbox. |
| 98 | +
|
| 99 | + Args: |
| 100 | + group_name (str): The name of the group to expand. |
| 101 | + checkbox_name (str): The name of the checkbox to deselect. |
| 102 | + """ |
| 103 | + self.expand_group(group_name) |
| 104 | + Checkbox( |
| 105 | + self.browser, |
| 106 | + Selector( |
| 107 | + by=By.XPATH, |
| 108 | + select=self.elements.get("container").select + f"//div[@data-test-field='{checkbox_name}']/parent::div" |
| 109 | + ) |
| 110 | + ).uncheck() |
| 111 | + |
| 112 | + def get_checkbox_value(self, group_name: str, checkbox_name: str) -> str: |
| 113 | + """ |
| 114 | + Expands a group and retrieves the value of the specified checkbox. |
| 115 | +
|
| 116 | + Args: |
| 117 | + group_name (str): The name of the group to expand. |
| 118 | + checkbox_name (str): The name of the checkbox to retrieve the value from. |
| 119 | +
|
| 120 | + Returns: |
| 121 | + str: The value of the checkbox. |
| 122 | + """ |
| 123 | + self.expand_group(group_name) |
| 124 | + return TextBox( |
| 125 | + self.browser, |
| 126 | + Selector( |
| 127 | + by=By.XPATH, |
| 128 | + select=self.elements.get("container").select + f"//div[@data-test-field='{checkbox_name}' and @data-test='number']" |
| 129 | + ) |
| 130 | + ).get_value() |
| 131 | + |
| 132 | + def expand_group(self, group_name: str) -> None: |
| 133 | + """ |
| 134 | + Expands the specified group if it is not already expanded. |
| 135 | +
|
| 136 | + Args: |
| 137 | + group_name (str): The name of the group to expand. |
| 138 | + """ |
| 139 | + is_expanded = self.is_group_expanded(group_name) |
| 140 | + if not is_expanded: |
| 141 | + getattr(self, f"{group_name}_button").click() |
0 commit comments