UNPKG

appwright

Version:

E2E mobile app testing done right, with the Playwright test runner

35 lines (34 loc) 1.48 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.uploadImageToBS = uploadImageToBS; const fs_1 = __importDefault(require("fs")); const form_data_1 = __importDefault(require("form-data")); function getAuthHeader() { const userName = process.env.BROWSERSTACK_USERNAME; const accessKey = process.env.BROWSERSTACK_ACCESS_KEY; const key = Buffer.from(`${userName}:${accessKey}`).toString("base64"); return `Basic ${key}`; } async function uploadImageToBS(imagePath) { const formData = new form_data_1.default(); if (!fs_1.default.existsSync(imagePath)) { throw new Error(`No image file found at the specified path: ${imagePath}. Please provide a valid image file. Supported formats include JPG, JPEG, and PNG. Ensure the file exists and the path is correct.`); } formData.append("file", fs_1.default.createReadStream(imagePath)); formData.append("custom_id", "SampleMedia"); const fetch = (await import("node-fetch")).default; const response = await fetch("https://api-cloud.browserstack.com/app-automate/upload-media", { method: "POST", headers: { Authorization: getAuthHeader(), }, body: formData, }); const data = (await response.json()); const imageURL = data.media_url.trim(); return imageURL; }