Skip to content

Commit

Permalink
feat: adding support for checkboxgroup component (#439)
Browse files Browse the repository at this point in the history
Added support for testing checkbox-group component.

- Added support for testing checkbox and textbox component using xpath
rather then fixing it with css selector.
- Reused checkbox and textbox component inside checkbox-group component.
- Updated test cases to check the checkbox-group component
  • Loading branch information
harshilgajera-crest authored Sep 19, 2024
1 parent 9fcd861 commit 856776a
Show file tree
Hide file tree
Showing 6 changed files with 405 additions and 12 deletions.
43 changes: 32 additions & 11 deletions pytest_splunk_addon_ui_smartx/components/controls/checkbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,38 @@ class Checkbox(BaseControl):

def __init__(self, browser, container, searchable=True):
super().__init__(browser, container)
self.elements.update(
{
"internal_container": Selector(
select=container.select + ' [data-test="switch"]'
),
"checkbox": Selector(select=container.select + ' [data-test="switch"]'),
"checkbox_btn": Selector(
select=container.select + ' [data-test="button"][role="checkbox"]'
),
}
)
if container.by == "xpath":
self.elements.update(
{
"internal_container": Selector(
by=By.XPATH,
select=container.select + '//div[@data-test="switch"]',
),
"checkbox": Selector(
by=By.XPATH,
select=container.select + '//div[@data-test="switch"]',
),
"checkbox_btn": Selector(
by=By.XPATH,
select=container.select + '//button[@role="checkbox"]',
),
}
)
else:
self.elements.update(
{
"internal_container": Selector(
select=container.select + ' [data-test="switch"]'
),
"checkbox": Selector(
select=container.select + ' [data-test="switch"]'
),
"checkbox_btn": Selector(
select=container.select
+ ' [data-test="button"][role="checkbox"]'
),
}
)

def toggle(self, max_attempts=5):
"""
Expand Down
152 changes: 152 additions & 0 deletions pytest_splunk_addon_ui_smartx/components/controls/checkboxgroup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
#
# Copyright 2024 Splunk Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
from selenium.webdriver.common.by import By

from ..base_component import Selector
from .base_control import BaseControl
from .checkbox import Checkbox
from .textbox import TextBox


class CheckboxGroup(BaseControl):
"""
Entity_Component : CheckboxGroup
This class represents a group of checkboxes, allowing for expanding groups,
selecting/deselecting checkboxes, and setting or retrieving values.
Note: Please use xpath as locator for this component.
"""

def __init__(self, browser, container) -> None:
"""
Initializes the CheckboxGroup.
Args:
browser (Browser): The browser instance to interact with.
container (Selector): The container element that holds the checkbox group.
"""
super().__init__(browser, container)

def is_group_expanded(self, checkbox_group_name: str) -> bool:
"""
Checks if the specified group is expanded.
Args:
checkbox_group_name (str): The name of the checkbox group to check.
Returns:
bool: True if the group is expanded, False otherwise.
"""
self.elements.update(
{
f"{checkbox_group_name}_button": Selector(
by=By.XPATH,
select=self.elements.get("container").select
+ f'//span[text()="{checkbox_group_name}"]/ancestor::button',
)
}
)
return (
getattr(self, f"{checkbox_group_name}_button").get_attribute(
"aria-expanded"
)
== "true"
)

def get_checkbox(self, checkbox_name: str) -> Checkbox:
return Checkbox(
self.browser,
Selector(
by=By.XPATH,
select=self.elements.get("container").select
+ f"//div[@data-test-field='{checkbox_name}']/parent::div",
),
)

def select_checkbox_and_set_value(
self, checkbox_group_name: str, checkbox_name: str, checkbox_value: str = None
) -> None:
"""
Expands a group and selects a checkbox, then sets the specified value.
Args:
checkbox_group_name (str): The name of the group to expand.
checkbox_name (str): The name of the checkbox to select.
checkbox_value (str): The value to set for the checkbox.
"""
self.expand_group(checkbox_group_name)
self.get_checkbox(checkbox_name).check()
if checkbox_value:
self.get_textbox(checkbox_name).set_value(checkbox_value)

def deselect(self, checkbox_group_name: str, checkbox_name: str) -> None:
"""
Expands a group and deselects the specified checkbox.
Args:
checkbox_group_name (str): The name of the group to expand.
checkbox_name (str): The name of the checkbox to deselect.
"""
self.expand_group(checkbox_group_name)
self.get_checkbox(checkbox_name).uncheck()

def get_textbox(self, checkbox_name: str) -> TextBox:
return TextBox(
self.browser,
Selector(
by=By.XPATH,
select=self.elements.get("container").select
+ f"//div[@data-test-field='{checkbox_name}' and @data-test='number']",
),
)

def get_checkbox_text_value(
self, checkbox_group_name: str, checkbox_name: str
) -> str:
"""
Expands a group and retrieves the text value of the specified checkbox.
Args:
checkbox_group_name (str): The name of the group to expand.
checkbox_name (str): The name of the checkbox to retrieve the value from.
Returns:
str: The value of the checkbox.
"""
self.expand_group(checkbox_group_name)
return self.get_textbox(checkbox_name).get_value()

def expand_group(self, checkbox_group_name: str) -> None:
"""
Expands the specified group if it is not already expanded.
Args:
checkbox_group_name (str): The name of the group to expand.
"""
is_expanded = self.is_group_expanded(checkbox_group_name)
if not is_expanded:
getattr(self, f"{checkbox_group_name}_button").click()

def collapse_group(self, checkbox_group_name: str) -> None:
"""
collapse the specified group if it is not already expanded.
Args:
checkbox_group_name (str): The name of the group to expand.
"""
is_expanded = self.is_group_expanded(checkbox_group_name)
if is_expanded:
getattr(self, f"{checkbox_group_name}_button").click()
10 changes: 9 additions & 1 deletion pytest_splunk_addon_ui_smartx/components/controls/textbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import platform

from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By

from ..base_component import Selector
from .base_control import BaseControl
Expand All @@ -37,7 +38,14 @@ def __init__(self, browser, container, encrypted=False):
self.encrypted = encrypted
self.container = container
self.browser = browser
self.elements.update({"input": Selector(select=container.select + " input")})
if container.by == "xpath":
self.elements.update(
{"input": Selector(by=By.XPATH, select=container.select + "//input")}
)
else:
self.elements.update(
{"input": Selector(select=container.select + " input")}
)

def set_value(self, value):
"""
Expand Down
158 changes: 158 additions & 0 deletions tests/testdata/Splunk_TA_UCCExample/globalConfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -860,6 +860,164 @@
"text": "Help Link",
"link": "https://docs.splunk.com/Documentation"
}
},
{
"field": "apis",
"label": "APIs/Interval (in seconds)",
"type": "checkboxGroup",
"options": {
"groups": [
{
"label": "EC2",
"options": {
"isExpandable": true
},
"fields": [
"ec2_volumes",
"ec2_instances",
"ec2_reserved_instances",
"ebs_snapshots",
"rds_instances",
"rds_reserved_instances",
"ec2_key_pairs",
"ec2_security_groups",
"ec2_images",
"ec2_addresses"
]
},
{
"label": "ELB",
"options": {
"isExpandable": true
},
"fields": [
"classic_load_balancers",
"application_load_balancers"
]
},
{
"label": "VPC",
"options": {
"isExpandable": true
},
"fields": [
"vpcs",
"vpc_network_acls",
"vpc_subnets"
]
}
],
"rows": [
{
"field": "ec2_volumes",
"checkbox": {
"defaultValue": true
},
"input": {
"defaultValue": 3600,
"required": true
}
},
{
"field": "ec2_instances",
"input": {
"defaultValue": 3600,
"required": true
}
},
{
"field": "ec2_reserved_instances",
"input": {
"defaultValue": 3600,
"required": true
}
},
{
"field": "ebs_snapshots",
"input": {
"defaultValue": 3600,
"required": true
}
},
{
"field": "rds_instances",
"input": {
"defaultValue": 3600,
"required": true
}
},
{
"field": "rds_reserved_instances",
"input": {
"defaultValue": 3600,
"required": true
}
},
{
"field": "ec2_key_pairs",
"input": {
"defaultValue": 3600,
"required": true
}
},
{
"field": "ec2_security_groups",
"input": {
"defaultValue": 3600,
"required": true
}
},
{
"field": "ec2_images",
"input": {
"defaultValue": 3600,
"required": true
}
},
{
"field": "ec2_addresses",
"input": {
"defaultValue": 3600,
"required": true
}
},
{
"field": "classic_load_balancers",
"input": {
"defaultValue": 3600,
"required": true
}
},
{
"field": "application_load_balancers",
"input": {
"defaultValue": 3600,
"required": true
}
},
{
"field": "vpcs",
"input": {
"defaultValue": 3600,
"required": true
}
},
{
"field": "vpc_network_acls",
"input": {
"defaultValue": 3600,
"required": true
}
},
{
"field": "vpc_subnets",
"input": {
"defaultValue": 3600,
"required": true
}
}
]
}
}
],
"title": "Example Input Two"
Expand Down
Loading

0 comments on commit 856776a

Please sign in to comment.