Skip to content

Commit

Permalink
fix: make whitespace in execute methods insignificant
Browse files Browse the repository at this point in the history
How this works may change in the future, but XCUITestDriver users expect to be able to use `mobile:foo` instead of or in addition to `mobile: foo`.

Any change on the Appium side affects more than just this driver, so the change should happen here for now to fix the extant regression.

In addition, updated `package-lock.json` since it was out-of-date w/r/t the package version.  See appium/appium#18553

Closes appium/appium#18551
  • Loading branch information
boneskull committed Apr 20, 2023
1 parent 9e761d6 commit 0787517
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 9 deletions.
12 changes: 9 additions & 3 deletions lib/commands/execute.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ function executeMethodExpectsParam(script, param) {
* @returns {script is keyof XCUITestDriver.executeMethodMap}
*/
function isExecuteMethod(script) {
return script.match(/^mobile:/) && script in XCUITestDriver.executeMethodMap;
return script in XCUITestDriver.executeMethodMap;
}

/**
Expand Down Expand Up @@ -64,7 +64,9 @@ function preprocessExecuteMethodArgs(script, args) {
* Most of these Execute Methods (typically beginning with `mobile*`) will accept an `Element|string` for `elementId`, in practice they will only ever get a `string`. `Element|string` in the method's docstring is simply for documentation purposes.
*/
if ('elementId' in executeMethodArgs && executeMethodExpectsParam(script, 'elementId')) {
executeMethodArgs.elementId = util.unwrapElement(/** @type {import('@appium/types').Element|string} */(executeMethodArgs.elementId));
executeMethodArgs.elementId = util.unwrapElement(
/** @type {import('@appium/types').Element|string} */ (executeMethodArgs.elementId)
);
}

return executeMethodArgs;
Expand Down Expand Up @@ -111,11 +113,15 @@ export default {
*/
async execute(script, args) {
// TODO: create a type that converts args to the parameters of the associated method using the `command` prop of `executeMethodMap`
// this replaces "mobile:foo" with "mobile: foo"
if (/^mobile:\S/.test(script)) {
script = script.replace(/^mobile:/, 'mobile: ');
}
if (isExecuteMethod(script)) {
const executeMethodArgs = preprocessExecuteMethodArgs(script, args);
return await this.executeMethod(script, [executeMethodArgs]);
} else if (this.isWebContext()) {
const atomsArgs = this.convertElementsForAtoms(/** @type {readonly any[]} */(args));
const atomsArgs = this.convertElementsForAtoms(/** @type {readonly any[]} */ (args));
const result = await this.executeAtom('execute_script', [script, atomsArgs]);
return this.cacheWebElements(result);
} else {
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 21 additions & 4 deletions test/unit/driver-specs.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ import chai from 'chai';
import * as utils from '../../lib/utils';
import {MOCHA_LONG_TIMEOUT} from './helpers';
import sinonChai from 'sinon-chai';
import chaiAsPromised from 'chai-as-promised';
chai.should();
chai.use(sinonChai);
chai.use(sinonChai).use(chaiAsPromised);

const expect = chai.expect;

const caps = {
Expand Down Expand Up @@ -114,8 +116,10 @@ describe('XCUITestDriver', function () {
setReduceTransparency: _.noop,
};
realDevice = null;
// eslint-disable-next-line require-await
sandbox.stub(driver, 'determineDevice').callsFake(async () => ({device, realDevice, udid: 'stuff'}));
sandbox
.stub(driver, 'determineDevice')
// eslint-disable-next-line require-await
.callsFake(async () => ({device, realDevice, udid: 'stuff'}));
sandbox.stub(driver, 'configureApp');
sandbox.stub(driver, 'startLogCapture');
sandbox.stub(driver, 'startSim');
Expand Down Expand Up @@ -191,13 +195,26 @@ describe('XCUITestDriver', function () {
spy.notCalled.should.be.true;
});
});

describe('execute', function () {
it('should allow execute methods without whitespace', async function () {
const driver = new XCUITestDriver();
const jwproxy = new JWProxy();
const value = {some: 'thing'};
sandbox.stub(jwproxy, 'command').resolves(value);
driver.wda = {
jwproxy,
};
await expect(driver.execute('mobile:deviceInfo')).to.eventually.eql(value);
});
});
});

describe('installOtherApps', function () {
/** @type {XCUITestDriver} */
let driver;

beforeEach(function() {
beforeEach(function () {
driver = new XCUITestDriver();
});

Expand Down

0 comments on commit 0787517

Please sign in to comment.