Descriptors

pomcorn now provides descriptor for Elements. This allows us to define elements as class attributes of a page or component. Example of usage:

from pomcorn import Element, Page, locators
from demo.pages.common.navigation_bar import Navbar

class PyPIPage(Page):

    search_input = Element(locators.ClassLocator("search"))

The descriptor takes a locator to locate element on page or inside component (then we need to pass is_relative_locator=true) and creates a new instance of element the first time search_input attribute is accessed and caches it to return the cached value the next time.

The cache is intended to avoid calling wait_until_visible multiple times in the initialization of the element.

Element descriptor interfaces

class pomcorn.descriptors.Element(locator: XPathLocator | None = None)[source]
class pomcorn.descriptors.Element(*, relative_locator: XPathLocator | None = None)

Descriptor for init PomcornElement as attribute by locator.

# Example
from pomcorn import Page, Element

class MainPage(Page):
    title_element = Element(locators.ClassLocator("page-title"))
cache_attribute_name = 'cached_elements'
__init__(locator: XPathLocator | None = None) None[source]
__init__(*, relative_locator: XPathLocator | None = None) None

Initialize descriptor.

Use relative_locator if you need to include base_locator of instance, otherwise use locator.

If descriptor is used for instance of Page, then relative_locator is not needed, since element will be searched across the entire page, not within some component.

prepare_element(instance: WebView) XPathElement[source]

Init and cache element in instance.

Initiate element only once, and then store it in an instance and return it each subsequent time. This is to avoid calling wait_until_visible multiple times in the init of component.

If the instance doesn’t already have an attribute to store cache, it will be set.

If descriptor is used for Component and self.is_relative_locator=True, element will be found by sum of base_locator of that component and passed locator of descriptor.