|
| 1 | +import MaestroOptions from '../models/maestro_options'; |
| 2 | +import logger from '../logger'; |
| 3 | +import Credentials from '../models/credentials'; |
| 4 | +import axios from 'axios'; |
| 5 | +import fs from 'node:fs'; |
| 6 | +import path from 'node:path'; |
| 7 | +import FormData from 'form-data'; |
| 8 | +import TestingBotError from '../models/testingbot_error'; |
| 9 | +import utils from '../utils'; |
| 10 | + |
| 11 | +export default class Maestro { |
| 12 | + private readonly URL = 'https://api.testingbot.com/v1/app-automate/maestro'; |
| 13 | + private credentials: Credentials; |
| 14 | + private options: MaestroOptions; |
| 15 | + |
| 16 | + private appId: number | undefined = undefined; |
| 17 | + |
| 18 | + public constructor(credentials: Credentials, options: MaestroOptions) { |
| 19 | + this.credentials = credentials; |
| 20 | + this.options = options; |
| 21 | + } |
| 22 | + |
| 23 | + private async validate(): Promise<boolean> { |
| 24 | + if (this.options.app === undefined) { |
| 25 | + throw new TestingBotError(`app option is required`); |
| 26 | + } |
| 27 | + |
| 28 | + try { |
| 29 | + await fs.promises.access(this.options.app, fs.constants.R_OK); |
| 30 | + } catch (err) { |
| 31 | + throw new TestingBotError( |
| 32 | + `Provided app path does not exist ${this.options.app}`, |
| 33 | + ); |
| 34 | + } |
| 35 | + |
| 36 | + if (this.options.testApp === undefined) { |
| 37 | + throw new TestingBotError(`testApp option is required`); |
| 38 | + } |
| 39 | + |
| 40 | + try { |
| 41 | + await fs.promises.access(this.options.testApp, fs.constants.R_OK); |
| 42 | + } catch (err) { |
| 43 | + throw new TestingBotError( |
| 44 | + `testApp path does not exist ${this.options.testApp}`, |
| 45 | + ); |
| 46 | + } |
| 47 | + |
| 48 | + if ( |
| 49 | + this.options.device === undefined && |
| 50 | + this.options.emulator === undefined |
| 51 | + ) { |
| 52 | + throw new TestingBotError(`Please specify either a device or emulator`); |
| 53 | + } |
| 54 | + |
| 55 | + return true; |
| 56 | + } |
| 57 | + |
| 58 | + public async run() { |
| 59 | + if (!(await this.validate())) { |
| 60 | + return; |
| 61 | + } |
| 62 | + try { |
| 63 | + logger.info('Uploading Maestro App'); |
| 64 | + await this.uploadApp(); |
| 65 | + |
| 66 | + logger.info('Uploading Maestro Test App'); |
| 67 | + await this.uploadTestApp(); |
| 68 | + |
| 69 | + logger.info('Running Maestro Tests'); |
| 70 | + await this.runTests(); |
| 71 | + } catch (error: any) { |
| 72 | + logger.error(error.message); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + private async uploadApp() { |
| 77 | + const fileName = path.basename(this.options.app); |
| 78 | + const fileStream = fs.createReadStream(this.options.app); |
| 79 | + |
| 80 | + const formData = new FormData(); |
| 81 | + formData.append('file', fileStream); |
| 82 | + const response = await axios.post(`${this.URL}/app`, formData, { |
| 83 | + headers: { |
| 84 | + 'Content-Type': 'application/vnd.android.package-archive', |
| 85 | + 'Content-Disposition': `attachment; filename=${fileName}`, |
| 86 | + 'User-Agent': utils.getUserAgent(), |
| 87 | + }, |
| 88 | + auth: { |
| 89 | + username: this.credentials.userName, |
| 90 | + password: this.credentials.accessKey, |
| 91 | + }, |
| 92 | + }); |
| 93 | + |
| 94 | + const result = response.data; |
| 95 | + if (result.id) { |
| 96 | + this.appId = result.id; |
| 97 | + } else { |
| 98 | + throw new TestingBotError(`Uploading app failed: ${result.error}`); |
| 99 | + } |
| 100 | + |
| 101 | + return true; |
| 102 | + } |
| 103 | + |
| 104 | + private async uploadTestApp() { |
| 105 | + const fileName = path.basename(this.options.testApp); |
| 106 | + const fileStream = fs.createReadStream(this.options.testApp); |
| 107 | + |
| 108 | + const formData = new FormData(); |
| 109 | + formData.append('file', fileStream); |
| 110 | + const response = await axios.post( |
| 111 | + `${this.URL}/${this.appId}/tests`, |
| 112 | + formData, |
| 113 | + { |
| 114 | + headers: { |
| 115 | + 'Content-Type': 'application/zip,', |
| 116 | + 'Content-Disposition': `attachment; filename=${fileName}`, |
| 117 | + 'User-Agent': utils.getUserAgent(), |
| 118 | + }, |
| 119 | + auth: { |
| 120 | + username: this.credentials.userName, |
| 121 | + password: this.credentials.accessKey, |
| 122 | + }, |
| 123 | + }, |
| 124 | + ); |
| 125 | + |
| 126 | + const result = response.data; |
| 127 | + if (!result.id) { |
| 128 | + throw new TestingBotError(`Uploading test app failed: ${result.error}`); |
| 129 | + } |
| 130 | + |
| 131 | + return true; |
| 132 | + } |
| 133 | + |
| 134 | + private async runTests() { |
| 135 | + try { |
| 136 | + const response = await axios.post( |
| 137 | + `${this.URL}/${this.appId}/run`, |
| 138 | + { |
| 139 | + capabilities: [ |
| 140 | + { |
| 141 | + deviceName: this.options.emulator, |
| 142 | + }, |
| 143 | + ], |
| 144 | + }, |
| 145 | + { |
| 146 | + headers: { |
| 147 | + 'Content-Type': 'application/json', |
| 148 | + 'User-Agent': utils.getUserAgent(), |
| 149 | + }, |
| 150 | + auth: { |
| 151 | + username: this.credentials.userName, |
| 152 | + password: this.credentials.accessKey, |
| 153 | + }, |
| 154 | + }, |
| 155 | + ); |
| 156 | + |
| 157 | + const result = response.data; |
| 158 | + if (result.success === false) { |
| 159 | + throw new TestingBotError(`Running Maestro test failed`, { |
| 160 | + cause: result.error, |
| 161 | + }); |
| 162 | + } |
| 163 | + |
| 164 | + return true; |
| 165 | + } catch (error) { |
| 166 | + throw new TestingBotError(`Running Maestro test failed`, { |
| 167 | + cause: error, |
| 168 | + }); |
| 169 | + } |
| 170 | + } |
| 171 | +} |
0 commit comments