What you are experiencing is a classic automation challenge related to Session Expiry or Unstable Environments.
When a web application automatically logs you out, the DOM (the underlying HTML structure) completely changes. Your script is hardcoded to look for Elements A, B, and C, but it is suddenly staring at the Login fields.
The Architectural Fix
To handle this gracefully and keep your tests scalable, we need to implement a Conditional State Check. Instead of assuming the user is always logged in, we will create a Custom Keyword that checks for a “Session Indicator” (like a sidebar, a user profile icon, or a dashboard heading). If that indicator disappears, the script will automatically handle the re-login sequence before attempting the next step.
Implementation: The Custom Keyword
We will create a Custom Keyword called checkSessionAndRecover. You can call this keyword periodically during long tests, or wrap your critical actions inside it.
Step 1: Create the Custom Keyword
In Katalon Studio, go to Keywords (in the Object Repository sidebar) → New → Keyword. Name the package com.recovery and the class SessionHandler.
Paste the following Groovy code:
Groovy
package com.recovery
import com.kms.katalon.core.annotation.Keyword
import com.kms.katalon.core.model.FailureHandling
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI
import com.kms.katalon.core.testobject.TestObject
public class SessionHandler {
/**
* Checks if the user is still logged in. If logged out, handles re-login.
* @param sessionElement The Test Object that proves you are logged in (e.g., Logout Button, Profile Icon)
* @param loginMethod A closure/block of code containing your login steps
*/
@Keyword
def checkSessionAndRecover(TestObject sessionElement, Closure loginMethod) {
// Check if the session element is present within a quick 3-second window
// We use FailureHandling.OPTIONAL so it doesn't crash the test if missing
boolean isTemplatePresent = WebUI.waitForElementPresent(sessionElement, 3, FailureHandling.OPTIONAL)
if (!isTemplatePresent) {
WebUI.comment("Session loss detected! Initiating recovery login sequence...")
// Execute the login steps passed to this keyword
loginMethod.call()
WebUI.comment("Recovery successful. Resuming test execution.")
} else {
WebUI.comment("Session is valid. Proceeding with test.")
}
}
}
Step 2: How to use it in your Test Case (Script View)
In your actual Test Case, you can now gracefully handle the flakey logout like this:
Groovy
import static com.kms.katalon.core.testobject.ObjectRepository.findTestObject
// 1. Define your "Session Indicator" (an element only visible when logged in)
def dashboardElement = findTestObject('Object Repository/MainPage/btn_Logout')
// 2. Before doing a long action, check the session. If it's gone, it runs the code inside the brackets {}
CustomKeywords.'com.recovery.SessionHandler.checkSessionAndRecover'(dashboardElement, {
WebUI.navigateToUrl('https://your-app.com/login')
WebUI.setText(findTestObject('Object Repository/LoginPage/input_Username'), 'your_user')
WebUI.setText(findTestObject('Object Repository/LoginPage/input_Password'), 'your_password')
WebUI.click(findTestObject('Object Repository/LoginPage/btn_Submit'))
})
// 3. Continue your normal test steps safely
WebUI.setText(findTestObject('Object Repository/FormPage/input_FirstName'), 'John')
Why this works beautifully:
-
Non-Blocking Check: By using FailureHandling.OPTIONAL, Katalon won’t panic if it doesn’t find your logged-in dashboard element. It simply returns false and moves to the if block.
-
Encapsulated Logic: The Closure loginMethod allows you to pass your entire login flow as a variable inside the keyword, keeping your recovery logic dynamic and clean.
-
No Waste of Time: It only waits a maximum of 3 seconds to confirm your session, meaning your test execution stays fast.
Generlly it is better if you post your codebase and community provide fix relevent to that. let me know if this work