Search code examples
javaseleniumtestnggetter-setter

Selenium Java - TestNG Data Providers using Getters and Setters


I am using Getters and Setters this within a Test Case and I am parsing an array to it. I then try to reference it later from another Test Case in a dataset controller class where I will pull the values of the array. By the time execution reaches that part of the code, the value returned is always Null. I have tried many things, including making the fields static, etc.... I can't seem to find the right resolution. Below is my code for the Getters and Setters. I left the commented code in. There are three data providers. This is set in the first test case. I then try to getSubscriber_Type in the second test case. Yet it still shows a Null value. However it shows the value in the third test case. Please can someone assist me?

public class SubscriberType {
    static String[] Subscriber_Type;

/*public SubscriberType(String[] sScenarioType)
{
        this.Subscriber_Type = sScenarioType;
}*/

/* public SubscriberType()
{
Subscriber_Type = null;
}*/

public static void setSubscriber_Type(String[] sSubscriber_Type) {
    Subscriber_Type = sSubscriber_Type;
}

public String[] getSubscriber_Type() {
    return Subscriber_Type;
}

}

Below the code for setting a worksheet object contain array of simple string values:

getSubscriberBaseTest.setSubscriber_Type(workSheet);

Below is the code for getting the worksheet value:

SubscriberType = getSubscriberBaseTest.getSubscriber_Type();

Desclarations:

public static SubscriberType getSubscriberBaseTest;
public static final String[] workSheet = new String[3];

Solution

  • This is how it was resolved : dependsOnMethods = { "DatasetFromExcel" }, I used the dependsOnMethod attribute as there were some information that I needed to use later on.

    This is google explanation of this Attribute:

    dependsOnMethods : dependsOnMethods attribute on a test method [test1 e.g.] specifies all the test methods [test2, test3,..] this test method depends on. It means test1 will start execution only after all the tests it depends on executed successfully.