您可以使用 getValue() 及 setValue() 方法,將變數中的多個值儲存在一個自訂程式碼呼叫中。 然後,您可以從變數中建立替代,而不用從多個自訂程式碼元素中建立。
例如,假設回應包含三個值:id、book title 及 price。您可以從回應中讀取所有這三個值,然後使用自訂程式碼來設定 id、book title 及 price 變數。然後,如有必要,您可以在測試中,以這三個變數來替代值,而不必為每個變數撰寫自訂程式碼。
package customcode;
import com.ibm.rational.test.lt.kernel.IDataArea;
import com.ibm.rational.test.lt.kernel.services.ITestExecutionServices;
/**
* For Javadoc information on the ICustomCode2 and ITestExecutionServices interfaces,
* see the 'Extending test execution with custom code' help topic.
*/
/**
* @author IBM Custom Code Samples
*/
public String exec(ITestExecutionServices tes, String[] args) {
tes.getValue("myVar", tes.STORAGE_USER); // This retrieves a value from a test for the variable called myVar. The storage area is shared between tests.
tes.getValue("myLocalVar", tes.STORAGE_LOCAL); // This variable is stored locally, per test.
tes.setValue("myVar", tes.STORAGE_USER, "myNewValue"); // Change the value of the variable myVar, which is shared between tests, to myNewValue.
tes.setValue("myLocalVar", tes.STORAGE_LOCAL, "myLocalNewVar"); // Change the value of the local variable to myLocalNewVar.
return null;
}