Skip to content

extract bytes and add a test for set clipboard #282

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 1, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions appium/common/helper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/usr/bin/env python

# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


def appium_bytes(value, encoding):
"""
Return a bytes-like object. Has _appium_ prefix to avoid overriding built-in bytes.

:param value: A value to convert
:type value: string

:param encoding: A encoding which will convert to
:type encoding: string

:return: A bytes-like object
:rtype: string
"""

try:
return bytes(value, encoding) # Python 3
except TypeError:
return value # Python 2
9 changes: 2 additions & 7 deletions appium/webdriver/webdriver.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from selenium.webdriver.remote.command import Command as RemoteCommand
from selenium.webdriver.support.ui import WebDriverWait

from appium.common.helper import appium_bytes
from appium.webdriver.clipboard_content_type import ClipboardContentType
from appium.webdriver.common.mobileby import MobileBy
from appium.webdriver.common.multi_action import MultiAction
Expand Down Expand Up @@ -1299,13 +1300,7 @@ def set_clipboard_text(self, text, label=None):
:param label: Optional label argument, which only works for Android
"""

def _bytes(value, encoding):
try:
return bytes(value, encoding) # Python 3
except TypeError:
return value # Python 2

self.set_clipboard(_bytes(str(text), 'UTF-8'), ClipboardContentType.PLAINTEXT, label)
self.set_clipboard(appium_bytes(str(text), 'UTF-8'), ClipboardContentType.PLAINTEXT, label)

def get_clipboard(self, content_type=ClipboardContentType.PLAINTEXT):
"""
Expand Down
21 changes: 20 additions & 1 deletion test/unit/webdriver/device/clipboard_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,30 @@
import json
import httpretty

from appium.webdriver.clipboard_content_type import ClipboardContentType
from appium.common.helper import appium_bytes


class TestWebDriverDeviceClipboard(object):

@httpretty.activate
def test_clipboard(self):
def test_set_clipboard_with_url(self):
driver = TestHelper.mock_android_driver()
httpretty.register_uri(
httpretty.POST,
'http://localhost:4723/wd/hub/session/1234567890/appium/device/set_clipboard',
body='{"value": ""}'
)
driver.set_clipboard(appium_bytes(str('http://appium.io/'), 'UTF-8'),
ClipboardContentType.URL, 'label for android')

d = json.loads(httpretty.last_request().body.decode('utf-8'))
assert d['content'] == 'aHR0cDovL2FwcGl1bS5pby8='
assert d['contentType'] == 'url'
assert d['label'] == 'label for android'

@httpretty.activate
def test_set_clipboard_text(self):
driver = TestHelper.mock_android_driver()
httpretty.register_uri(
httpretty.POST,
Expand Down