I am trying to expand a GWT tree using selenium and i did successfully with that code
for (int i = 0; i < elem.size(); i++) {
if (!elem.get(i).isExpanded() || !elem.get(i).isLeaf()) {
elem.get(i).toggleExpand();
}
for (int j = 0; j < elem.get(i).getChildren().size(); j++) {
if (!elem.get(i).getChildren().get(j).isExpanded() ||
!elem.get(i).getChildren().get(j).isLeaf()) {
elem.get(i).getChildren().get(j).toggleExpand();
}
for (int k = 0; k < elem.get(i).getChildren().get(j).getChildren().size(); k++) {
if (!elem.get(i).getChildren().get(j).getChildren().get(k).isExpanded() ||
!elem.get(i).getChildren().get(j).getChildren().get(k).isLeaf()) {
elem.get(i).getChildren().get(j).getChildren().get(k).toggleExpand();
}.....
}
}
}
My question is how to enhance that code or how to put in recursive method ?
To make that code more readable there are a few things you could do (:
Reuse method results, eg that of elem.get(i)
or use an extended for-loop (if elem
etc. support that):
for (int i = 0; i < elem.size(); i++) {
ElementClass element = elem.get(i);
if (!element.isExpanded() || !element.isLeaf()) {
element.toggleExpand();
}
...
}
Additionally since you seem to do the same for the element and it's children you could provide a method eg:
public void expandIfNeeded(ElementClass element) {
if (!element.isExpanded() || !element.isLeaf()) {
element.toggleExpand();
}
}
Then you loop could look like this:
for (int i = 0; i < elem.size(); i++) {
ElementClass element = elem.get(i);
expandIfNeeded( element );
... //handle children here and call the same method for those (could also be using recursion)
}
Additionally, if you don't need the index i
and elem
is iterable you could use the extended for:
for ( ElementClass element : elem ) {
expandIfNeeded( element );
... //handle children here and call the same method for those (could also be using recursion)
}
And of course recursion would make it more readable, especially if you don't know the depth of the tree:
//I'm assuming "elem" is a List<ElementClass> here
public void expandElements( List<ElementClass> elem ) {
for ( ElementClass element : elem ) {
expandIfNeeded( element );
expandElements( element.getChildren() );
}
}
Unreadable code.
Try something like this:
for (int i = 0; i < elem.size(); i++) {
if (!elem.get(i).isExpanded()) {
elem.get(i).toggleExpand();
}
// I don't know what this method returns; no time to research for you
List<Element> children = elem.get(i).getChildren();
for (int j = 0; j < children.size(); j++) {
// don't know what you call it; must be a void method of some kind
yourMethodName(children.get(i));
}
}
You can recursively toggled the element by using following code.
public static void toggleElement(Element elem){
if(elem.isExpand() || elem.isLeaf()){
return;
}else{
elem.toggledExpand();
}
List<Element> children=elem.getChildren();
for (int i = 0; i < children.size(); i++) {
toggleElement(children.get(i));
}
}
Hopefully it will resolve your problem.
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.