Skip to content

Commit 9c46a64

Browse files
authored
refactor: raise helpful ValueError on UnicodeDecodeError
1 parent 5f153e3 commit 9c46a64

File tree

1 file changed

+3
-12
lines changed

1 file changed

+3
-12
lines changed

appium/webdriver/extensions/images_comparison.py

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,6 @@
1414

1515
from typing import Any, Dict, Union
1616

17-
import base64
18-
19-
import binascii
20-
2117
from appium.protocols.webdriver.can_execute_commands import CanExecuteCommands
2218

2319
from ..mobilecommand import MobileCommand as Command
@@ -151,13 +147,8 @@ def _add_commands(self) -> None:
151147

152148

153149
def _adjust_image_payload(payload: Base64Payload) -> str:
154-
if isinstance(payload, str):
155-
return payload
156150
try:
157-
b64_str = payload.decode('ascii')
158-
base64.b64decode(payload,validate=True)
159-
return b64_str
160-
except (UnicodeDecodeError, binascii.Error):
161-
return base64.b64encode(payload).decode('ascii')
162-
151+
return payload if isinstance(payload, str) else payload.decode('utf-8')
152+
except UnicodeDecodeError as e:
153+
raise ValueError('The image payload cannot be serialized to a string. Make sure to base64-encode it first')
163154

0 commit comments

Comments
 (0)