@labnex/cli
Version:
CLI for Labnex, an AI-Powered Testing Automation Platform
83 lines • 3.95 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.handleUpload = handleUpload;
const elementFinderV2_1 = require("../elementFinderV2"); // Updated import
const path = __importStar(require("path"));
const fs = __importStar(require("fs"));
async function handleUpload(page, currentFrame, addLog, selector, filePath, originalStep, retryApiCallFn // Added
) {
if (!page)
throw new Error('Page not available for upload');
if (!currentFrame)
throw new Error('Current frame not available for upload');
if (!selector)
throw new Error('Upload selector not provided');
if (!filePath)
throw new Error('File path not provided for upload');
const absoluteFilePath = path.resolve(filePath);
if (!fs.existsSync(absoluteFilePath)) {
throw new Error(`File not found at path: ${absoluteFilePath}`);
}
addLog(`[Upload] Attempting to upload file: ${absoluteFilePath} to element identified by: ${selector}`);
const element = await (0, elementFinderV2_1.findElementWithFallbacks)(page, currentFrame, addLog, selector, selector, originalStep, false, retryApiCallFn);
if (!element) {
throw new Error('Element not found');
}
// Ensure the element is an input type=file
const isFileInput = await element.evaluate(el => el.tagName === 'INPUT' && el.type === 'file');
if (!isFileInput) {
await element.dispose();
throw new Error(`Element identified by "${selector}" is not a file input element.`);
}
await element.uploadFile(absoluteFilePath);
await element.dispose();
addLog(`[Upload] Successfully uploaded file: ${absoluteFilePath} to element: ${selector}`);
// Validate upload success by checking for a confirmation message or element
try {
const uploadConfirmationSelector = '#uploaded-files'; // Specific to the-internet.herokuapp.com/upload
await page.waitForSelector(uploadConfirmationSelector, { timeout: 5000 });
const uploadedFileName = await page.evaluate(sel => {
const el = document.querySelector(sel);
return el && el.textContent ? el.textContent.trim() : '';
}, uploadConfirmationSelector);
addLog(`[Upload Validation] Upload confirmed. File name displayed: ${uploadedFileName}`);
}
catch (validationError) {
addLog(`[Upload Validation] Failed to confirm upload: ${validationError.message}`);
throw new Error(`Upload action performed but confirmation not found: ${validationError.message}`);
}
}
//# sourceMappingURL=handleUpload.js.map