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¶
Descriptor for init PomcornElement as attribute by locator.
.. code-block:: python
# Example
from pomcorn import Page, Element
class MainPage(Page):
title_element = Element(locators.ClassLocator("page-title"))
Source code in pomcorn/descriptors/element.py
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 | |
__get__(instance, _type)
¶
Get element with stored locator.
Source code in pomcorn/descriptors/element.py
62 63 64 65 66 67 68 69 70 | |
__init__(locator=None, *, relative_locator=None)
¶
__init__(locator: locators.XPathLocator | None = None) -> None
__init__(*, relative_locator: locators.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.
Source code in pomcorn/descriptors/element.py
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | |
__set_name__(_owner, name)
¶
Save attribute name for which descriptor is created.
Source code in pomcorn/descriptors/element.py
58 59 60 | |
prepare_element(instance)
¶
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.
Source code in pomcorn/descriptors/element.py
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 | |