Skip to content

Commit 429a348

Browse files
committed
Prevent Input Password to reveal password from Selenium logs
Fixes robotframework#1454
1 parent ac49c0a commit 429a348

File tree

2 files changed

+36
-10
lines changed

2 files changed

+36
-10
lines changed

atest/acceptance/keywords/textfields.robot

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,24 @@ Input Text and Input Password
2525
Submit Form
2626
Verify Location Is "forms/submit.html"
2727

28+
Input Password Should Not Log Password String
29+
[Tags] NoGrid
30+
[Setup] Go To Page "forms/login.html"
31+
[Documentation]
32+
... LOG 2:1 INFO Typing password into text field 'password_field'.
33+
... LOG 2:2 DEBUG STARTS: POST http
34+
... LOG 2:3 DEBUG STARTS: http
35+
... LOG 2:4 DEBUG Finished Request
36+
... LOG 2:5 DEBUG STARTS: POST http
37+
... LOG 2:6 DEBUG STARTS: http
38+
... LOG 2:7 DEBUG Finished Request
39+
... LOG 2:8 INFO Temporally setting log level to: NONE
40+
... LOG 2:9 INFO Log level changed from NONE to DEBUG.
41+
... LOG 2:10 NONE
42+
... LOG 3:1 INFO Typing text 'username' into text field 'username_field'.
43+
Input Password password_field password
44+
Input Text username_field username
45+
2846
Input Text and Input Password No Clear
2947
[Setup] Go To Page "forms/login.html"
3048
Input Text username_field user clear=False

src/SeleniumLibrary/keywords/formelement.py

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
import os
1818

19+
from robot.libraries.BuiltIn import BuiltIn
20+
1921
from SeleniumLibrary.base import LibraryComponent, keyword
2022
from SeleniumLibrary.errors import ElementNotFound
2123
from SeleniumLibrary.utils import is_noney, is_truthy
@@ -231,16 +233,15 @@ def input_password(self, locator, password, clear=True):
231233
232234
| Input Password | password_field | ${PASSWORD} |
233235
234-
Notice also that SeleniumLibrary logs all the communication with
235-
browser drivers using the DEBUG level, and the actual password can
236-
be seen there. Additionally, Robot Framework logs all arguments using
237-
the TRACE level. Tests must thus not be executed using level below
238-
INFO if the password should not be logged in any format.
236+
Please notice that Robot Framework logs all arguments using
237+
the TRACE level and tests must not be executed using level below
238+
DEBUG if the password should not be logged in any format.
239239
240-
The `clear` argument is new in SeleniumLibrary 4.0
240+
The `clear` argument is new in SeleniumLibrary 4.0. Hiding password
241+
logging from Selenium logs is new in SeleniumLibrary 4.2.
241242
"""
242243
self.info("Typing password into text field '%s'." % locator)
243-
self._input_text_into_text_field(locator, password, clear)
244+
self._input_text_into_text_field(locator, password, clear, True)
244245

245246
@keyword
246247
def input_text(self, locator, text, clear=True):
@@ -266,7 +267,7 @@ def input_text(self, locator, text, clear=True):
266267
argument are new in SeleniumLibrary 4.0
267268
"""
268269
self.info("Typing text '%s' into text field '%s'." % (text, locator))
269-
self._input_text_into_text_field(locator, text, clear)
270+
self._input_text_into_text_field(locator, text, clear, False)
270271

271272
@keyword
272273
def page_should_contain_textfield(self, locator, message=None, loglevel='TRACE'):
@@ -421,8 +422,15 @@ def _get_value_from_radio_buttons(self, elements):
421422
return element.get_attribute('value')
422423
return None
423424

424-
def _input_text_into_text_field(self, locator, text, clear):
425+
def _input_text_into_text_field(self, locator, text, clear, disable_log):
425426
element = self.find_element(locator)
426427
if is_truthy(clear):
427428
element.clear()
428-
element.send_keys(text)
429+
if disable_log:
430+
self.info('Temporally setting log level to: NONE')
431+
previous_level = BuiltIn().set_log_level('NONE')
432+
try:
433+
element.send_keys(text)
434+
finally:
435+
if disable_log:
436+
BuiltIn().set_log_level(previous_level)

0 commit comments

Comments
 (0)