-
-
Notifications
You must be signed in to change notification settings - Fork 24
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
feat: add webDriverAgentMacUrl #41
Changes from all commits
73e5b0d
d6c524a
f4b06ec
4b2f2d8
d979188
799ac19
85919ab
b188c8d
5712287
07276ce
38a2925
dd66d78
083f45f
d5516a0
5c3cd14
537c6cb
438e57d
7fbad39
a7a31c8
a8119d3
5b2d81a
59b45fb
391e260
721ca47
9640b44
2130a05
a846d80
548eb51
49c4f95
4c7573d
ca9dda0
ab16e4b
7b6c390
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
import _ from 'lodash'; | ||
import path from 'path'; | ||
import url from 'url'; | ||
import { JWProxy, errors } from 'appium-base-driver'; | ||
import { fs, logger, util, timing, mkdirp } from 'appium-support'; | ||
import { SubProcess, exec } from 'teen_process'; | ||
|
@@ -279,13 +280,12 @@ class WDAMacProcess { | |
|
||
class WDAMacServer { | ||
constructor () { | ||
this.process = new WDAMacProcess(); | ||
this.process = null; | ||
this.serverStartupTimeoutMs = STARTUP_TIMEOUT_MS; | ||
this.proxy = null; | ||
} | ||
|
||
get isRunning () { | ||
return !!(this.process?.isRunning); | ||
// To handle if the WDAMac server is proxying requests to a remote WDAMac app instance | ||
this.isProxyingToRemoteServer = false; | ||
} | ||
|
||
async isProxyReady (throwOnExit = true) { | ||
|
@@ -304,21 +304,91 @@ class WDAMacServer { | |
} | ||
} | ||
|
||
|
||
/** | ||
* @typedef {Object} ProxyProperties | ||
* | ||
* @property {string} scheme - The scheme proxy to. | ||
* @property {string} host - The host name proxy to. | ||
* @property {number} port - The port number proxy to. | ||
* @property {string} path - The path proxy to. | ||
*/ | ||
|
||
/** | ||
* Returns proxy information where WDAMacServer proxy to. | ||
* | ||
* @param {Object} caps - The capabilities in the session. | ||
* @return {ProxyProperties} | ||
* @throws Error if 'webDriverAgentMacUrl' had invalid URL | ||
*/ | ||
parseProxyProperties (caps) { | ||
let scheme = 'http'; | ||
if (!caps.webDriverAgentMacUrl) { | ||
return { | ||
scheme, | ||
server: (this.process?.host ?? caps.systemHost) ?? DEFAULT_SYSTEM_HOST, | ||
port: (this.process?.port ?? caps.systemPort) ?? DEFAULT_SYSTEM_PORT, | ||
path: '' | ||
}; | ||
} | ||
|
||
let parsed_url; | ||
try { | ||
parsed_url = new url.URL(caps.webDriverAgentMacUrl); | ||
} catch (e) { | ||
throw new Error(`webDriverAgentMacUrl, '${caps.webDriverAgentMacUrl}', ` + | ||
`in the capabilities is invalid. ${e.message}`); | ||
} | ||
|
||
const { protocol, hostname, port, pathname } = parsed_url; | ||
if (_.isString(protocol)) { | ||
scheme = protocol.split(':')[0]; | ||
} | ||
return { | ||
scheme, | ||
server: hostname ?? DEFAULT_SYSTEM_HOST, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would say we should throw an exception if hostname cannot be parsed There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same about protocol There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will do this later There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I did this raising an exception in https://github.com/appium/appium-mac2-driver/pull/41/files#diff-cb3604d19b5cec159d0260eb8b8739d21bf074cf54584fd2b555e9969caaa92fR335-R341 (url.URL) |
||
port: _.isEmpty(port) ? DEFAULT_SYSTEM_PORT : _.parseInt(port), | ||
path: pathname === '/' ? '' : pathname | ||
}; | ||
} | ||
|
||
async startSession (caps) { | ||
this.serverStartupTimeoutMs = caps.serverStartupTimeout ?? this.serverStartupTimeoutMs; | ||
const wasProcessInitNecessary = await this.process.init(caps); | ||
|
||
if (wasProcessInitNecessary || !(await this.isProxyReady(false))) { | ||
this.isProxyingToRemoteServer = !!caps.webDriverAgentMacUrl; | ||
|
||
let wasProcessInitNecessary; | ||
if (this.isProxyingToRemoteServer) { | ||
KazuCocoa marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (this.process) { | ||
await this.process.kill(); | ||
await cleanupObsoleteProcesses(); | ||
this.process = null; | ||
} | ||
KazuCocoa marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
wasProcessInitNecessary = false; | ||
} else { | ||
if (!this.process) { | ||
this.process = new WDAMacProcess(); | ||
} | ||
wasProcessInitNecessary = await this.process.init(caps); | ||
} | ||
|
||
if (wasProcessInitNecessary || this.isProxyingToRemoteServer || !this.proxy) { | ||
const {scheme, server, port, path} = this.parseProxyProperties(caps); | ||
this.proxy = new WDAMacProxy({ | ||
mykola-mokhnach marked this conversation as resolved.
Show resolved
Hide resolved
|
||
server: DEFAULT_SYSTEM_HOST, | ||
port: this.process.port, | ||
base: '', | ||
scheme, | ||
server, | ||
port, | ||
base: path, | ||
keepAlive: true, | ||
}); | ||
this.proxy.didProcessExit = false; | ||
this.process.proc.on('exit', () => { | ||
this.proxy.didProcessExit = true; | ||
}); | ||
|
||
if (this.process) { | ||
this.process.proc.on('exit', () => { | ||
this.proxy.didProcessExit = true; | ||
}); | ||
} | ||
|
||
const timer = new timing.Timer().start(); | ||
try { | ||
|
@@ -327,22 +397,29 @@ class WDAMacServer { | |
intervalMs: 1000, | ||
}); | ||
} catch (e) { | ||
if (this.process.isRunning) { | ||
if (this.process?.isRunning) { | ||
// avoid "frozen" processes, | ||
await this.process.kill(); | ||
} | ||
if (/Condition unmet/.test(e.message)) { | ||
throw new Error(`Mac2Driver server is not listening within ${this.serverStartupTimeoutMs}ms timeout. ` + | ||
const msg = this.isProxyingToRemoteServer | ||
? `No response from '${scheme}://${server}:${port}${path}' within ${this.serverStartupTimeoutMs}ms timeout.` + | ||
`Please make sure the remote server is running and accessible by Appium` | ||
: `Mac2Driver server is not listening within ${this.serverStartupTimeoutMs}ms timeout. ` + | ||
`Try to increase the value of 'serverStartupTimeout' capability, check the server logs ` + | ||
`and make sure the ${XCODEBUILD} host process could be started manually from a terminal`); | ||
`and make sure the ${XCODEBUILD} host process could be started manually from a terminal`; | ||
throw new Error(msg); | ||
} | ||
mykola-mokhnach marked this conversation as resolved.
Show resolved
Hide resolved
|
||
throw e; | ||
} | ||
const pid = this.process.pid; | ||
const childrenPids = await this.process.listChildrenPids(); | ||
RUNNING_PROCESS_IDS.push(...childrenPids, pid); | ||
this.process.proc.on('exit', () => void _.pull(RUNNING_PROCESS_IDS, pid)); | ||
log.info(`The host process is ready within ${timer.getDuration().asMilliSeconds.toFixed(0)}ms`); | ||
|
||
if (this.process) { | ||
const pid = this.process.pid; | ||
const childrenPids = await this.process.listChildrenPids(); | ||
RUNNING_PROCESS_IDS.push(...childrenPids, pid); | ||
this.process.proc.on('exit', () => void _.pull(RUNNING_PROCESS_IDS, pid)); | ||
log.info(`The host process is ready within ${timer.getDuration().asMilliSeconds.toFixed(0)}ms`); | ||
} | ||
} else { | ||
log.info('The host process has already been listening. Proceeding with session creation'); | ||
} | ||
|
@@ -356,7 +433,7 @@ class WDAMacServer { | |
} | ||
|
||
async stopSession () { | ||
if (!this.isRunning) { | ||
if (!this.isProxyingToRemoteServer && !(this.process?.isRunning)) { | ||
KazuCocoa marked this conversation as resolved.
Show resolved
Hide resolved
|
||
log.info(`Mac2Driver session cannot be stopped, because the server is not running`); | ||
return; | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import chai from 'chai'; | ||
import chaiAsPromised from 'chai-as-promised'; | ||
import WDA_MAC_SERVER from '../../lib/wda-mac'; | ||
|
||
chai.use(chaiAsPromised); | ||
|
||
describe('WDAMacServer', function () { | ||
describe('parseProxyProperties', function () { | ||
it('should default', function () { | ||
WDA_MAC_SERVER.parseProxyProperties({}).should.eql( | ||
{scheme: 'http', server: '127.0.0.1', port: 10100, path: ''} | ||
); | ||
}); | ||
|
||
it('should follow WebDriverAgentMacUrl', function () { | ||
WDA_MAC_SERVER.parseProxyProperties({ webDriverAgentMacUrl: 'http://customhost:9999' }).should.eql( | ||
{scheme: 'http', server: 'customhost', port: 9999, path: ''} | ||
); | ||
}); | ||
|
||
it('should follow WebDriverAgentMacUrl with custom path', function () { | ||
WDA_MAC_SERVER.parseProxyProperties({ webDriverAgentMacUrl: 'https://customhost/path' }).should.eql( | ||
{scheme: 'https', server: 'customhost', port: 10100, path: '/path'} | ||
); | ||
}); | ||
|
||
it('should follow WebDriverAgentMacUrl with invalid url', function () { | ||
try { | ||
WDA_MAC_SERVER.parseProxyProperties({ webDriverAgentMacUrl: 'invalid url' }); | ||
} catch (e) { | ||
e.message.should.contain('is invalid'); | ||
} | ||
}); | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
parsedUrl
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oops...