The reuseIfExists flag returns an existing instance with matching labels instead of creating a new one. This is useful for development to avoid creating duplicate instances.
3
Connect to the instance
Use the WebSocket API to connect and control the device:
if (!instance.status.apiUrl) { throw new Error('API URL is missing');}const client = await Ios.createInstanceClient({ apiUrl: instance.status.apiUrl, token: instance.status.token, logLevel: 'info',});console.log('Connected to iOS instance');
4
Automate the device
Now you can interact with the iOS simulator. Let’s take a screenshot and launch Safari:
// Take a screenshotconst screenshot = await client.screenshot();console.log(`Screenshot: ${screenshot.width}x${screenshot.height}`);// Save the screenshotimport fs from 'fs';fs.writeFileSync( 'screenshot.jpg', Buffer.from(screenshot.base64, 'base64'));// Launch Safariawait client.launchApp('com.apple.mobilesafari');console.log('Launched Safari');// Open a URLawait client.openUrl('https://www.example.com');console.log('Opened example.com');// Tap at coordinatesawait client.tap(200, 400);console.log('Tapped at (200, 400)');
5
Clean up
Always disconnect when you’re done:
client.disconnect();console.log('Disconnected');
Instances automatically terminate after 3 minutes of inactivity by default. You can customize this with the inactivityTimeout spec option.
Use accessibility selectors instead of hard-coded coordinates:
// Tap a button by its accessibility labelawait client.tapElement({ elementType: 'Button', label: 'Submit' });// Set text field value (faster than typing)await client.setElementValue('[email protected]', { elementType: 'TextField', labelContains: 'Email',});