0

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)
  • why would you pollute your "Base class" just to create a random string('createTestUser')? – Vishal Aggarwal Oct 25 '18 at 0:22
  • SInce everypage class is extending the baseclass, it will be easier to fetch testdata. – Aditi Apte Oct 25 '18 at 9:11
  • Yes, I see your point but I would prefer composition over inheritance to create an data object. – Vishal Aggarwal Oct 25 '18 at 9:15
  • I prefer to distinguish between data and functionality. – Vishal Aggarwal Oct 25 '18 at 9:16
  • Yes, I see your point but I would prefer composition over inheritance to create an data object. can you quote an example for better understanding. – Aditi Apte Oct 25 '18 at 9:20
1

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.

0

Everytime you are making a call to getTestUser or createTestUser, it creates a new String.

I would suggest share the created test user across the test classes where you need the user. You might think of doing so by the TestClass's instance.

Below is a link of the same by Krishnan. A lot of times I refer this article.

https://rationaleemotions.wordpress.com/2014/02/13/sharing-data-across-tests-in-different-testclasses/

Hope this solves the issue.

  • I have added my findings after testing you suggestion. – Aditi Apte Oct 24 '18 at 7:30
  • You have to use ITestContext in the test. Please have a look at the Consumer class. – Kshetra Mohan Prusty Oct 24 '18 at 8:28
  • I am using Junit with Jbehave. – Aditi Apte Oct 24 '18 at 9:55
  • I am not sure if junit has ITestContext. If you are not creating a lot of users at runtime, you can also think of having real usernames coming from some data provider and can access the same data in all the classes. – Kshetra Mohan Prusty Oct 24 '18 at 10:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.