How to handle Test Listeners based on the Test Case or Test Suite execution

My configuration when test execution is through the Test suite level,
BeforeTestSuite
def login(){
}
AfterTestSuite
def logout(){
}

Again configuration when test execution is through Test case level,
BeforeTestCase
def login(){
}
AfterTestCase
def logout(){
}

Every time I’m updating the configuration. Instead of this, is there any alternative configuration to handle test listeners without updating every time.

I would recommend you to make it simple.

Just remove @BeforeTestSuite and @AfterTestSuite; and let @BeforeTestCase and @AterTestCase be as is. And you stop editting your TestListener code at all. Your Test Cases and Test Suite should run ok.

When you run a Test Suite, you will see the test repeats login&logout as often as the number of your test cases contained in the test suite. It may look redundant. But would you really mind it?

If you stick to minimizing the number of login/logout processing, you can do something like the following psuedo code …

@BeforeTestCase
def loginIfNotYet(){
    if (you are not yet login) {
        login()
    }
}
@AfterTestCase
def logoutIfNotYet(){
    if (you are not yet logout) {
        logout()
    }
}

Your problem is reformed: how can a TestListener tell if you are logged in or logged out? It depends on your AUT design.

1 Like

@kazurayam
Thanks for your reply.
I want the login method should run only one time in start of the execution & logout method should run only one time at end of the execution.
The execution can be either Test Case or Test suite level but only one time both the methods should execute.
So for this configuration I need the Test Listeners should be in the respective execution control.
So kindly let me know another alternative solution.

I do not know.

Hey, @Harinath, I think the solution @kazurayam suggests is general enough, because if you can tell the TestListener if you are already logged in or not yet, then you can direct the execution flow whenever you want, not just for the specific case of once before and after TestCase or TestSuite execution.

May I suggest try to set a GlobalVariable.logged_in to false initially, and then when you’re done logging in, you just set the variable to true. Would this solution work for you ?

@ThanhTo

My issue is if I’m executing Testcase level with the below config, execution fails
BeforeTestSuite
def login(){
}
AfterTestSuite
def logout(){
}

Similarly, if I’m executing at Test Suite level with the below config, login and logout methods executes for every test case
BeforeTestCase
def login(){
}
AfterTestCase
def logout(){
}

But I want the Listeners to execute only one time for any kind of execution i.e., either TestSuite level or TestCase level…

As @kazurayam suggested, just delete (or comment out) the BeforeTestSuite and AfterTestSuite and listeners will execute only before and after test case.

I want to execute the login and logout method only one time either it could be TestCase level or TestSuite level execution.
Kindly provide the solution.

@Harinath I think I understand your requirement. It’s a fun puzzle to solve!

I think you need something like your code plus a global variable.

First, in your profile create a global variable SUITEID and set it to empty string ""

@BeforeTestSuite
def login(TestSuiteContext testSuiteContext) {
    GlobalVariable.SUITEID = testSuiteContext.getTestSuiteId()
  }
}
@AfterTestSuite
def logout(){
}

@BeforeTestCase
def checkLogin(){
  if(!GlobalVariable.SUITEID) {
    login()
  }
}
@AfterTestCase
def checkLogout(){
  if(!GlobalVariable.SUITEID) {
    logout()
  }
}

Something like the above should do what you need.

2 Likes

It’s been a long day… and I think I see a hole but I’m so tired I can’t think straight.

I think you need to login in the @BeforeTestCase method…

I’ll edit/update the code if needed.

@kazurayam can you see what I’m talking about?

How to find the status info if I am logged into the AUT or not?

I think the best way is to make some query or search onto the web page and find the status — check if my avator photo is displayed or not for example.

But a poor man’s solution would be possible. Let your TestListener “memember the status”. The following code shows what I mean.

class MyTestListener {
    private boolean LOGGED_IN = false
    def login() {
        // do login to the AUT
        LOGGED_IN = true
        WebUI.comment("******** status LOGGED_IN turned to be ${LOGGED_IN} ********")  
    }
    def logout() {
        // do logout to the AUT
        LOGGED_IN = false
        WebUI.comment("******** status LOGGED_IN truned to be ${LOGGED_IN} ********")
    }
    @BeforeTestSuite
    def doBeforeTestSuite(TestSuiteContext testSuiteContext) {
        login()
    }
    @AfterTestSuite
    def doAfterTestSuite(TestSuiteContext testSuiteContext) {
        logout(){
    }
    @BeforeTestCase
    def doBeforeTestCase(TestCaseContext testCaseContext) {
        if (!LOGGED_IN) {
            login()
        }
    }
    @AfterTestCase
    def doAfterTestCase(TestCaseContext testCaseContext) {
        if (LOGGED_IN) {
            logout()
    }
}
1 Like

Thanks for helping me out…
Got resolved by adding the flags.

So, how exactly you resolved the case?