Group Key Features¶
Note
Here you will find information on complex or ambiguous functionality. For a comprehensive list of all available methods, please refer to the Group Interface documentation.
The Group class extends the Element class to provide a mechanism for grouping related elements together and managing
their context within a web page. This grouping facilitates the creation of more organized and reusable
Page Component Object Models (PCOM).
1. Context Management¶
The primary distinction between Group and Element is the ability to change the context from which element locators
are resolved. By default, elements are located within the entire browser window. However, when an element is defined
as an attribute within a Group class, its locator is searched within the context of the Group’s locator.
The Group class offers flexibility in defining parent-child relationships.
The parent arg of Element can be set to False to opt out of automatic assignment or to any other Element
to create custom hierarchies. For more details, how Element object works refer to Element documentation
Implementation details¶
The Group class automatically sets the parent argument for all of its element-based attributes during initialization.
This parent value is used to determine the context within which the element’s locator should be searched.
Code example¶
from mops.base.element import Element
from mops.base.group import Group
from mops.base.driver_wrapper import DriverWrapper
class Section(Group):
def __init__(self):
super().__init__('.section', name='section')
section_button = Element('.button', name='section button')
section_footer = Element('.footer', name='section button', parent=False)
section_footer_link = Element('.link', name='section button', parent=section_footer)
data = 'section data'
# pytest usage
def test_section_verification(driver_wrapper: DriverWrapper):
section = Section()
assert section.section_button.is_displayed()
assert section.section_footer_link.text == 'www.example.com'
Code Explanation - Section initialization:
Automatically sets the
parentargument for thesection_buttonattribute to theSectioninstance.Does not set the
parentargument for thesection_footerattribute because its ownparentargument isFalse.Does not set the
parentargument for thesection_footer_linkattribute because its ownparentargument already issection_footer.Does not set the
parentargument for thedataattribute because it is not an instance of theElementobject.
Code Explanation - Behavior when methods are called:
When the
is_displayed()method ofsection_buttonis called:The driver will initially search for the
Sectionlocator.Then, it will proceed to locate the
section_buttonelement within theSectioncontext.
When the
textproperty ofsection_footer_linkis called:The driver will initially search for the
section_footerlocator.Then, it will proceed to locate the
section_footer_linkelement within thesection_footercontext.