|
38 | 38 | from ..generated.playwright_pb2 import Request |
39 | 39 | from ..utils import keyword, logger |
40 | 40 | from ..utils.data_types import ( |
| 41 | + ROBOT_FRAMEWORK_BROWSER_NO_SET, |
41 | 42 | AreaFields, |
42 | 43 | AriaSnapshotReturnType, |
43 | 44 | BoundingBox, |
|
52 | 53 | SelectionStrategy, |
53 | 54 | SelectOptions, |
54 | 55 | SizeFields, |
| 56 | + TextType, |
55 | 57 | ViewportDimensions, |
56 | 58 | ) |
57 | 59 |
|
@@ -228,47 +230,67 @@ def get_text( |
228 | 230 | assertion_operator: AssertionOperator | None = None, |
229 | 231 | assertion_expected: Any | None = None, |
230 | 232 | message: str | None = None, |
231 | | - ) -> str: |
| 233 | + *, |
| 234 | + text_type: TextType | None = None, |
| 235 | + ) -> str | list: |
232 | 236 | """Returns text attribute of the element found by ``selector``. |
233 | 237 |
|
234 | | - Keyword can also return `input` or `textarea` value property text. |
| 238 | + Keyword can also return `input` or `textarea` value property text. |
235 | 239 | See the `Finding elements` section for details about the selectors. |
236 | 240 |
|
237 | | - | =Arguments= | =Description= | |
238 | | - | ``assertion_operator`` | See `Assertions` for further details. Defaults to None. | |
239 | | - | ``assertion_expected`` | Expected value for the state | |
240 | | - | ``message`` | overrides the default error message for assertion. | |
| 241 | + | =Arguments= | =Description= | |
| 242 | + | ``assertion_operator`` | See `Assertions` for further details. Defaults to None. | |
| 243 | + | ``assertion_expected`` | Expected value for the state | |
| 244 | + | ``message`` | overrides the default error message for assertion. | |
| 245 | + | ``text_type`` | How text is text is returned. Possible values are ``allInnerTexts``, ``allTextContents``, ``innerText``, ``inputValue``, and ``innerHTML``. | |
241 | 246 |
|
242 | | - Keyword uses strict mode, see `Finding elements` for more details about strict mode. |
| 247 | + Keyword uses strict mode, see `Finding elements` for more details about strict mode. |
| 248 | + The ``text_type`` argument determines how text is returned. The ``allInnerTexts`` and |
| 249 | + ``allTextContents`` will return a list of strings, while other types return a single |
| 250 | + string. |
243 | 251 |
|
244 | | - Optionally asserts that the text matches the specified assertion. See `Assertions` |
245 | | - for further details for the assertion arguments. By default, assertion is not done. |
| 252 | + Optionally asserts that the text matches the specified assertion. See `Assertions` |
| 253 | + for further details for the assertion arguments. By default, assertion is not done. |
246 | 254 |
|
247 | | - Example: |
248 | | - | ${text} = `Get Text` id=important # Returns element text without assertion. |
249 | | - | ${text} = `Get Text` id=important == Important text # Returns element text with assertion. |
250 | | - | ${text} = `Get Text` //input == root # Returns input element text with assertion. |
| 255 | + Example: |
| 256 | + | ${text} = `Get Text` id=important # Returns element text without assertion. |
| 257 | + | ${text} = `Get Text` id=important == Important text # Returns element text with assertion. |
| 258 | + | ${text} = `Get Text` //input == root # Returns input element text with assertion. |
| 259 | + | ${text} = `Get Text` id=important text_type=innerHTML # Returns element inner HTML. |
| 260 | + | ${text} = `Get Text` id=important text_type=allInnerTexts # Returns element inner text as list of strings. |
251 | 261 |
|
252 | | - [https://forum.robotframework.org/t//4285|Comment >>] |
| 262 | + [https://forum.robotframework.org/t//4285|Comment >>] |
253 | 263 | """ |
254 | 264 | selector = self.presenter_mode(selector, self.strict_mode) |
255 | | - response = self._get_text(selector) |
| 265 | + response = self._get_text(selector, text_type) |
256 | 266 | logger.debug(response.log) |
257 | | - logger.info(f"Text: {response.body!r}") |
| 267 | + is_all_type = text_type in (TextType.allInnerTexts, TextType.allTextContents) |
| 268 | + if is_all_type: |
| 269 | + value = [str(item) for item in response.items] |
| 270 | + else: |
| 271 | + value = [str(response.items[0])] |
| 272 | + logger.info(f"Text: {value!r}") |
258 | 273 | formatter = self.get_assertion_formatter("Get Text") |
259 | 274 | return verify_assertion( |
260 | | - response.body, |
| 275 | + value[0] if not is_all_type else value, |
261 | 276 | assertion_operator, |
262 | 277 | assertion_expected, |
263 | 278 | "Text", |
264 | 279 | message, |
265 | 280 | formatter, |
266 | 281 | ) |
267 | 282 |
|
268 | | - def _get_text(self, selector: str): # To ease unit testing |
| 283 | + def _get_text( |
| 284 | + self, selector: str, text_type: TextType | None |
| 285 | + ): # To ease unit testing |
| 286 | + text_type_value = ( |
| 287 | + str(text_type.name) if text_type else ROBOT_FRAMEWORK_BROWSER_NO_SET |
| 288 | + ) |
269 | 289 | with self.playwright.grpc_channel() as stub: |
270 | 290 | return stub.GetText( |
271 | | - Request().ElementSelector(selector=selector, strict=self.strict_mode) |
| 291 | + Request().ElementSelectorWithTextType( |
| 292 | + selector=selector, strict=self.strict_mode, textType=text_type_value |
| 293 | + ) |
272 | 294 | ) |
273 | 295 |
|
274 | 296 | @keyword(tags=("Getter", "Assertion", "PageContent")) |
|
0 commit comments