简体   繁体   中英

Create an object to share data between multiple TestNG classes

I have created following relationship with object

public class Parent {
  //Making API calls to get some data which can be used
  //across multiple test classes
}

Then I'm inherting the Test classes as

public class Child1 extends Parent {
 //I need data collected in Parent class
}

Again I have second class as

public class Child2 extends Parent {
 //I also need the same  data collected in Parent class
}

My TestNG suite is as given below

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="e2e-suite" verbose="1">

    <test name="e2e tests" preserve-order="true">
        <classes>
            <class name="com.abc.test.Child1" />
            <class name="com.abc.test.Child2" />
        </classes>
    </test>
</suite>

But , when I trigger the test suites , my Parent class API data collection logic get invoked properly, I also get data that is direct available to first class which consumes it ie Child1

But, for Child2 class , the Parent class logic isn't get invoked again (even I don't want it to execute again) . The question is how the data that I collected in Parent class can be shared & persistent across other Child objects like Child2 .

Currently, I'm using static Parent class Fields to share data. I'm looking for some Implementation which can allow me to store data from Parent class to another object which can be used across all Child Test objects (If any other better way to share data collected in parent class to later test classes is also fine for me)

//Making API calls to get some data which can be used //across multiple test classes

private String config1;
private String config2;

public Parent() {
this.instance = getInstance();
}

private Parent(String config1, String config2) {
this.config1 = config1;
this.config2 = config2;

}

private static Parent instance = null;

public static Parent getInstance() {
if (instance == null) {
    synchronized (Parent.class) {
    if (instance == null) {
        instance = new Parent("SingleTonConfig1", "SingleTonConfig2");
    }
    }
}
return instance;
}

public String getConfig1() {
return config1;
}

public void setConfig1(String config1) {
this.config1 = config1;
}

public String getConfig2() {
return config2;
}

public void setConfig2(String config2) {
this.config2 = config2;
}

}

import com.demo.config.Parent;

public class Child1 extends Parent {

private String Child1Config1;
private String Child1Config2;

public Child1(String child1Config1, String child1Config2) {
super();
Child1Config1 = child1Config1;
Child1Config2 = child1Config2;
}

}

public class Child2 extends Parent {

private String Child2Config1;
private String Child2Config2;

public Child2(String child2Config1, String child2Config2) {
super();
Child2Config1 = child2Config1;
Child2Config2 = child2Config2;
}

}

public static void main(String[] args) {
Child1 c1 = new Child1("child1Config1", "child1Config2");
Child1 c11 = new Child1("child1Config11", "child1Config22");

Child1 c2 = new Child1("child2Config1", "child2Config2");
Child1 c22 = new Child1("child2Config11", "child2Config22");

System.out.println("Parent object c1- config1" + c1.getInstance().getConfig1());

System.out.println("Parent object c1- config2" + c1.getInstance().getConfig2());

System.out.println("Parent object c11- config1" + c11.getInstance().getConfig1());

System.out.println("Parent object c11- config2" + c11.getInstance().getConfig2());

System.out.println("Parent object c2- config1" + c2.getInstance().getConfig1());

System.out.println("Parent object c2- config2" + c2.getInstance().getConfig2());

System.out.println("Parent object c22- config1" + c22.getInstance().getConfig1());

System.out.println("Parent object c22- config2" + c22.getInstance().getConfig2());

System.out.println("Parent Object c1- parentInstance: " + c1.getInstance());

System.out.println("Parent Object c11- parentInstance: " + c11.getInstance());

System.out.println("Parent Object c2- parentInstance: " + c2.getInstance());

System.out.println("Parent Object c22- parentInstance: " + c22.getInstance());
}

}

When you will run the above main method you will see that the object always remains same since its singleton. But we have to give access to one public constructor where we will instantiate the object. There are different approaches on how you can instantiate the instance like eager initialization ,static block initialization, and lazy initialization.

So, here is explanation of my test suite. I have exact below situation which is helping me a lot

TestNg's @BeforeTest on base class only happening once per fixture

public class TestBase {
    @BeforeTest
    public void before() {
        System.out.println("BeforeTest");
    }
}

public class TestClass extends TestBase {
    @Test
    public void test1(){}

    @Test
    public void test2(){}
}

public class TestClass1 extends TestBase {
    @Test
    public void test3(){}

    @Test
    public void test4(){}
}

Here, @BeforeTest playing a role of @BeforeSuite for me and I also want that particular piece to execute only once in lifetime of my suite. first problem solved.

Second Problem: to share the data between multiple Test classes. I got the solution here https://stackoverflow.com/a/38792029/1665592

Set value:

@Test
public void setvaluetest(ITestContext context) {
        String customerId = "GDFg34fDF";
        context.setAttribute("customerId", customerId);
}

Get value:

@Test
public void getvaluetest(ITestContext context) {
        String id = context.getAttribute("customerId");
}

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