Howdy,

I had a problem using WebTester's getElementAttributByXPath method b/c
it was asserting that the element existed before getting the attribute
value, and I didn't know if the element was going to be present. If it
wasn't for my test, that was fine, but if it was I wanted the attribute
value. But my test would always fail if the element was not present.

To fix it I compiled my own version of WebTester, adding the following
method:

    /**
     * Return true if an element with a given xpath is present.
     * 
     * @param xpath element xpath to test for.
     */
    public boolean hasElementByXPath(String xpath) {
        return getTestingEngine().hasElementByXPath(xpath);
    }

Then I could check to see if the element existed before getting the
attribute. So I wondered:

a) Is there a better way to do this that doesn't require changing
JWebUnit code? I'm extending WebTestCase, is there an existing way for
me to check for the presence or an element by xpath that I'm missing?  

b) If not, would this method be a useful addition to WebTester?

Thanks,
Jonathan

P.S. In case it clarifies my post, here is the method I'm using this in:

/**
 * Get a Map of values from an HTML table, providing the ID 
 * of the table. Expects a table with two columns, one with 
 * keys and one with values. If a "value" cell has an empty 
 * String value, this method will attempt to look for an 
 * image and use its "alt" value instead.
 * 
 * @param tableId The HTML id attribute value of the table.
 */
protected Map<String, String> getTableMap(String tableId) {
  Map<String, String> map = new HashMap<String, String>();

  Table table = getTable(tableId);

  List<Row> rows = table.getRows();

  int rowCount = 0;
  for (Row row : rows) {
    rowCount++;
    String key = ((Cell) row.getCells().get(0)).getValue();
    String value = ((Cell) row.getCells().get(1)).getValue();

    if (StringStuff.isEmpty(value)) {
      //ok so this is a bit site specific...
      String xpath = "/html/body/[EMAIL PROTECTED]'container']/[EMAIL 
PROTECTED]'"
           + tableId + "']/tbody/tr[" + rowCount + "]/td[2]/img";
      if (hasElementByXPath(xpath))
        value = getElementAttributByXPath(xpath, "alt");
    }

    map.put(key, value);
  }
  return map;
}

P.P.S. Building JWebUnit w/ Maven was so easy! I think I'm going to be a
convert. :)



-------------------------------------------------------------------------
Check out the new SourceForge.net Marketplace.
It's the best place to buy or sell services for
just about anything Open Source.
http://ad.doubleclick.net/clk;164216239;13503038;w?http://sf.net/marketplace
_______________________________________________
JWebUnit-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/jwebunit-users

Reply via email to