Skip to content

Commit f2949ba

Browse files
committed
Locator type can also be WebElement
1 parent b19292d commit f2949ba

File tree

9 files changed

+126
-115
lines changed

9 files changed

+126
-115
lines changed

atest/acceptance/keywords/press_keys.robot

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ ${CTRL_OR_COMMAND} ${EMPTY}
88

99
*** Test Cases ***
1010
Press Keys Normal Keys
11-
Press Keys text_field AAAAA
11+
${element} = Get WebElement text_field
12+
Press Keys ${element} AAAAA
1213
Click Button OK
1314
Wait Until Page Contains AAAAA
1415

src/SeleniumLibrary/keywords/element.py

Lines changed: 49 additions & 49 deletions
Large diffs are not rendered by default.

src/SeleniumLibrary/keywords/formelement.py

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,18 @@
1515
# limitations under the License.
1616

1717
import os
18-
from typing import Optional
18+
from typing import Optional, Union
1919

2020
from robot.libraries.BuiltIn import BuiltIn
21+
from selenium.webdriver.remote.webelement import WebElement
2122

2223
from SeleniumLibrary.base import LibraryComponent, keyword
2324
from SeleniumLibrary.errors import ElementNotFound
2425

2526

2627
class FormElementKeywords(LibraryComponent):
2728
@keyword
28-
def submit_form(self, locator: Optional[str] = None):
29+
def submit_form(self, locator: Union[WebElement, None, str] = None):
2930
"""Submits a form identified by ``locator``.
3031
3132
If ``locator`` is not given, first form on the page is submitted.
@@ -40,7 +41,7 @@ def submit_form(self, locator: Optional[str] = None):
4041
element.submit()
4142

4243
@keyword
43-
def checkbox_should_be_selected(self, locator: str):
44+
def checkbox_should_be_selected(self, locator: Union[WebElement, str]):
4445
"""Verifies checkbox ``locator`` is selected/checked.
4546
4647
See the `Locating elements` section for details about the locator
@@ -54,7 +55,7 @@ def checkbox_should_be_selected(self, locator: str):
5455
)
5556

5657
@keyword
57-
def checkbox_should_not_be_selected(self, locator: str):
58+
def checkbox_should_not_be_selected(self, locator: Union[WebElement, str]):
5859
"""Verifies checkbox ``locator`` is not selected/checked.
5960
6061
See the `Locating elements` section for details about the locator
@@ -67,7 +68,7 @@ def checkbox_should_not_be_selected(self, locator: str):
6768

6869
@keyword
6970
def page_should_contain_checkbox(
70-
self, locator: str, message: Optional[str] = None, loglevel: str = "TRACE"
71+
self, locator: Union[WebElement, str], message: Optional[str] = None, loglevel: str = "TRACE"
7172
):
7273
"""Verifies checkbox ``locator`` is found from the current page.
7374
@@ -81,7 +82,7 @@ def page_should_contain_checkbox(
8182

8283
@keyword
8384
def page_should_not_contain_checkbox(
84-
self, locator: str, message: Optional[str] = None, loglevel: str = "TRACE"
85+
self, locator: Union[WebElement, str], message: Optional[str] = None, loglevel: str = "TRACE"
8586
):
8687
"""Verifies checkbox ``locator`` is not found from the current page.
8788
@@ -94,7 +95,7 @@ def page_should_not_contain_checkbox(
9495
self.assert_page_not_contains(locator, "checkbox", message, loglevel)
9596

9697
@keyword
97-
def select_checkbox(self, locator: str):
98+
def select_checkbox(self, locator: Union[WebElement, str]):
9899
"""Selects the checkbox identified by ``locator``.
99100
100101
Does nothing if checkbox is already selected.
@@ -108,7 +109,7 @@ def select_checkbox(self, locator: str):
108109
element.click()
109110

110111
@keyword
111-
def unselect_checkbox(self, locator: str):
112+
def unselect_checkbox(self, locator: Union[WebElement, str]):
112113
"""Removes the selection of checkbox identified by ``locator``.
113114
114115
Does nothing if the checkbox is not selected.
@@ -123,7 +124,7 @@ def unselect_checkbox(self, locator: str):
123124

124125
@keyword
125126
def page_should_contain_radio_button(
126-
self, locator: str, message: Optional[str] = None, loglevel: str = "TRACE"
127+
self, locator: Union[WebElement, str], message: Optional[str] = None, loglevel: str = "TRACE"
127128
):
128129
"""Verifies radio button ``locator`` is found from current page.
129130
@@ -138,7 +139,7 @@ def page_should_contain_radio_button(
138139

139140
@keyword
140141
def page_should_not_contain_radio_button(
141-
self, locator: str, message: Optional[str] = None, loglevel: str = "TRACE"
142+
self, locator: Union[WebElement, str], message: Optional[str] = None, loglevel: str = "TRACE"
142143
):
143144
"""Verifies radio button ``locator`` is not found from current page.
144145
@@ -200,7 +201,7 @@ def select_radio_button(self, group_name: str, value: str):
200201
element.click()
201202

202203
@keyword
203-
def choose_file(self, locator: str, file_path: str):
204+
def choose_file(self, locator: Union[WebElement, str], file_path: str):
204205
"""Inputs the ``file_path`` into the file input field ``locator``.
205206
206207
This keyword is most often used to input files into upload forms.
@@ -226,7 +227,7 @@ def choose_file(self, locator: str, file_path: str):
226227
self.ctx._running_keyword = None
227228

228229
@keyword
229-
def input_password(self, locator: str, password: str, clear: bool = True):
230+
def input_password(self, locator: Union[WebElement, str], password: str, clear: bool = True):
230231
"""Types the given password into the text field identified by ``locator``.
231232
232233
See the `Locating elements` section for details about the locator
@@ -254,7 +255,7 @@ def input_password(self, locator: str, password: str, clear: bool = True):
254255
self._input_text_into_text_field(locator, password, clear, disable_log=True)
255256

256257
@keyword
257-
def input_text(self, locator: str, text: str, clear: bool = True):
258+
def input_text(self, locator: Union[WebElement, str], text: str, clear: bool = True):
258259
"""Types the given ``text`` into the text field identified by ``locator``.
259260
260261
When ``clear`` is true, the input element is cleared before
@@ -281,7 +282,7 @@ def input_text(self, locator: str, text: str, clear: bool = True):
281282

282283
@keyword
283284
def page_should_contain_textfield(
284-
self, locator: str, message: Optional[str] = None, loglevel: str = "TRACE"
285+
self, locator: Union[WebElement, str], message: Optional[str] = None, loglevel: str = "TRACE"
285286
):
286287
"""Verifies text field ``locator`` is found from current page.
287288
@@ -295,7 +296,7 @@ def page_should_contain_textfield(
295296

296297
@keyword
297298
def page_should_not_contain_textfield(
298-
self, locator: str, message: Optional[str] = None, loglevel: str = "TRACE"
299+
self, locator: Union[WebElement, str], message: Optional[str] = None, loglevel: str = "TRACE"
299300
):
300301
"""Verifies text field ``locator`` is not found from current page.
301302
@@ -309,7 +310,7 @@ def page_should_not_contain_textfield(
309310

310311
@keyword
311312
def textfield_should_contain(
312-
self, locator: str, expected: str, message: Optional[str] = None
313+
self, locator: Union[WebElement, str], expected: str, message: Optional[str] = None
313314
):
314315
"""Verifies text field ``locator`` contains text ``expected``.
315316
@@ -330,7 +331,7 @@ def textfield_should_contain(
330331

331332
@keyword
332333
def textfield_value_should_be(
333-
self, locator: str, expected: str, message: Optional[str] = None
334+
self, locator: Union[WebElement, str], expected: str, message: Optional[str] = None
334335
):
335336
"""Verifies text field ``locator`` has exactly text ``expected``.
336337
@@ -351,7 +352,7 @@ def textfield_value_should_be(
351352

352353
@keyword
353354
def textarea_should_contain(
354-
self, locator: str, expected: str, message: Optional[str] = None
355+
self, locator: Union[WebElement, str], expected: str, message: Optional[str] = None
355356
):
356357
"""Verifies text area ``locator`` contains text ``expected``.
357358
@@ -372,7 +373,7 @@ def textarea_should_contain(
372373

373374
@keyword
374375
def textarea_value_should_be(
375-
self, locator: str, expected: str, message: Optional[str] = None
376+
self, locator: Union[WebElement, str], expected: str, message: Optional[str] = None
376377
):
377378
"""Verifies text area ``locator`` has exactly text ``expected``.
378379
@@ -393,7 +394,7 @@ def textarea_value_should_be(
393394

394395
@keyword
395396
def page_should_contain_button(
396-
self, locator: str, message: Optional[str] = None, loglevel: str = "TRACE"
397+
self, locator: Union[WebElement, str], message: Optional[str] = None, loglevel: str = "TRACE"
397398
):
398399
"""Verifies button ``locator`` is found from current page.
399400
@@ -411,7 +412,7 @@ def page_should_contain_button(
411412

412413
@keyword
413414
def page_should_not_contain_button(
414-
self, locator: str, message: Optional[str] = None, loglevel: str = "TRACE"
415+
self, locator: Union[WebElement, str], message: Optional[str] = None, loglevel: str = "TRACE"
415416
):
416417
"""Verifies button ``locator`` is not found from current page.
417418
@@ -428,7 +429,7 @@ def page_should_not_contain_button(
428429
def _get_value(self, locator, tag):
429430
return self.find_element(locator, tag).get_attribute("value")
430431

431-
def _get_checkbox(self, locator):
432+
def _get_checkbox(self, locator: Union[WebElement, str]):
432433
return self.find_element(locator, tag="checkbox")
433434

434435
def _get_radio_buttons(self, group_name):

src/SeleniumLibrary/keywords/frames.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,16 @@
1313
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1414
# See the License for the specific language governing permissions and
1515
# limitations under the License.
16+
from typing import Union
17+
18+
from selenium.webdriver.remote.webelement import WebElement
1619

1720
from SeleniumLibrary.base import LibraryComponent, keyword
1821

1922

2023
class FrameKeywords(LibraryComponent):
2124
@keyword
22-
def select_frame(self, locator: str):
25+
def select_frame(self, locator: Union[WebElement, str]):
2326
"""Sets frame identified by ``locator`` as the current frame.
2427
2528
See the `Locating elements` section for details about the locator
@@ -78,7 +81,7 @@ def current_frame_should_not_contain(self, text: str, loglevel: str = "TRACE"):
7881
self.info(f"Current frame did not contain text '{text}'.")
7982

8083
@keyword
81-
def frame_should_contain(self, locator: str, text: str, loglevel: str = "TRACE"):
84+
def frame_should_contain(self, locator: Union[WebElement, str], text: str, loglevel: str = "TRACE"):
8285
"""Verifies that frame identified by ``locator`` contains ``text``.
8386
8487
See the `Locating elements` section for details about the locator
@@ -94,7 +97,7 @@ def frame_should_contain(self, locator: str, text: str, loglevel: str = "TRACE")
9497
)
9598
self.info(f"Frame '{locator}' contains text '{text}'.")
9699

97-
def _frame_contains(self, locator, text):
100+
def _frame_contains(self, locator: Union[WebElement, str], text: str):
98101
element = self.find_element(locator)
99102
self.driver.switch_to.frame(element)
100103
self.info(f"Searching for text from frame '{locator}'.")

src/SeleniumLibrary/keywords/screenshot.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@
1414
# See the License for the specific language governing permissions and
1515
# limitations under the License.
1616
import os
17+
from typing import Union, Optional
1718

1819
from robot.utils import get_link_path
20+
from selenium.webdriver.remote.webelement import WebElement
1921

2022
from SeleniumLibrary.base import LibraryComponent, keyword
2123
from SeleniumLibrary.utils.path_formatter import _format_path
@@ -27,7 +29,7 @@
2729

2830
class ScreenshotKeywords(LibraryComponent):
2931
@keyword
30-
def set_screenshot_directory(self, path: str) -> str:
32+
def set_screenshot_directory(self, path: Union[None, str]) -> str:
3133
"""Sets the directory for captured screenshots.
3234
3335
``path`` argument specifies the absolute path to a directory where
@@ -125,7 +127,7 @@ def _capture_page_screen_to_log(self):
125127

126128
@keyword
127129
def capture_element_screenshot(
128-
self, locator: str, filename: str = DEFAULT_FILENAME_ELEMENT
130+
self, locator: Union[WebElement, None, str], filename: str = DEFAULT_FILENAME_ELEMENT
129131
) -> str:
130132
"""Captures a screenshot from the element identified by ``locator`` and embeds it into log file.
131133

0 commit comments

Comments
 (0)