2 Replies Latest reply on Dec 12, 2008 6:58 AM by Adrien Adrien

    Jsunit Beta 3 : how to test  selectOneMenu ?

    Adrien Adrien Apprentice

      Hi,

      I just migrated from jsfunit beta2 to beta 3.
      I m blocked to test my selectOneMenu.
      I m trying :
      client.setValue("form:ismale",0+"0");// KO
      client.click("form:ismale:0"); //KO

      There is someone who have a code sample of unitary test for selectonemenu?
      Thanks



      <h:selectOneMenu id="ismale" value="#{ctrlCreateUser.mySexAndMySearchCste}" required="true">
      <f:selectItems value="#{selectBean.sexSelectWithNoSelect}" />
      <f:validator validatorId="selectIsMaleValidator" />
      </h:selectOneMenu>

        • 1. Re: Jsunit Beta 3 : how to test  selectOneMenu ?
          Stan Silvert Master

          For JSF tags like <h:selectOneMenu> and <h:selectManyListbox> that generate select tags in HTML, you will sometimes need to use the HtmlUnit API. For example, I have some markup like this:

          <h:selectManyListbox id="WeekdaysUsingItemList">
           <f:selectItems id="WeekdayItems" value="#{weekdaylist}"/>
          </h:selectManyListbox>


          It creates HTML like this:
          <select id="form1:WeekdaysUsingItemList" size="0" multiple="multiple" name="form1:WeekdaysUsingItemList">
           <option value="Monday">Monday</option>
           <option value="Wednesday">Wednesday</option>
           <option value="Friday">Friday</option>
           <option value="Tuesday">Tuesday</option>
           <option value="Thursday">Thursday</option>
          </select>


          I can select/deselect the items with the HtmlUnit API like this:
          JSFSession jsfSession = new JSFSession("/index.faces");
          JSFClientSession client = jsfSession.getJSFClientSession();
          
          HtmlSelect select = (HtmlSelect)client.getElement("WeekdaysUsingItemList");
          select.getOptionByValue("Monday").setSelected(true);
          select.getOptionByValue("Tuesday").setSelected(false);
          select.getOptionByValue("Wednesday").setSelected(true);
          select.getOptionByValue("Thursday").setSelected(true);
          select.getOptionByValue("Friday").setSelected(false);


          If you are using <f:selectItem> instead of <f:selectItems> like this you can set an id for each item and make things a bit easier:
          <h:selectManyListbox id="Weekdays">
           <f:selectItem id="selectMonday" itemValue="Monday"/>
           <f:selectItem id="selectTuesday" itemValue="Tuesday"/>
           <f:selectItem id="selectWednesday" itemValue="Wednesday"/>
           <f:selectItem id="selectThursday" itemValue="Thursday"/>
           <f:selectItem id="selectFriday" itemValue="Friday"/>
           <f:selectItem id="selectSaturday" itemValue="Saturday"/>
          </h:selectManyListbox>


          JSFSession jsfSession = new JSFSession("/index.faces");
          JSFClientSession client = jsfSession.getJSFClientSession();
          
          client.click("selectMonday");
          client.click("selectWednesday");
          client.click("selectFriday");


          Stan

          • 2. Re: Jsunit Beta 3 : how to test  selectOneMenu ?
            Adrien Adrien Apprentice

            Thanks Stan, its woks fine!