How can I keep the variable alive across the project globally?



  • I have a BaseClass.java which extends PageObject.java class i.e. net.serenitybdd.core.pages.PageObject;

    Then I have two Page classes LoginPage.java and ProfilePage.java, these two also extending the same PageObject.java class

    I have a function in BaseClass.java:

    public String createTestUser() {
            testUser = "test" + getTimeStamp() + "@demo.com";
            return testUser;
        }
    

    Then I have function in LoginPage.java class where I test login feature using above created user, like this

    public void doLogin() {
    username.type(baseClass.createTestUser());
    password.type("test123");
    submitBtn.click();
      }
    

    Now I have to verify whether correct user is logged in or not so I have a function in ProfilePage.java class

    public boolean VerifySuccessfulLogin(){
       return verifyWebElementXpath("//div[contains(text(),'"+baseClass.createTestUser()+"')]");
    }
    

    This test failed. To fix this I tried:

    I created a variable and function in LoginPage.java as

    public String userForTesting = null;
    
    public String getTestUser(){
        if(userForTesting==null ){
        userForTesting = baseClass.createTestUser();
        return userForTesting;
    }
    else {
        return userForTesting;
        }
    }
    

    Now I changed boolean function in ProfilePage.java as

    public boolean VerifySuccessfulLogin(){
       return verifyWebElementXpath("//div[contains(text(),'"+loginPage.getTestUser()+"')]");
    }
    

    Still it is failing. It is generating new user for this boolean function. I am new to Java I have got few responses here https://stackoverflow.com/questions/52953005/how-to-pass-values-in-java-from-one-class-to-another but none worked.

    What am I doing wrong? The userForTesting variable stays alive within LoginPage.java but after that it becomes null and so boolean function always gets newly created user.

    Update:

    public class BaseClass extends PageObject {

    private static BaseClass baseClassFinder = null;
    

    public static BaseClass getInstance() { if (baseClassFinder== null) baseClassFinder= new BaseClass(); return baseClassFinder; }

    }

    This is giving me error:

    java.lang.StackOverflowError
        at java.lang.reflect.Executable.synthesizeAllParams(Executable.java:363)
        at java.lang.reflect.Executable.privateGetParameters(Executable.java:415)
        at java.lang.reflect.Executable.getParameters(Executable.java:359)
        at net.thucydides.core.pages.Pages.lambda$hasDefaultConstructor$0(Pages.java:251)
        at java.util.stream.MatchOps$1MatchSink.accept(MatchOps.java:90)
        at java.util.Spliterators$ArraySpliterator.tryAdvance(Spliterators.java:958)
        at java.util.stream.ReferencePipeline.forEachWithCancel(ReferencePipeline.java:126)
        at java.util.stream.AbstractPipeline.copyIntoWithCancel(AbstractPipeline.java:498)
        at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:485)
        at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
        at java.util.stream.MatchOps$MatchOp.evaluateSequential(MatchOps.java:230)
        at java.util.stream.MatchOps$MatchOp.evaluateSequential(MatchOps.java:196)
        at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
        at java.util.stream.ReferencePipeline.anyMatch(ReferencePipeline.java:449)
        at net.thucydides.core.pages.Pages.hasDefaultConstructor(Pages.java:251)
        at net.thucydides.core.pages.Pages.createFromSimpleConstructor(Pages.java:232)
        at net.thucydides.core.pages.Pages.getCurrentPageOfType(Pages.java:204)
        at net.thucydides.core.pages.Pages.getPage(Pages.java:90)
        at net.thucydides.core.steps.PageObjectDependencyInjector.instantiatePageObjectIfNotAssigned(PageObjectDependencyInjector.java:55)
        at net.thucydides.core.steps.PageObjectDependencyInjector.injectDependenciesInto(PageObjectDependencyInjector.java:33)
        at net.thucydides.core.steps.PageObjectDependencyInjector.instantiatePageObjectIfNotAssigned(PageObjectDependencyInjector.java:56)
    


  • You can have a static class called TestData, which will hold the test user.

    public final class TestData {
    
        @Getter
        private static String testUser;
    
        public static void resetTestUser() {
            this.testUser = String.format("test%s@demo.com", getTimeStamp());
        }
    }
    

    And on BaseClass, you can have a @BeforeSuite method:

    @BeforeSuite
    public static void creatingUser() {
            TestData.resetTestUser();
    }
    

    This way, you can access the test user anywhere with TestData.getTestUser(); And if you need to reset it during the suite, you can call TestData.resetTestUser();

    OBS: @Getter comes from Lombok.



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2