Skip to content

Commit 62cc0bf

Browse files
committed
refact: _adjust_image_payload
1 parent 6225fd6 commit 62cc0bf

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

appium/webdriver/extensions/images_comparison.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!/usr/bin/env python
2-
2+
import binascii
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.
55
# You may obtain a copy of the License at
@@ -14,6 +14,8 @@
1414

1515
from typing import Any, Dict, Union
1616

17+
import base64
18+
1719
from appium.protocols.webdriver.can_execute_commands import CanExecuteCommands
1820

1921
from ..mobilecommand import MobileCommand as Command
@@ -147,4 +149,13 @@ def _add_commands(self) -> None:
147149

148150

149151
def _adjust_image_payload(payload: Base64Payload) -> str:
150-
return payload if isinstance(payload, str) else payload.decode('utf-8')
152+
if isinstance(payload, str):
153+
return payload
154+
try:
155+
b64_str = payload.decode('ascii')
156+
base64.b64decode(payload,validate=True)
157+
return b64_str
158+
except (UnicodeDecodeError, binascii.Error):
159+
return base64.b64encode(payload).decode('ascii')
160+
161+

0 commit comments

Comments
 (0)