import webdriverio from 'webdriverio';
import GboxSDK from "gbox-sdk";
const gboxSDK = new GboxSDK({
apiKey: process.env["GBOX_API_KEY"] // This is the default and can be omitted
});
async function main() {
const box = await gboxSDK.create({ type: "android" });
const { url, defaultOption } = await box.appiumURL();
console.log("Appium connection URL:", url);
// Connect to Appium with defaultOption from backend
console.log("Connecting to Appium server...");
const ac = await remote(defaultOption);
console.log("✅ Successfully connected to Appium server");
console.log("Session ID:", ac.sessionId);
try {
// Get current page XML layout
console.log("Fetching XML layout...");
const xmlLayout = await ac.getPageSource();
console.log("✅ XML layout fetched successfully!");
console.log("XML length:", xmlLayout.length, "characters");
// Display a preview of the XML (first 500 characters)
console.log("\n--- XML Layout Preview (first 500 chars) ---");
console.log(xmlLayout.substring(0, 500) + "...\n");
// Save XML to file for easier viewing
const outputDir = path.join(__dirname, "output");
if (!fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir, { recursive: true });
}
const timestamp = new Date().toISOString().replace(/[:.]/g, "-");
const filename = `layout_${timestamp}.xml`;
const filepath = path.join(outputDir, filename);
fs.writeFileSync(filepath, xmlLayout, "utf-8");
console.log(`📁 XML layout saved to: ${filepath}`);
// Optional: Parse and display some basic info
const elementMatches = xmlLayout.match(/<[\w.-]+/g);
if (elementMatches) {
const elementSet = new Set(elementMatches.map(e => e.substring(1)));
const uniqueElements = Array.from(elementSet);
console.log("\n--- UI Elements Found ---");
console.log("Total elements:", elementMatches.length);
console.log("Unique element types:", uniqueElements.length);
console.log("Element types:", uniqueElements.slice(0, 10).join(", "), "...");
}
} catch (error) {
console.error(
"❌ Error fetching XML layout:",
error instanceof Error ? error.message : String(error)
);
} finally {
console.log("\nClosing session...");
await ac.deleteSession();
console.log("Session closed.");
}
}
main()