|
12 | 12 | # See the License for the specific language governing permissions and |
13 | 13 | # limitations under the License. |
14 | 14 |
|
15 | | -import unittest |
16 | | -import httpretty |
17 | 15 | import json |
| 16 | +import httpretty |
18 | 17 |
|
19 | 18 | from appium import webdriver |
20 | 19 |
|
| 20 | +# :return: A string of test URL |
| 21 | +SERVER_URL_BASE = 'http://localhost:4723/wd/hub' |
| 22 | + |
| 23 | + |
| 24 | +def appium_command(command): |
| 25 | + """ |
| 26 | + Return a command of Appium |
| 27 | +
|
| 28 | + :return: A string of command URL |
| 29 | + "rtype: string |
| 30 | + """ |
| 31 | + return '{}{}'.format(SERVER_URL_BASE, command) |
| 32 | + |
21 | 33 |
|
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 | | - }, |
| 34 | +def android_w3c_driver(): |
| 35 | + """ |
| 36 | + Return a W3C driver which is generated by a mock response for Android |
| 37 | +
|
| 38 | + :return: An instance of WebDriver |
| 39 | + :rtype: WebDriver |
| 40 | + """ |
| 41 | + |
| 42 | + response_body_json = json.dumps( |
| 43 | + { |
| 44 | + 'value': { |
| 45 | + 'sessionId': '1234567890', |
| 46 | + 'capabilities': { |
| 47 | + 'platform': 'LINUX', |
| 48 | + 'desired': { |
46 | 49 | 'platformName': 'Android', |
47 | 50 | 'automationName': 'uiautomator2', |
48 | 51 | 'platformVersion': '7.1.1', |
49 | | - 'deviceName': 'emulator-5554', |
| 52 | + 'deviceName': 'Android Emulator', |
50 | 53 | '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 | | - } |
| 54 | + }, |
| 55 | + 'platformName': 'Android', |
| 56 | + 'automationName': 'uiautomator2', |
| 57 | + 'platformVersion': '7.1.1', |
| 58 | + 'deviceName': 'emulator-5554', |
| 59 | + 'app': '/test/apps/ApiDemos-debug.apk', |
| 60 | + 'deviceUDID': 'emulator-5554', |
| 61 | + 'appPackage': 'com.example.android.apis', |
| 62 | + 'appWaitPackage': 'com.example.android.apis', |
| 63 | + 'appActivity': 'com.example.android.apis.ApiDemos', |
| 64 | + 'appWaitActivity': 'com.example.android.apis.ApiDemos' |
57 | 65 | } |
58 | 66 | } |
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 | 67 | } |
73 | | - driver = webdriver.Remote( |
74 | | - 'http://localhost:4723/wd/hub', |
75 | | - desired_caps |
76 | | - ) |
77 | | - return driver |
| 68 | + ) |
| 69 | + |
| 70 | + httpretty.register_uri( |
| 71 | + httpretty.POST, |
| 72 | + appium_command('/session'), |
| 73 | + body=response_body_json |
| 74 | + ) |
| 75 | + |
| 76 | + desired_caps = { |
| 77 | + 'platformName': 'Android', |
| 78 | + 'deviceName': 'Android Emulator', |
| 79 | + 'app': 'path/to/app', |
| 80 | + 'automationName': 'UIAutomator2' |
| 81 | + } |
| 82 | + |
| 83 | + driver = webdriver.Remote( |
| 84 | + SERVER_URL_BASE, |
| 85 | + desired_caps |
| 86 | + ) |
| 87 | + return driver |
| 88 | + |
| 89 | + |
| 90 | +def ios_w3c_driver(): |
| 91 | + """ |
| 92 | + Return a W3C driver which is generated by a mock response for iOS |
| 93 | +
|
| 94 | + :return: An instance of WebDriver |
| 95 | + :rtype: WebDriver |
| 96 | + """ |
| 97 | + |
| 98 | + response_body_json = json.dumps( |
| 99 | + { |
| 100 | + 'value': { |
| 101 | + 'sessionId': '1234567890', |
| 102 | + 'capabilities': { |
| 103 | + 'device': 'iphone', |
| 104 | + 'browserName': 'UICatalog', |
| 105 | + 'sdkVersion': '11.4', |
| 106 | + 'CFBundleIdentifier': 'com.example.apple-samplecode.UICatalog' |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + ) |
| 111 | + |
| 112 | + httpretty.register_uri( |
| 113 | + httpretty.POST, |
| 114 | + appium_command('/session'), |
| 115 | + body=response_body_json |
| 116 | + ) |
| 117 | + |
| 118 | + desired_caps = { |
| 119 | + 'platformName': 'iOS', |
| 120 | + 'deviceName': 'iPhone Simulator', |
| 121 | + 'app': 'path/to/app', |
| 122 | + 'automationName': 'XCUITest' |
| 123 | + } |
| 124 | + |
| 125 | + driver = webdriver.Remote( |
| 126 | + SERVER_URL_BASE, |
| 127 | + desired_caps |
| 128 | + ) |
| 129 | + return driver |
0 commit comments