@testlio/cli
Version:
Official Testlio platform command-line interface
102 lines (81 loc) • 3.23 kB
JavaScript
const axios = require('axios');
const Joi = require('joi');
const fs = require('fs');
const { findDeviceResult } = require('./utils');
const FAILURE = 1;
const SUCCESS = 0;
const printHelp = () => {
console.log(`Upload asset
Usage:
testlio find-device-result [OPTIONS]
Options:
--automatedBrowserId AutomatedBrowserId, pass on to find corresponding device-result-id.
--automatedDeviceId AutomatedDeviceId, pass on to find corresponding device-result-id.
--projectConfig [path] (Optional) Path to project config. Defaults to 'project-config.json'.
Note: One of the following options must be specified: --automatedBrowserId, --automatedDeviceId.
`);
};
const setAuthorizationToken = (token) => {
axios.defaults.headers.common.Authorization = `Bearer ${token}`;
};
const setBaseUri = (baseURI) => {
axios.defaults.baseURL = baseURI;
};
const projectConfigSchema = Joi.object({
baseURI: Joi.string().required(),
resultCollectionGuid: Joi.string().required()
});
const findDeviceResultSchema = Joi.object({
automatedBrowserId: Joi.string(),
automatedDeviceId: Joi.string(),
projectConfig: Joi.string().default('project-config.json')
})
.xor('automatedBrowserId', 'automatedDeviceId')
.label('Please specify either automatedBrowserId or automatedDeviceId to find corresponding device result');
module.exports = async (params) => {
if (params.h || params.help) {
printHelp();
return;
}
const {
automatedDeviceId,
automatedBrowserId,
projectConfig: projectConfigFilePath
} = Joi.attempt(params, findDeviceResultSchema);
if (!fs.existsSync(projectConfigFilePath)) {
console.log(`File "${projectConfigFilePath}" not found!`);
return FAILURE;
}
try {
const projectConfig = JSON.parse(fs.readFileSync(projectConfigFilePath).toString());
const { baseURI, resultCollectionGuid } = Joi.attempt(projectConfig, projectConfigSchema.unknown());
if (!process.env.RESULT_ID) {
console.log('For results parsing, RESULTS_ID must be provided');
return FAILURE;
}
if (!process.env.RUN_API_TOKEN) {
console.log('Please provide RUN API TOKEN');
return FAILURE;
}
setBaseUri(baseURI);
setAuthorizationToken(process.env.RUN_API_TOKEN);
const automatedBrowserOrAutomatedDeviceId = automatedDeviceId || automatedBrowserId;
const resultType = automatedDeviceId ? 'automatedDevice' : 'automatedBrowser';
const deviceResultGuid = await findDeviceResult(
resultCollectionGuid,
automatedBrowserOrAutomatedDeviceId,
resultType,
process.env.RESULT_ID
);
if (!deviceResultGuid) {
console.log(`Device result with ${resultType} id: ${automatedBrowserOrAutomatedDeviceId} not found`);
return FAILURE;
}
console.log(`Device result found, here is the guid: ${deviceResultGuid}`);
return SUCCESS;
} catch (e) {
console.log(`Something went wrong while trying to find the device result id`, e);
return FAILURE;
}
};