[Grok-dev] Help on functional doctest for List component

Kris Degryse Kris.Degryse at traficon.com
Mon Oct 12 05:24:13 EDT 2009


Dear Christian,

Underneath a screen shot of the "Add Candidate" form that I want to test in the zope testbrowser through a doctest file.
In this doctest file I would like to fill in or make a selection for each control and press the "Add Candidate" button.
>>> browser.getControl('Video').selected = True
                Here I don't know how to make a  selection for the "*Data Type(s)"???
>>> browser.getControl('Data Format').getControl('XML').selected = True
      >>> import cStringIO
      >>> browser.getControl('Candidate File').add_file(cStringIO.StringIO('Candidate file contents'),'application/octet-stream','C:\\Temp\Candidate.log')
      >>> browser.getControl('Hardware Platform').getControl('win32').selected = True
      >>> browser.getControl('*hardware ID').value = 'Offline'

After the screen shot is my HTML

Thanks anyway,
Kris Degryse
[cid:image001.png at 01CA4B2D.412166C0]

<body>
<form action="http://localhost:8080/arts/Products/VIP-T/V2.09/ResultGroup1/addcandidate"
      method="post" class="edit-form"
      enctype="multipart/form-data">
  <table class="form-fields">
    <tbody>
        <tr>
          <td class="label">
            <label for="form.video">
              <span>Video</span>
            </label>
          </td>
          <td class="field">
            <div class="widget"><div>
<div class="value">
<select id="form.video" name="form.video" size="1" >
<option value="0x086c">Video</option>
</select>
</div>
<input name="form.video-empty-marker" type="hidden" value="1" />
</div></div>
          </td>
        </tr>
        <tr>
          <td class="label">
            <label for="form.data_types">
              <span class="required">*</span><span>Data Type(s)</span>
            </label>
          </td>
          <td class="field">
            <div class="widget"><script type="text/javascript">

function moveItems(from, to)
  {
  // shortcuts for selection fields
  var src = document.getElementById(from);
  var tgt = document.getElementById(to);

  if (src.selectedIndex == -1) selectionError();
  else
    {
    // iterate over all selected items
    // --> attribute "selectedIndex" doesn't support multiple selection.
    // Anyway, it works here, as a moved item isn't selected anymore,
    // thus "selectedIndex" indicating the "next" selected item :)
    while (src.selectedIndex > -1)
      if (src.options[src.selectedIndex].selected)
        {
        // create a new virtal object with values of item to copy
        temp = new Option(src.options[src.selectedIndex].text,
                      src.options[src.selectedIndex].value);
        // append virtual object to targe
        tgt.options[tgt.length] = temp;
        // want to select newly created item
        temp.selected = true;
        // delete moved item in source
        src.options[src.selectedIndex] = null;
      }
    }
  }

// move item from "from" selection to "to" selection
function from2to(name)
  {
  moveItems(name+".from", name+".to");
  copyDataForSubmit(name);
  }

// move item from "to" selection back to "from" selection
function to2from(name)
  {
  moveItems(name+".to", name+".from");
  copyDataForSubmit(name);
  }

function swapFields(a, b)
  {
  // swap text
  var temp = a.text;
  a.text = b.text;
  b.text = temp;
  // swap value
  temp = a.value;
  a.value = b.value;
  b.value = temp;
  // swap selection
  temp = a.selected;
  a.selected = b.selected;
  b.selected = temp;
  }

// move selected item in "to" selection one up
function moveUp(name)
  {
  // shortcuts for selection field
  var toSel = document.getElementById(name+".to");

  if (toSel.selectedIndex == -1)
      selectionError();
  else if (toSel.options[0].selected)
      alert("Cannot move further up!");
  else for (var i = 0; i < toSel.length; i++)
    if (toSel.options[i].selected)
      {
      swapFields(toSel.options[i-1], toSel.options[i]);
      copyDataForSubmit(name);
      }
  }

// move selected item in "to" selection one down
function moveDown(name)
  {
    // shortcuts for selection field
    var toSel = document.getElementById(name+".to");

    if (toSel.selectedIndex == -1) {
        selectionError();
    } else if (toSel.options[toSel.length-1].selected) {
        alert("Cannot move further down!");
    } else {
      for (var i = toSel.length-1; i >= 0; i--) {
        if (toSel.options[i].selected) {
          swapFields(toSel.options[i+1], toSel.options[i]);
        }
      }
      copyDataForSubmit(name);
    }
  }

// copy each item of "toSel" into one hidden input field
function copyDataForSubmit(name)
  {
  // shortcuts for selection field and hidden data field
  var toSel = document.getElementById(name+".to");
  var toDataContainer = document.getElementById(name+".toDataContainer");

  // delete all child nodes (--> complete content) of "toDataContainer" span
  while (toDataContainer.hasChildNodes())
      toDataContainer.removeChild(toDataContainer.firstChild);

  // create new hidden input fields - one for each selection item of
  // "to" selection
  for (var i = 0; i < toSel.options.length; i++)
    {
    // create virtual node with suitable attributes
    var newNode = document.createElement("input");
    var newAttr = document.createAttribute("name");
    newAttr.nodeValue = name;
    newNode.setAttributeNode(newAttr);

    newAttr = document.createAttribute("type");
    newAttr.nodeValue = "hidden";
    newNode.setAttributeNode(newAttr);

    newAttr = document.createAttribute("value");
    newAttr.nodeValue = toSel.options[i].value;
    newNode.setAttributeNode(newAttr);

    // actually append virtual node to DOM tree
    toDataContainer.appendChild(newNode);
    }
  }

// error message for missing selection
function selectionError()
  {alert("Must select something!")}
</script>
<table border="0" class="ordered-selection-field">
  <tr>
    <td>
      <select id="form.data_types.from"
              name="form.data_types.from" size="5"
              multiple="">
        <option value="2be858dafefeebdb71643a221075c81a">Inverse Directions</option>
      </select>
    </td>
    <td>
      <button name="from2toButton" type="button"
              value=" -&gt;"
              onclick="javascript:from2to('form.data_types')">&nbsp;-&gt;</button>
      <br />
      <button name="to2fromButton" type="button"
              value="&lt;- "
              onclick="javascript:to2from('form.data_types')">&lt;-&nbsp;</button>
    </td>
    <td>
      <select id="form.data_types.to"
              name="form.data_types.to" size="5" multiple="">
      </select>
      <input name="form.data_types-empty-marker"
             type="hidden" />
      <span id="form.data_types.toDataContainer">
        <script type="text/javascript">
          copyDataForSubmit('form.data_types');</script>
      </span>
    </td>
    <td>
      <button name="upButton" type="button" value="^"
              onclick="javascript:moveUp('form.data_types')">^</button>
      <br />
      <button name="downButton" type="button" value="v"
              onclick="javascript:moveDown('form.data_types')">v</button>
    </td>
  </tr>
</table>
</div>
          </td>
        </tr>
        <tr>
          <td class="label">
            <label for="form.data_format">
              <span>Data Format</span>
            </label>
           </td>
          <td class="field">
            <div class="widget"><div>
<div class="value">
<select id="form.data_format" name="form.data_format" size="1" >
<option value="3501bb093d363810b671059b9cfed3f8">XML</option>
<option value="cc8d68c551c4a9a6d5313e07de4deafd">CSV</option>
</select>
</div>
<input name="form.data_format-empty-marker" type="hidden" value="1" />
</div></div>
           </td>
        </tr>
         <tr>
          <td class="label">
            <label for="form.file">
              <span class="required">*</span><span>Candidate File</span>
            </label>
           </td>
          <td class="field">
            <div class="widget"><input class="hiddenType" id="form.file.used" name="form.file.used" type="hidden" value="" /> <input class="fileType" id="form.file" name="form.file" size="20" type="file"  /></div>
           </td>
        </tr>
         <tr>
          <td class="label">
            <label for="form.platform">
              <span>Hardware Platform</span>
            </label>
           </td>
          <td class="field">
            <div class="widget"><div>
<div class="value">
<select id="form.platform" name="form.platform" size="1" >
<option value="0x0858">win32</option>
<option value="0x085f">win64</option>
</select>
</div>
<input name="form.platform-empty-marker" type="hidden" value="1" />
</div></div>
           </td>
        </tr>
         <tr>
          <td class="label">
            <label for="form.board">
              <span class="required">*</span><span>hardware ID</span>
            </label>
          </td>
          <td class="field">
            <div class="widget"><input class="textType" id="form.board" name="form.board" size="20" type="text" value=""  /></div>
          </td>
        </tr>
    </tbody>
  </table>
  <div id="actionsView">
    <span class="actionButtons">
      <input type="submit" id="form.actions.4164642043616e646964617465" name="form.actions.4164642043616e646964617465" value="Add Candidate" class="button" />
    </span>
  </div>
</form>
</body>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.zope.org/pipermail/grok-dev/attachments/20091012/3b93cc59/attachment-0001.html 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image001.png
Type: image/png
Size: 15483 bytes
Desc: image001.png
Url : http://mail.zope.org/pipermail/grok-dev/attachments/20091012/3b93cc59/attachment-0001.png 


More information about the Grok-dev mailing list