Hi Jonathan,

You can use assertElementPresentByXPath and catch the exception (not very nice 
I agree).

I don't have JWebUnit sources here but is it not possible to subclass WebTester 
and use this custom WebTester in your WebTestCase?

You can also open a feature request or a patch with your modification but I 
don't know when it will be merged.

Regards,

Julien


----- Message d'origine ----
De : Jonathan Slate <[EMAIL PROTECTED]>
À : jwebunit-users@lists.sourceforge.net
Envoyé le : Jeudi, 3 Avril 2008, 22h05mn 00s
Objet : [JWebUnit-users] hasElementByXPath

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
JWebUnit-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/jwebunit-users






      
_____________________________________________________________________________ 
Envoyez avec Yahoo! Mail. Plus de moyens pour rester en contact. 
http://mail.yahoo.fr

-------------------------------------------------------------------------
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
JWebUnit-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/jwebunit-users

Reply via email to