Rational Functional Tester Java jTree relative references

4885 ワード

Problem(Abstract)
The recording in IBM® Rational® Functional Tester of a Java™ jTree has a complication. This technote demonstrates a way to use relative paths to access jTree objects.
Symptom
The recording generates the following code.
 
jTree().doubleClick(atPath("F3 - Clients->HUIZINGA, JOHAN->Client Details"));      ​

If the item of HUIZINGA, JOHAN changes, the playback fails.
 
Resolving the problem
The Java method getTreeHierarchy converts the above line of code to the following code.
 
jTree().doubleClick(atPath(getTreeHierarchy(j​Tree(),​​ "Client Details")));​

This code puts the same path as above and does not consider the full hierarchy. The hierarchy can be dynamic. The following code helps you to search a tree for the parent hierarchy.
public String getTreeHierarchy(TestObject treeName, String searchTree) { ​
​/**​ ​  * Description : Search a tree for the parent hierarchy.​ ​  *   We need to use this within atPath statements with trees​ ​  *   in the situation where the parents may be DYNAMIC.​ ​  *   For example, if you had -> Licence where of course​ ​  *   Client Name is going to change, you simply call​ ​  *   getTreeHierarchy(jTree(), "Licence") - or similar -​ ​  *   and it works out the parent(s). In this case you don't​ ​  *   have to worry.​ ​  *   This is reproducible, because RFT does not allow you to​ ​  *   say click on a "child"node, you must specify the full​ ​  *   path.​ ​  * ​ ​  * Parameters  : treeName   - the tree which you want to search ​ ​  *    you will need () at the end of the tree​ ​  *   searchTree - the node you are looking for​ ​  * ​ ​  * Returns     : Full path of tree as a string.​ ​  * ​ ​  */​ ​ ​//Declare variables for tree​ ​ ITestDataTree  treeToSearch;​ ​ ITestDataTreeNodes  treeToSearchTreeNodes;​ ​ ITestDataTreeNode[] treeToSearchSpecificNode;​ ​ String foundHierarchy = "";​ ​ ​       //Variables to hold tree data​ ​ treeToSearch = (ITestDataTree) treeName.getTestData("tree");​ ​ treeToSearchTreeNodes = treeToSearch.getTreeNodes();​ ​ treeToSearchSpecificNode =treeToSearchTreeNodes.getRootNodes();​ ​ ​//Iterate through tree branches; this is a recursive method.​ ​ for ( int i = 0; i < treeToSearchSpecificNode.length;++i )​ ​ {​ ​ foundHierarchy = searchTree(treeToSearchSpecificNode[i], searchTree, "");​ ​ if (foundHierarchy != "")​ ​  break;​ ​ }​ ​ ​ ​ return foundHierarchy;​ ​ }​ ​ ​ private String searchTree(ITestDataTreeNode node, String searchText, String hierarchy) {​ ​/**​ ​  * Description : Searches the tree for a specific node​ ​  *               and calls itself recursively to traverse the tree.​ ​  *  Called by getTreeHierarchy.​ ​  * ​ ​  * Parameters  : node     - the tree node to traverse​ ​  *    searchText - the node to look for​ ​  *    hierarchy - current parent hierarchy above this node ​ ​  * ​ ​  * Returns     : The tree hierarchy that has been found.​ ​  * ​ ​  */​ ​ ​ String nodeSeparator = "->";​ ​ String searchResult  = "";​ ​ ​ ​//If we are at the top level of the hierarchy, don't prepend nodeSeparator​ ​ if (hierarchy == "")​ ​ hierarchy = node.getNode().toString();​ ​ else​ ​  hierarchy += nodeSeparator + node.getNode().toString();​ ​ ​//If we have found our target Node record the current hierarchy ​ ​  if (node.getNode().toString().startsWith(searchT​ext))​​ {​ ​  searchResult = hierarchy;​ ​  }​ ​  else​ ​  {​ ​ //only bother doing this if we havent found our target Node​ ​//Determine if node has children and if so,​ ​//recursively call this same method to print out child nodes.​ ​  ITestDataTreeNode[] children = node.getChildren();​ ​ int childCount = ( children != null ? children.length : 0 );​ ​ ​ ​  for ( int i = 0; i < childCount;++i ) ​ ​  {​ ​  searchResult = searchTree(children[i], searchText, hierarchy);​ ​ ​ if (searchResult != "")​ ​  break;​ ​ }​ ​ }​ ​ ​ ​ return searchResult;​ ​ }​ ​
COMMENT Alternatively you can use equalsIgnoreCase in searchTree if startsWith fails. DISCLAIMER: All source code and/or binaries attached to this document are referred to here as "the Program". IBM is not providing program services of any kind for the Program. IBM is providing the Program on an "AS IS"basis without warranty of any kind. IBM WILL NOT BE LIABLE FOR ANY ACTUAL, DIRECT, SPECIAL, INCIDENTAL, OR INDIRECT DAMAGES OR FOR ANY ECONOMIC CONSEQUENTIAL DAMAGES (INCLUDING LOST PROFITS OR SAVINGS), EVEN IF IBM, OR ITS RESELLER, HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.