|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import unittest |
| 16 | +import httpretty |
| 17 | +import json |
| 18 | + |
| 19 | +from appium import webdriver |
| 20 | + |
| 21 | + |
| 22 | +class TestHelper(): |
| 23 | + |
| 24 | + @staticmethod |
| 25 | + def mock_android_driver(): |
| 26 | + """ |
| 27 | + Return a driver which is generated a mock response |
| 28 | +
|
| 29 | + :return: An instance of WebDriver |
| 30 | + :rtype: WebDriver |
| 31 | + """ |
| 32 | + |
| 33 | + response_body_json = json.dumps( |
| 34 | + { |
| 35 | + 'value': { |
| 36 | + 'sessionId': '1234567890', |
| 37 | + 'capabilities': { |
| 38 | + 'platform': 'LINUX', |
| 39 | + 'desired': { |
| 40 | + 'platformName': 'Android', |
| 41 | + 'automationName': 'uiautomator2', |
| 42 | + 'platformVersion': '7.1.1', |
| 43 | + 'deviceName': 'Android Emulator', |
| 44 | + 'app': '/test/apps/ApiDemos-debug.apk', |
| 45 | + }, |
| 46 | + 'platformName': 'Android', |
| 47 | + 'automationName': 'uiautomator2', |
| 48 | + 'platformVersion': '7.1.1', |
| 49 | + 'deviceName': 'emulator-5554', |
| 50 | + 'app': '/test/apps/ApiDemos-debug.apk', |
| 51 | + 'deviceUDID': 'emulator-5554', |
| 52 | + 'appPackage': 'com.example.android.apis', |
| 53 | + 'appWaitPackage': 'com.example.android.apis', |
| 54 | + 'appActivity': 'com.example.android.apis.ApiDemos', |
| 55 | + 'appWaitActivity': 'com.example.android.apis.ApiDemos' |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + ) |
| 60 | + |
| 61 | + httpretty.register_uri( |
| 62 | + httpretty.POST, |
| 63 | + 'http://localhost:4723/wd/hub/session', |
| 64 | + body=response_body_json |
| 65 | + ) |
| 66 | + |
| 67 | + desired_caps = { |
| 68 | + 'platformName': 'Android', |
| 69 | + 'deviceName': 'Android Emulator', |
| 70 | + 'app': 'path/to/app', |
| 71 | + 'automationName': 'UIAutomator2' |
| 72 | + } |
| 73 | + driver = webdriver.Remote( |
| 74 | + 'http://localhost:4723/wd/hub', |
| 75 | + desired_caps |
| 76 | + ) |
| 77 | + return driver |
0 commit comments