|
7 | 7 | fileExists, |
8 | 8 | isWin, |
9 | 9 | executeCommand, |
10 | | - findFreePort |
| 10 | + findFreePort, |
| 11 | + getRegexResultsAsArray |
11 | 12 | } from "./utils"; |
12 | 13 | import { INsCapabilities } from "./interfaces/ns-capabilities"; |
13 | 14 | import { IDeviceManager } from "./interfaces/device-manager"; |
@@ -57,40 +58,9 @@ export class AppiumServer { |
57 | 58 | } |
58 | 59 |
|
59 | 60 | public async start(port, deviceManager: IDeviceManager = new DeviceManger()) { |
60 | | - this._deviceManager = deviceManager; |
61 | | - if (!this._args.device) { |
62 | | - const device = await this._deviceManager.startDevice(this._args); |
63 | | - this._args.device = device; |
64 | | - } |
65 | | - |
66 | | - if (this._args.devMode) { |
67 | | - const appPackage = this._args.isAndroid ? "appPackage" : "bundleId"; |
68 | | - const appFullPath = this._args.appiumCaps.app; |
69 | | - |
70 | | - if (appFullPath && !this._args.appiumCaps[appPackage]) { |
71 | | - console.log(`Trying to resolve automatically ${appPackage}!`); |
72 | | - this._args.appiumCaps[appPackage] = this._deviceManager.getPackageId(this._args.device, appFullPath); |
73 | | - console.log(`Setting capabilities ${this._args.runType}{ "${appPackage}" : "${this._args.appiumCaps[appPackage]}" }!`); |
74 | | - } |
75 | | - |
76 | | - if (!this._args.appiumCaps[appPackage]) { |
77 | | - throw new Error(`In order to use reuse app functionality, please set ${appPackage} in ${this._args.appiumCapsLocation} file!`); |
78 | | - } |
| 61 | + await this.prepareDevice(deviceManager); |
| 62 | + await this.prepareApp(); |
79 | 63 |
|
80 | | - this._args.appiumCaps.app = ""; |
81 | | - } |
82 | | - |
83 | | - if (this._args.isAndroid && (!this._args.appiumCaps['appActivity'] || this._args.appiumCaps['appActivity'].trim() === "")) { |
84 | | - if (fileExists(this._args.appPath)) { |
85 | | - this._args.appiumCaps['appActivity'] = AndroidController.getLaunchableActivity(this._args.appPath); |
86 | | - console.log(`Setting capabilities ${this._args.runType}{ "appActivity" : "${this._args.appiumCaps['appActivity']}" }!`); |
87 | | - } else { |
88 | | - console.error(`No launchable activity found. You should set it here ${this._args.appiumCapsLocation} in runType: ${this._args.runType}!`); |
89 | | - } |
90 | | - } |
91 | | - if (!this._args.devMode) { |
92 | | - this._deviceManager.installApp(this._args); |
93 | | - } |
94 | 64 | log("Starting server...", this._args.verbose); |
95 | 65 | const logLevel = this._args.verbose === true ? "debug" : "info"; |
96 | 66 | this.port = port || this._args.port; |
@@ -162,6 +132,39 @@ export class AppiumServer { |
162 | 132 | }); |
163 | 133 | } |
164 | 134 |
|
| 135 | + private async prepareDevice(deviceManager: IDeviceManager) { |
| 136 | + this._deviceManager = deviceManager; |
| 137 | + if (!this._args.device) { |
| 138 | + const device = await this._deviceManager.startDevice(this._args); |
| 139 | + this._args.device = device; |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + private async prepareApp() { |
| 144 | + const appPackage = this._args.isAndroid ? "appActivity" : "bundleId"; |
| 145 | + const appFullPath = this._args.appiumCaps.app; |
| 146 | + |
| 147 | + if (appFullPath && !this._args.appiumCaps[appPackage]) { |
| 148 | + console.log(`Trying to resolve automatically ${appPackage}!`); |
| 149 | + this._args.appiumCaps[appPackage] = this._deviceManager.getPackageId(this._args.device, appFullPath); |
| 150 | + console.log(`Setting capabilities ${this._args.runType}{ "${appPackage}" : "${this._args.appiumCaps[appPackage]}" }!`); |
| 151 | + } |
| 152 | + |
| 153 | + if (!this._args.appiumCaps[appPackage]) { |
| 154 | + throw new Error(`Please, provide ${appPackage} in ${this._args.appiumCapsLocation} file!`); |
| 155 | + } |
| 156 | + |
| 157 | + const groupings = getRegexResultsAsArray(/(\w+)/gi, this._args.appiumCaps[appPackage]); |
| 158 | + this._args.appName = groupings[groupings.length - 1]; |
| 159 | + console.log(`Setting application name as ${this._args.appName}`); |
| 160 | + if (!this._args.devMode) { |
| 161 | + await this._deviceManager.uninstallApp(this._args); |
| 162 | + } else { |
| 163 | + this._args.appiumCaps.app = ""; |
| 164 | + } |
| 165 | + |
| 166 | + } |
| 167 | + |
165 | 168 | // Resolve appium dependency |
166 | 169 | private resolveAppiumDependency() { |
167 | 170 | const projectDir = this._args.projectDir; |
|
0 commit comments