Skip to content

Commit

Permalink
fix(appium): Load plugins in the same order that was used for the CLI…
Browse files Browse the repository at this point in the history
… command (#19388)
  • Loading branch information
mykola-mokhnach committed Nov 10, 2023
1 parent 5ae8df3 commit 7124eb6
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 50 deletions.
28 changes: 22 additions & 6 deletions packages/appium/lib/extension/index.js
Expand Up @@ -38,14 +38,30 @@ export async function loadExtensions(appiumHome) {
* @returns {PluginNameMap} Mapping of PluginClass to name
*/
export function getActivePlugins(pluginConfig, usePlugins = []) {
/** @type {string[]} */
let filteredPluginNames = [];
if (usePlugins.length === 1 && usePlugins[0] === USE_ALL_PLUGINS) {
filteredPluginNames = _.keys(pluginConfig.installedExtensions);
} else {
// It is important to load plugins in the same order that was used while enumerating them
for (const pluginName of usePlugins) {
if (pluginName in pluginConfig.installedExtensions) {
filteredPluginNames.push(pluginName);
} else if (pluginName === USE_ALL_PLUGINS) {
throw new Error(
`The reserved plugin name '${pluginName}' cannot be combined with other names.`
);
} else {
throw new Error(
`Could not load the plugin '${pluginName}' because it is not installed. ` +
`Only the following plugins are available: ${_.keys(pluginConfig.installedExtensions)}`
);
}
}
}
return new Map(
_.compact(
Object.keys(pluginConfig.installedExtensions)
.filter(
(pluginName) =>
_.includes(usePlugins, pluginName) ||
(usePlugins.length === 1 && usePlugins[0] === USE_ALL_PLUGINS)
)
filteredPluginNames
.map((pluginName) => {
try {
log.info(`Attempting to load plugin ${pluginName}...`);
Expand Down
56 changes: 12 additions & 44 deletions packages/appium/test/e2e/plugin.e2e.spec.js
Expand Up @@ -107,55 +107,23 @@ describe('FakePlugin w/ FakeDriver via HTTP', function () {
});

describe('without plugin registered', function () {
/** @type {AppiumServer} */
let server;

before(async function () {
it('should reject server creation if plugin is not activated', async function () {
const args = {
appiumHome,
port,
address: TEST_HOST,
usePlugins: ['other1', 'other2'],
};
server = /** @type {AppiumServer} */ (await appiumServer(args));
});

after(async function () {
if (server) {
await server.close();
}
});

it('should not update the server if plugin is not activated', async function () {
await axios.post(`http://${TEST_HOST}:${port}/fake`).should.eventually.be.rejectedWith(/404/);
});
it('should not update method map if plugin is not activated', async function () {
const driver = await wdio(wdOpts);
const {sessionId} = driver;
try {
await axios
.post(`${testServerBaseSessionUrl}/${sessionId}/fake_data`, {
data: {fake: 'data'},
})
.should.eventually.be.rejectedWith(/404/);
} finally {
await driver.deleteSession();
}
await appiumServer(args).should.eventually.be.rejected;
});
it('should not handle commands if plugin is not activated', async function () {
const driver = await wdio(wdOpts);
const {sessionId} = driver;
try {
const el = (
await axios.post(`${testServerBaseSessionUrl}/${sessionId}/element`, {
using: 'xpath',
value: '//MockWebView',
})
).data.value;
el.should.not.have.property('fake');
} finally {
await driver.deleteSession();
}
it('should reject server creation if reserved plugin name is provided with other names', async function () {
const args = {
appiumHome,
port,
address: TEST_HOST,
usePlugins: ['fake', 'all'],
};
await appiumServer(args).should.eventually.be.rejected;
});
});

Expand All @@ -167,7 +135,7 @@ describe('FakePlugin w/ FakeDriver via HTTP', function () {
let usePlugins;
before(async function () {
// then start server if we need to
usePlugins = registrationType === 'explicit' ? ['fake', 'p2', 'p3'] : ['all'];
usePlugins = registrationType === 'explicit' ? ['fake'] : ['all'];
const args = {
appiumHome,
port,
Expand Down Expand Up @@ -358,7 +326,7 @@ describe('FakePlugin w/ FakeDriver via HTTP', function () {
/** @type {AppiumServer} */
let server;

/** @type {import('webdriverio').Browser<'async'>} */
/** @type {import('webdriverio').Browser} */
let driver;

before(async function () {
Expand Down

0 comments on commit 7124eb6

Please sign in to comment.