Skip to content

fix base64 encoded string#231

Merged
KazuCocoa merged 1 commit intoappium:masterfrom
KazuCocoa:fix-encode-string
Jul 13, 2018
Merged

fix base64 encoded string#231
KazuCocoa merged 1 commit intoappium:masterfrom
KazuCocoa:fix-encode-string

Conversation

@KazuCocoa
Copy link
Copy Markdown
Member

Fix: function find_element_by_image always raise an exception: "TypeError: Object of type 'bytes' is not JSON serializable" #228

encodestring generate byte data in Python 3. Thus, we should encode it to string.

Python2.7

>>> import base64
>>> base64.encodestring(b'hello')
'aGVsbG8=\n'

Python3.6

>>> import base64
>>> base64.encodestring(b'hello')
b'aGVsbG8=\n'

b64_data should be base64 encoded string.
This way is as same as https://github.com/SeleniumHQ/selenium/blob/6b5fe890da7d1592693d1dfcf795d882fc0426c5/py/selenium/webdriver/firefox/firefox_profile.py#L180 way.

screenshot = self.get_screenshot_as_base64()
with open(png_img_path, 'rb') as png_file:
b64_data = base64.encodestring(png_file.read())
b64_data = base64.b64encode(png_file.read()).decode('UTF-8')
Copy link
Copy Markdown
Member Author

@KazuCocoa KazuCocoa Jul 13, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could call json.dump(b64_data) without exception in Python2/3

with open('/Users/kazu/GitHub/python-client/sample.JPG', 'rb') as f:
    b64_data = base64. b64encode(f.read()).decode('UTF-8')

json.dumps(b64_data)

@KazuCocoa KazuCocoa merged commit c6f58bc into appium:master Jul 13, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants