Skip to content

Commit 2964d34

Browse files
feat: adding support for checkboxgroup component
1 parent d156287 commit 2964d34

File tree

6 files changed

+357
-86
lines changed

6 files changed

+357
-86
lines changed

pytest_splunk_addon_ui_smartx/components/controls/checkbox.py

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,30 @@ class Checkbox(BaseControl):
2828

2929
def __init__(self, browser, container, searchable=True):
3030
super().__init__(browser, container)
31-
self.elements.update(
32-
{
33-
"internal_container": Selector(
34-
select=container.select + ' [data-test="switch"]'
35-
),
36-
"checkbox": Selector(select=container.select + ' [data-test="switch"]'),
37-
"checkbox_btn": Selector(
38-
select=container.select + ' [data-test="button"][role="checkbox"]'
39-
),
40-
}
41-
)
31+
if container.by == "xpath":
32+
self.elements.update(
33+
{
34+
"internal_container": Selector(by=By.XPATH,
35+
select=container.select + '//div[@data-test="switch"]'
36+
),
37+
"checkbox": Selector(by=By.XPATH,select=container.select + '//div[@data-test="switch"]'),
38+
"checkbox_btn": Selector(by=By.XPATH,
39+
select=container.select + '//button[@role="checkbox"]'
40+
),
41+
}
42+
)
43+
else:
44+
self.elements.update(
45+
{
46+
"internal_container": Selector(
47+
select=container.select + ' [data-test="switch"]'
48+
),
49+
"checkbox": Selector(select=container.select + ' [data-test="switch"]'),
50+
"checkbox_btn": Selector(
51+
select=container.select + ' [data-test="button"][role="checkbox"]'
52+
),
53+
}
54+
)
4255

4356
def toggle(self, max_attempts=5):
4457
"""
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
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()

pytest_splunk_addon_ui_smartx/components/controls/textbox.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import platform
1717

1818
from selenium.webdriver.common.keys import Keys
19+
from selenium.webdriver.common.by import By
1920

2021
from ..base_component import Selector
2122
from .base_control import BaseControl
@@ -37,7 +38,10 @@ def __init__(self, browser, container, encrypted=False):
3738
self.encrypted = encrypted
3839
self.container = container
3940
self.browser = browser
40-
self.elements.update({"input": Selector(select=container.select + " input")})
41+
if container.by == "xpath":
42+
self.elements.update({"input": Selector(by=By.XPATH, select=container.select + "//input")})
43+
else:
44+
self.elements.update({"input": Selector(select=container.select + " input")})
4145

4246
def set_value(self, value):
4347
"""

0 commit comments

Comments
 (0)