Here the code snippet to access DD variables and manipulate the values of those variable in Java code.
1) Simple variables:
If you have the simple variable in project variables list as "name" then here is the ways to get that variable value in Java code.
//First way
IVariable way1ToGet_Name = objSCESession.getVariable("name");
String strWay1ToGetName = way1ToGet_Name.getSimpleVariable().getStringValue();
//Second way
IVariable way2ToGet_Name = objSCESession.getVariable(IProjectVariables.NAME);
String strWay2ToGetName = way2ToGet_Name.getSimpleVariable().getStringValue();
//Third way
IVariableField way3ToGet_Name = objSCESession.getVariable(IProjectVariables.NAME).getSimpleVariable();
String getstrWay3ToGetNameName = way3ToGet_Name.getStringValue();
//Fourth way
String getstrWay4ToGetNameName = objSCESession.getVariableField("name").getStringValue();
//Fifth way
String getstrWay5ToGetNameName = objSCESession.getVariableField(IProjectVariables.NAME).getStringValue();
Note: Here there is a drawback of using the First and Fourth way, if we these ways then later in your project variables list if you change the variable name then it won't show any error and at runtime it will give you the problem.
So better go for other ways(other than First and Fourth). If you change the variable name in project variables list then it will show you some errors at compile time itself. So that you can figure out where exactly you have used that variable and where you need to change.
The above is to get the DD variable value in Java code and assign that value to local java variables.
If you want to set the DD variable value then you have to use setValue("") method.
The following line shows you how to set the String value to DD variable.
objSCESession.getVariableField(IProjectVariables.NAME).setValue("Ramu");
(or)
objSCESession.getVariableField(IProjectVariables.NAME).setValue(strName); //Java String type variable
2) Simple Collection Variables:(To store Array of values to DD variable)
IVariable mySimpleVar = objSCESession.getVariable(IProjectVariables.ARRAY_OF_PERSONS);
mySimpleVar.setCollection(new SimpleCollection(mySimpleVar.getSimpleVariable(), IProjectVariables.ARRAY_OF_PERSONS));
ICollection mySimpleCollectionVar = mySimpleVar.getCollection();
//To Clear the collection
mySimpleVar.getCollection().removeAll();
mySimpleCollectionVar.removeAll();
//To Reset the collection
mySimpleVar.getCollection().reset();
mySimpleCollectionVar.reset();
3) Complex Variables:
Here I have declared one complex variable called "person" with fields "age" and "name". To access these values use the following code snippet.
IComplexVariable complexVar = objSCESession.getVariable(IProjectVariables.PERSON).getComplexVariable();
IVariableField complexVarField1 = complexVar.getField(IProjectVariables.PERSON_FIELD_AGE);
IVariableField complexVarField2 = complexVar.getField(IProjectVariables.PERSON_FIELD_NAME);
complexVarField1.getIntValue();
complexVarField2.getStringValue();
To Set the values to complex fields use the following code snippet.
complexVarField1.setValue(28); //Set the age as 28
complexVarField2.setValue("Ramu"); //Set the name as Ramu
4) Complex Collection variables:
IComplexVariable complexVar = objSCESession.getVariable(IProjectVariables.PERSON).getComplexVariable();
IVariableField complexVarField1 = complexVar.getField(IProjectVariables.PERSON_FIELD_AGE);
IVariableField complexVarField2 = complexVar.getField(IProjectVariables.PERSON_FIELD_NAME);
complexVarField1.getIntValue();
complexVarField2.getStringValue();
complexVarField1.setValue(28); //Set the age as 28
complexVarField2.setValue("Ramu"); //Set the name as Ramu
IVariable myComplex = objSCESession.getVariable(IProjectVariables.PERSON);
myComplex.setCollection(new ComplexCollection(myComplex.getComplexVariable(), IProjectVariables.PERSON));
ICollection myComplexCollection = myComplex.getCollection();
//To Reset the complex collection
myComplexCollection.reset();
//To Remove the complex collection values and make it empty
myComplexCollection.removeAll();
IVariableField complexCollectionField1_Age = myComplex.getComplexVariable().getField(IProjectVariables.PERSON_FIELD_AGE);
IVariableField complexCollectionField2_Name = myComplex.getComplexVariable().getField(IProjectVariables.PERSON_FIELD_NAME);
//Set first record values
complexCollectionField1_Age.setValue(28);
complexCollectionField1_Age.setValue("Ramu");
myComplexCollection.append();
//Set Second record values
complexCollectionField1_Age.setValue(28);
complexCollectionField1_Age.setValue("Srikanth");
myComplex.getCollection().append();
//Reset the collection to travarse the collection values
myComplexCollection.reset();
while (myComplexCollection.hasMore())
{
myComplexCollection.next();
//Log the record values
TraceInfo.trace(ITraceInfo.TRACE_LEVEL_DEBUG, ("Person Age : " + complexCollectionField1_Age.getIntValue()), objSCESession);
TraceInfo.trace(ITraceInfo.TRACE_LEVEL_DEBUG, ("Person Name : " + complexCollectionField2_Name.getStringValue()), objSCESession);
}
//Reset the collection to travarse the collection values
myComplexCollection.reset();
Note: If you write your own POJO class then you need to pass SCESession object as parameter to the methods in POJO. Because, all the DD variables are stored in SCESession object.
No comments:
Post a Comment