83 lines
2.5 KiB
JavaScript
83 lines
2.5 KiB
JavaScript
const http = require('http');
|
|
|
|
const HOST = process.env.APPIUM_HOST || '127.0.0.1';
|
|
const PORT = parseInt(process.env.APPIUM_PORT || '4723', 10);
|
|
const TIMEOUT = 30000;
|
|
|
|
function request(method, path, body) {
|
|
return new Promise((resolve, reject) => {
|
|
const opts = {
|
|
hostname: HOST, port: PORT, path, method,
|
|
headers: body ? { 'Content-Type': 'application/json' } : {},
|
|
timeout: TIMEOUT,
|
|
};
|
|
const req = http.request(opts, (res) => {
|
|
let data = '';
|
|
res.on('data', (chunk) => { data += chunk; });
|
|
res.on('end', () => {
|
|
try {
|
|
const json = JSON.parse(data);
|
|
const val = json.value !== undefined ? json.value : json;
|
|
if (val && val.error) {
|
|
reject(new Error(val.message || val.error));
|
|
} else {
|
|
resolve(json);
|
|
}
|
|
} catch (e) {
|
|
reject(new Error(`Response parse error: ${data.slice(0, 300)}`));
|
|
}
|
|
});
|
|
});
|
|
req.on('error', reject);
|
|
req.on('timeout', () => { req.destroy(); reject(new Error('Request timed out')); });
|
|
if (body) req.write(JSON.stringify(body));
|
|
req.end();
|
|
});
|
|
}
|
|
|
|
function sleep(ms) {
|
|
return new Promise((r) => setTimeout(r, ms));
|
|
}
|
|
|
|
async function main() {
|
|
console.log(`Connecting to Appium at ${HOST}:${PORT}...`);
|
|
|
|
const caps = {
|
|
platformName: 'Android',
|
|
'appium:automationName': 'UiAutomator2',
|
|
'appium:deviceName': 'Android',
|
|
'appium:noReset': true,
|
|
};
|
|
|
|
console.log('Creating session...');
|
|
const createRes = await request('POST', '/session', {
|
|
capabilities: { firstMatch: [caps] },
|
|
});
|
|
const sid = (createRes.value && createRes.value.sessionId) || createRes.sessionId;
|
|
if (!sid) throw new Error(`No sessionId in response: ${JSON.stringify(createRes)}`);
|
|
console.log(` sessionId: ${sid}`);
|
|
|
|
console.log('Taking screenshot...');
|
|
await sleep(1000);
|
|
const ssRes = await request('GET', `/session/${sid}/screenshot`);
|
|
const ssLen = ssRes.value ? ssRes.value.length : 0;
|
|
console.log(` screenshot: ${ssLen} bytes`);
|
|
|
|
console.log('Quering current package...');
|
|
try {
|
|
const pkgRes = await request('GET', `/session/${sid}/appium/device/current_package`);
|
|
console.log(` current package: ${pkgRes.value}`);
|
|
} catch (e) {
|
|
console.log(` (could not get package: ${e.message})`);
|
|
}
|
|
|
|
console.log('Deleting session...');
|
|
await request('DELETE', `/session/${sid}`);
|
|
|
|
console.log('\nSmoke test PASSED');
|
|
}
|
|
|
|
main().catch((err) => {
|
|
console.error(`\nSmoke test FAILED: ${err.message}`);
|
|
process.exit(1);
|
|
});
|