diff --git a/appium/common/helper.py b/appium/common/helper.py new file mode 100644 index 00000000..c8372095 --- /dev/null +++ b/appium/common/helper.py @@ -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 diff --git a/appium/webdriver/webdriver.py b/appium/webdriver/webdriver.py index 1b34fa77..572a9c27 100644 --- a/appium/webdriver/webdriver.py +++ b/appium/webdriver/webdriver.py @@ -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 @@ -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): """ diff --git a/test/unit/webdriver/device/clipboard_test.py b/test/unit/webdriver/device/clipboard_test.py index 0c4656ad..5f662d57 100644 --- a/test/unit/webdriver/device/clipboard_test.py +++ b/test/unit/webdriver/device/clipboard_test.py @@ -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,