0 Replies Latest reply: Dec 16, 2011 7:49 AM by Adrien Adrien RSS

Sample to test rich:pickList with Richfaces4

Adrien Adrien Apprentice

Hi,

 

If someone need to test rich:picklist in richfaces 4.1.0Final  while waiting for jfunit has it you can find this utility it,

 

Adrien

 

 



import org.jboss.jsfunit.jsfsession.JSFClientSession;


import com.gargoylesoftware.htmlunit.html.HtmlButton;
import com.gargoylesoftware.htmlunit.html.HtmlDivision;
import com.gargoylesoftware.htmlunit.html.HtmlElement;
import com.gargoylesoftware.htmlunit.html.HtmlPage;


public class UtilRichPickList{
            
            private String xPathAddAll;
            private String xPathAddOne;
            private String xPathRemoveOne;
            private String xPathRemoveAll;
            private String xPathDivSource;
            private String xPathDivTarget;
            private String pickListId;
            private JSFClientSession client;
            
  
          public UtilRichPickList( JSFClientSession jsfClient,String pickListId) {
                    super();
                    this.pickListId = pickListId;
                    this.client=jsfClient;
                    String beginXPath=".//*[@id='"+pickListId+"']";
                    xPathAddAll=beginXPath+"/table/tbody/tr/td[2]/table/tbody/tr[1]/td/button";
                    xPathAddOne=beginXPath+"/table/tbody/tr/td[2]/table/tbody/tr[2]/td/button";
                    xPathRemoveOne=beginXPath+"/table/tbody/tr/td[2]/table/tbody/tr[3]/td/button";
                    xPathRemoveAll=beginXPath+"/table/tbody/tr/td[2]/table/tbody/tr[4]/td/button";
                    xPathDivSource=".//*[@id='"+pickListId+"SourceItems']";
                    xPathDivTarget = ".//*[@id='"+pickListId+"TargetItems']";
          }




          public void clickAddAll(){ 
                    clickButton(xPathAddAll);
          }
          public void clickAdd(String itemTxt){                     
                     dblClickItem(xPathDivSource, itemTxt);
          }
  
          public void clickRemoveAll(){ 
                    clickButton(xPathRemoveAll);
          }
          public void clickRemove(String itemTxt){                     
                     dblClickItem(xPathDivTarget, itemTxt);
          }




          private void dblClickItem(String wrapper, String itemTxt) {
                    try {
  
                              findItemHtmlElt(wrapper, itemTxt).dblClick();
                    } catch (IOException e) {
                              throw new RuntimeException("Error during dblclic on item"+itemTxt,e);
                    }
          }




          private void clickButton(String buttonXpath) {
                    HtmlButton b = (HtmlButton) ((HtmlPage) client.getContentPage()).getByXPath(buttonXpath).get(0);
                    try {
                              b.click();
                    } catch (IOException e) {
                              throw new RuntimeException("Error during clic on button"+buttonXpath,e);
                    }
          }


          /**
           * 
           * @return text displayed in source picklist
           */
          public List<String> getSourceItems(){ 
                    return readItems(xPathDivSource);
          }
          /**
           * 
           * @return text displayed in target picklist
           */
          public List<String> getTargetItems(){
                    return readItems(xPathDivTarget);
          }




          private List<String> readItems(String xpathSrcTarg) { 
                    final HtmlDivision divWrapItems = (HtmlDivision) ((HtmlPage) client.getContentPage()).getByXPath(xpathSrcTarg).get(0);
                    Iterable<HtmlElement> items = divWrapItems.getHtmlElementDescendants();
                    ArrayList<String> targetItemsTxt=new ArrayList<String>();
                    for (HtmlElement htmlElement : items) {
                              targetItemsTxt.add(htmlElement.getTextContent());
                    }
                    return targetItemsTxt;
          }
          public HtmlElement findItemHtmlElt(String xpathWrapperSrcTarget, String txtToFind){
                    final HtmlDivision divWrapItems = (HtmlDivision) ((HtmlPage) client.getContentPage()).getByXPath(xpathWrapperSrcTarget).get(0);
                    Iterable<HtmlElement> items = divWrapItems.getHtmlElementDescendants();                     
                    for (HtmlElement htmlElement : items) {
                              if(htmlElement.getTextContent().equals(txtToFind) ){
                                        return htmlElement;
                              }
  
                    }
                    throw new RuntimeException("Not found Item '"+txtToFind+"' xpath"+xpathWrapperSrcTarget);
          }
  
  
          public String debug(){
                    StringBuilder b = new StringBuilder();
                    b.append("\n\n######################################## PICKLIST"+ pickListId+" ###############################################\n\n");
                    List<String> src = getSourceItems();
                    b.append("SourceItems count:"+src.size()+":\n");
                    b.append("-------------------------------\n\n");
  
                    for (String item : src) {
                              b.append(" ").append(item);//.append("\n");
                    }
  
                    b.append("\n\n");
  
                    List<String> target = getTargetItems();
                    b.append("TargetItems count:"+target.size()+":\n");
                    b.append("-------------------\n"); 
                    for (String item : target) {
                              b.append(" ").append(item);//.append("\n");
                    }
                    b.append("\n\n===========================================PICKLIST"+ pickListId+" ===========================================\n");
                    return b.toString();
          }
  
}