简体   繁体   中英

How to handle session timeout with selenium

I looked at some answers for similar question but could not find satisfied answer.

I am building automation framework where I am running 200 test cases and it takes time about 60 minutes to complete.

Application has session time out of 30 minutes so while my test cases are running, it logs out after 30 minutes and then test cases getting failed because it could not find required elements.

What Login I am thinking to handle session timeout :

  1. Call login method again in test case number # X and check if logout then login again But this seems not good idea as if tomorrow session timeout time updated from 30 minutes to something else, I will have to call login method in some other test case.

2.If Current time - Test start time >= 30 minute s then check if system logout and if yes then login again. but for this question is same like I have to call it in either every method or in some specific methods to check every time if logout or not.

So I am looking for some solution where it checks constantly from start of my automation that if logout window found then call login method.

Note : I am using selenium webdriver, Java, TestNG, Maven and following page object pattern.

So I'm assuming your login page has a different url than any of your pages inside the actual application. If you don't already have a method with the testng @BeforeTest you could create one and include in your new or existing beforetest method the following:

if (driver.getCurrentUrl() == "whatever.yourLoginPageUrl.is") {
    //call login method or do whatever you have to do to login

    //If you want to rerun the previous test that probably failed, do that here
}

A slightly slicker way (I think) to do this would be to implement the IRetryAnalyzer interface and override the retry function, for example like this

public class MyRetry implements IRetryAnalyzer {

@Override
public boolean retry(ITestResult result) {
    if (driver.getCurrentUrl() == "loginPageUrl") {
        //call login function or do whatever you need to to login 
        return true;
    }
    return false;
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM