Skip to content

Instantly share code, notes, and snippets.

@jpcercal
Created February 21, 2024 12:05
Show Gist options
  • Save jpcercal/b9bedbbdb4244f01e18f6047a90fa9f9 to your computer and use it in GitHub Desktop.
Save jpcercal/b9bedbbdb4244f01e18f6047a90fa9f9 to your computer and use it in GitHub Desktop.
Use it to clear the sessions started and left open by selenium grid hub when terminating the executing un-gracefully
async function clearSessions() {
try {
const url = 'http://127.0.0.1:4444';
// Get information about all active sessions on the Selenium Grid hub
const response = await fetch(`${url}/status`);
const data = await response.json();
// Loop through all active sessions and terminate them
for (const node of data.value.nodes) {
for (const slot of node.slots) {
if (slot.session) {
const sessionId = slot.session.sessionId;
// Send a DELETE request to the session endpoint to terminate the session
const deleteResponse = await fetch(`${url}/session/${sessionId}`, { method: 'DELETE' });
const deleteResult = await deleteResponse.json();
console.log(`Terminated session ${sessionId}: ${deleteResult.value}`);
}
}
}
console.log(`Terminated all sessions`);
} catch (error) {
console.error(error);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment