Search code examples
javasession-variablesui-automation

Why does session variable throwing a null exception?


I have page objects classes which I used for test scripts. I usually assign these classes sessions to variables as I need to use them in multiple places. I'm getting null exception in some test scripts while running the code. The same script sometime getting passed without showing an error too. Why does session variable throwing a null exception?

TestCommonLandingPage.java

public class TestCommonLandingPage extends PageObject{
   public TestCommonLandingPage(Session session) {
        super(session);        
    }

   public TestCommonLandingPage stepOpenWindowByShortcut() throws FrameworkException {
        this.test_step_initiation();
        this.TestCommonLandingPagePanel.action_perform_shortcuts_window();
        log.info("Step: OpenWindowByShortcut");
        return this;
    }
}

TestScript.java

public class TC1614 extends VerificationsWidget {
    public void viewingWidgetParameters() throws FrameworkException, InterruptedException {
        final TestCommonLandingPage testCommonLandingPage = new TestCommonLandingPage (getSession()); 

testCommonLandingPage
                .stepOpenWindowByShortcut());

The code("testCommonLandingPage.stepOpenWindowByShortcut());") most of the time throws a null exception, why is that? When I'm using session without assign to a variable this is working properly as well


Solution

  • I was declared all the variables at the beginning of the class. This happened when user switch between windows(according to the script steps). When directing session was not set to the variable as there was some delay all the time. If you face same problem as this, check whether there is any loading delay between system windows and declare the variables once it is fully loaded.