Locators¶
Locators are special mechanisms used to find elements on a web page. They allow to locate specific elements such as buttons, input fields, or links to interact with them.
The Selenium webdriver methods use tuples (By, query) to find elements, where By is one of
the supported locator
strategies and query is the query for that strategy.
Read about Selenium supported locator strategies.
Pomcorn implements its own classes for defining web page elements - Locators. XPath was chosen as the only strategy because it eliminated the need to specify the type of strategy and also made it easier to create relative locators.
Note
Others prefer NOT to use XPath because the DOM can change frequently and tests will crash. Therefore, to make the tests more stable, we have implemented a number of locators that follow the same logic as the other strategies (search by css, by tag name, by classes, by properties, etc.), but based on XPath.
Interfaces¶
classDiagram
Locator <|-- XPathLocator
XPathLocator <|-- TInitLocator
XPathLocator <|-- TLocator
XPathLocator <|-- ElementWithTextLocator
XPathLocator <|-- InputByLabelLocator
XPathLocator <|-- PropertyLocator
XPathLocator <|-- TagNameLocator
XPathLocator <|-- TextAreaByLabelLocator
PropertyLocator <|-- ClassLocator
PropertyLocator <|-- DataTestIdLocator
PropertyLocator <|-- IdLocator
PropertyLocator <|-- NameLocator
ElementWithTextLocator <|-- ButtonWithTextLocator
- you can zoom it
Base locator for looking for elements in page.
Source code in pomcorn/locators/base_locators.py
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 | |
__init__(by, query)
¶
Init locator.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
by
|
str
|
One of the supported selenium locator strategies. |
required |
query
|
str
|
Query for the strategy. |
required |
Source code in pomcorn/locators/base_locators.py
36 37 38 39 40 41 42 43 44 45 46 47 | |
__iter__()
¶
Unpack locator.
This is necessary because selenium WebDriver methods use
By and value tuples to find elements. Thanks to this we can use
*locator for these methods.
Source code in pomcorn/locators/base_locators.py
49 50 51 52 53 54 55 56 57 | |
Bases: Locator
Locator to looking for elements in page by XPath.
XPathLocator overrides methods for / and // operators to provide
"path-like" syntax for locators.
So we can use / and // operators to concatenate locators query by /
and // accordingly:
# //\*[@class="class"]
class_locator = ClassLocator("class")
# //container[@prop="value"]
property_locator = PropertyLocator(
prop="prop",
value="value",
container="container",
)
# //\*[@class="class"]/container[@prop="value"]
class_locator / property_locator
# //\*[@class="class"]//container[@prop="value"]
class_locator // property_locator
To extend query of locator you can use extend_query method:
new_locator = class_locator.extend_query("[@some_prop='value']")
new_locator.query # //\*[@class="class"][@some_prop="value"]
All custom locators that inherit XPathLocator should be independent and
start with //.
Source code in pomcorn/locators/base_locators.py
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 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 | |
__bool__()
¶
Return whether query of current locator is empty or not.
Source code in pomcorn/locators/base_locators.py
198 199 200 | |
__floordiv__(other)
¶
Override // operator to implement nested XPath locators.
"//" used to select all descendants (children, grandchildren, great-grandchildren, etc.) of current node, regardless of their level in hierarchy.
Source code in pomcorn/locators/base_locators.py
135 136 137 138 139 140 141 142 143 | |
__getitem__(value)
¶
Allow to set xpath expressions or index into the locator query.
Examples:
div_locator = XPathLocator("//div") --> //div
Indexation¶
div_locator[0] --> (//div)[1] # xpath numeration starts with 1
div_locator[-1] --> (//div)[last()]
Attribute condition¶
div_locator["@type='black'"] --> `//div[@type='black']
Checking if there are children¶
child_locator = XPathLocator("//label").contains("Schedule")
--> //label[contains(., 'Star']
div_locator[child_locator] --> //div[//label[contains(., 'Star']]
Source code in pomcorn/locators/base_locators.py
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 | |
__init__(query)
¶
Set related query for locators concatenation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query
|
str
|
Query for the XPath locator strategy. |
required |
Source code in pomcorn/locators/base_locators.py
117 118 119 120 121 122 123 124 125 | |
__or__(other)
¶
Override | operator to implement variant XPath locators.
Example
span = XPathLocator("//span") div = XPathLocator("//div") img = XPathLocator("//img")
(span | div) // img == XPathLocator("(//span | //div)//img")
span | div // img == XPathLocator("(//span | //div//img)")
Source code in pomcorn/locators/base_locators.py
145 146 147 148 149 150 151 152 153 154 155 156 157 158 | |
__truediv__(other)
¶
Override / operator to implement following XPath locators.
"/" used to select the nearest children of the current node.
Source code in pomcorn/locators/base_locators.py
127 128 129 130 131 132 133 | |
contains(text, exact=False)
¶
Return new XPathLocator with search on contained text.
This is shortcut for the commonly used
.extend_query(f"[contains(., '{text}')]).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The text that should be inside the tag. |
required |
exact
|
bool
|
Specify whether the text being searched must match exactly. By default, the search is based on a partial match. |
False
|
Source code in pomcorn/locators/base_locators.py
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 | |
extend_query(extra_query)
¶
Return new XPathLocator with extended query.
Source code in pomcorn/locators/base_locators.py
242 243 244 | |
prepare_relative_locator(other, separator='/')
¶
Prepare relative locator base on queries of two locators.
If one of parent and other locator queries is empty, the method will return only the filled one.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
XPathLocator | str
|
Child locator object or str locator query. |
required |
separator
|
Literal['/', '//']
|
Literal which will placed between locators queries - "/" used to select nearest children of current node and "//" used to select all descendants (children, grandchildren, great-grandchildren, etc.) of current node, regardless of their level in hierarchy. |
'/'
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If parent and child locators queries are empty. |
Source code in pomcorn/locators/base_locators.py
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 | |
ButtonWithTextLocator
¶
Bases: ElementWithTextLocator
Locator to looking for button with text by XPath.
Inherits from ElementWithTextLocator and sets the default value
element="button".
Source code in pomcorn/locators/xpath_locators.py
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 | |
__init__(text, exact=False)
¶
Init XPathLocator.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The text that should be inside the button tag. |
required |
exact
|
bool
|
Specify whether the value of the property being searched must match exactly. By default, the search is based on a partial match of the value. |
False
|
Source code in pomcorn/locators/xpath_locators.py
194 195 196 197 198 199 200 201 202 203 204 | |
ClassLocator
¶
Bases: PropertyLocator
Locator to look for elements with partial class by XPath.
Inherits from PropertyLocator and sets the default value
prop="class".
Source code in pomcorn/locators/xpath_locators.py
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 | |
__init__(class_name, container='*', exact=False)
¶
Init XPathLocator.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
class_name
|
str
|
The name of tag class. |
required |
container
|
str
|
The tag in which the property should be. The default is
|
'*'
|
exact
|
bool
|
Specify whether the value of the property being searched must match exactly. By default, the search is based on a partial match of the value. |
False
|
Source code in pomcorn/locators/xpath_locators.py
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 | |
DataTestIdLocator
¶
Bases: PropertyLocator
Locator to look for elements with custom testid property.
Inherits from PropertyLocator and sets the default values
prop="data-testid" and exact=True.
Source code in pomcorn/locators/xpath_locators.py
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 | |
__init__(value, container='*', exact=True)
¶
Init XPathLocator.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
str
|
The value of |
required |
container
|
str
|
The tag in which the property should be. The default is
|
'*'
|
exact
|
bool
|
Specify whether the value of the property being searched must match exactly. By default, the search is based on a partial match of the value. |
True
|
Source code in pomcorn/locators/xpath_locators.py
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | |
ElementWithTextLocator
¶
Bases: XPathLocator
Locator to look for elements with text by XPath.
Source code in pomcorn/locators/xpath_locators.py
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 | |
__init__(text, element='*', exact=False)
¶
Init XPathLocator.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The text that should be inside the tag. |
required |
element
|
str
|
The tag in which the text should be. The default is
|
'*'
|
exact
|
bool
|
Specify whether the value of the property being searched must match exactly. By default, the search is based on a partial match of the value. |
False
|
Source code in pomcorn/locators/xpath_locators.py
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 | |
IdLocator
¶
Bases: PropertyLocator
Locator to look for elements with ID by Xpath.
Inherits from PropertyLocator and sets the default values prop="id"
and exact=True.
Source code in pomcorn/locators/xpath_locators.py
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 | |
__init__(value, container='*')
¶
Init XPathLocator.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
str
|
The value of |
required |
container
|
str
|
The tag in which the property should be. The default is
|
'*'
|
Source code in pomcorn/locators/xpath_locators.py
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 | |
InputByLabelLocator
¶
Bases: XPathLocator
Locator to looking for input next to label by XPath.
Specify the query as the string
//label[contains(., "label")]/following-sibling::input, where label
is the text of the input label.
.. code-block:: html
# Example
<div>
<label for="InputWithLabel">Title</label>
<input id="InputWithLabel" value="Value">
</div>
Source code in pomcorn/locators/xpath_locators.py
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 | |
__init__(label)
¶
Init XPathLocator.
Source code in pomcorn/locators/xpath_locators.py
247 248 249 250 251 252 253 254 | |
InputInLabelLocator
¶
Bases: XPathLocator
Locator to looking for input with label by XPath.
Specify the query as the string
//label[contains(., "label")]//input, where label is the text of
the input label.
.. code-block:: html
# Example
<label>Title</label>
<input value="Value">
</label>
Source code in pomcorn/locators/xpath_locators.py
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 | |
__init__(label)
¶
Init XPathLocator.
Source code in pomcorn/locators/xpath_locators.py
223 224 225 226 227 | |
NameLocator
¶
Bases: PropertyLocator
Locator to look for elements with name by Xpath.
Inherits from PropertyLocator and sets the default values
prop="name" and exact=True.
Source code in pomcorn/locators/xpath_locators.py
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 | |
__init__(value, container='*')
¶
Init XPathLocator.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
str
|
The value of |
required |
container
|
str
|
The tag in which the property should be. The default is
|
'*'
|
Source code in pomcorn/locators/xpath_locators.py
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 | |
PropertyLocator
¶
Bases: XPathLocator
Locator to look for elements with property by XPath.
Source code in pomcorn/locators/xpath_locators.py
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 | |
__init__(prop, value, container='*', exact=False)
¶
Init XPathLocator.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prop
|
str
|
The name of the html tag property. |
required |
value
|
str
|
The value of property. |
required |
container
|
str
|
The tag in which the property should be. The default is
|
'*'
|
exact
|
bool
|
Specify whether the value of the property being searched must match exactly. By default, the search is based on a partial match of the value. |
False
|
Source code in pomcorn/locators/xpath_locators.py
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | |
TagNameLocator
¶
Bases: XPathLocator
Locator to look for elements with tag by Xpath.
Source code in pomcorn/locators/xpath_locators.py
4 5 6 7 8 9 10 11 12 13 14 | |
__init__(tag)
¶
Init XPathLocator.
Specify the query as the string //tag, where tag is the name of
the html tag.
Source code in pomcorn/locators/xpath_locators.py
7 8 9 10 11 12 13 14 | |
TextAreaByLabelLocator
¶
Bases: XPathLocator
Locator to looking for textarea with label by XPath.
Specify the query as the string
//*[label[contains(text(), "{label}")]]/textarea, where label
is the text of the textarea label.
Source code in pomcorn/locators/xpath_locators.py
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 | |
__init__(label)
¶
Init XPathLocator.
Source code in pomcorn/locators/xpath_locators.py
266 267 268 269 270 271 272 273 | |