Hi everyone, I’ve set a @BeforeTest method in the test listener to login with specific users in the test cases. But after this setting, I cannot run from a specific step in an open instance. It starts from the beginning and fails. Continue executing from a specific step helps a lot for using time effectively. What should I do? Is there something that I’m missing?
Here is my Login Hook, In test case level, I specify user type with a variable.
import static com.kms.katalon.core.testcase.TestCaseFactory.findTestCase
import static com.kms.katalon.core.testdata.TestDataFactory.findTestData
import static com.kms.katalon.core.testobject.ObjectRepository.findTestObject
import com.kms.katalon.core.checkpoint.Checkpoint as Checkpoint
import com.kms.katalon.core.model.FailureHandling as FailureHandling
import com.kms.katalon.core.testcase.TestCase as TestCase
import com.kms.katalon.core.testdata.TestData as TestData
import com.kms.katalon.core.testobject.TestObject as TestObject
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI
import internal.GlobalVariable as GlobalVariable
import com.kms.katalon.core.annotation.BeforeTestCase
import com.kms.katalon.core.annotation.BeforeTestSuite
import com.kms.katalon.core.annotation.AfterTestCase
import com.kms.katalon.core.annotation.AfterTestSuite
import com.kms.katalon.core.context.TestCaseContext
import com.kms.katalon.core.context.TestSuiteContext
class LoginHook {
@BeforeTestCase
def loginUser(TestCaseContext testCaseContext) {
// Check if it's a WebUI test
if (testCaseContext.getTestCaseId().startsWith('Test Cases/WebUI')) {
// Get the user type from test case variables
def userType = testCaseContext.getTestCaseVariables().get('userType')
// Open browser and navigate to the login page
WebUI.openBrowser('')
WebUI.navigateToUrl(GlobalVariable.URL)
WebUI.click(findTestObject('Object Repository/Logining/Page_*** Login/h4_**Login'))
// Get credentials based on user type
def credentials = getUserCredentials(userType)
// Login
WebUI.setText(findTestObject('Object Repository/Logining/Page_***Login/input_email'), credentials.username)
WebUI.setEncryptedText(findTestObject('Object Repository/Logining/Page_*** Login/input_password'), credentials.password)
WebUI.click(findTestObject('Object Repository/Logining/Page_***Login/button_Log in'))
}
}
private def getUserCredentials(String userType) {
switch (userType.toLowerCase()) {
case 'globaladmin':
return [username: GlobalVariable.GAusername, password: GlobalVariable.GApassword]
case 'distributorowner':
return [username: GlobalVariable.DistributorOwner_username, password: GlobalVariable.DistributorOwner_password]
case 'distributorreader':
return [username: GlobalVariable.DistributorReader_username, password: GlobalVariable.DistributorReader_password]
case 'tenantowner':
return [username: GlobalVariable.TenantOwner_username, password: GlobalVariable.TenantOwner_password]
case 'tenanteditor':
return [username: GlobalVariable.TenantEditor_username, password: GlobalVariable.TenantEditor_password]
case 'tenantreader':
return [username: GlobalVariable.TenantReader_username, password: GlobalVariable.TenantReader_password]
case 'tenantmember':
return [username: GlobalVariable.TenantMember_username, password: GlobalVariable.TenantMember_password]
case 'devicegroupowner':
return [username: GlobalVariable.DG_owner_username, password: GlobalVariable.DG_owner_password]
case 'devicegroupreader':
return [username: GlobalVariable.DG_reader_username, password: GlobalVariable.DG_reader_password]
case 'devicegroupeditor':
return [username: GlobalVariable.DG_editor_username, password: GlobalVariable.DG_editor_password]
default:
throw new IllegalArgumentException("Invalid user type: ${userType}")
}
}
}
And here is one of example test case.Lets say If I want to execute after 2nd step, at the moment it starts from the beginning.
WebUI.setText(findTestObject('Common Objects/input_searchboxACL'), GlobalVariable.flatmeshGWmac)
WebUI.click(findTestObject('Common Objects/firstElementInTable'))
WebUI.click(findTestObject('Page_***/span_Status'))
WebUI.click(findTestObject('InfraGuard License/btn_Activate_License'))
WebUI.click(findTestObject('InfraGuard License/input_licenseKey'))
WebUI.setText(findTestObject('Object Repository/InfraGuard License/Page_***/part1_licenseKey'), GlobalVariable.expired_WOS_License)
WebUI.click(findTestObject('InfraGuard License/Page_***/button_Activate'))
WebUI.verifyTextPresent('Invalid License Key', false)
WebUI.click(findTestObject('InfraGuard License/Page_***/button_Cancel'))
WebUI.closeBrowser() ```