AjaxElementLocatorFactory and xpath

59 views
Skip to first unread message

Danny Guerrier

unread,
Feb 5, 2010, 9:56:19 AM2/5/10
to webd...@googlegroups.com
I see many warnings about not using AjaxElementLocatorFactory with xpaths because of xpaths poor performance.
What are the side effects if I ignore this warning?  I have a custom method that polls for elements and it seems like IE driver chokes from time to time and throws a ( org.openqa.selenium.WebDriverException: An unhandled exception has occured. )
The code is pretty straight forward; not sure if this would be considered a bug

I was contemplating converting over the page factory method of doing things since I am already using page objects but that may not help much if I'd be in the same boat.  I have to use xpaths.

    public WebElement getElement(WebDriver driver, By by, int seconds) {
       
        WebElement element = null;
        long end = System.currentTimeMillis() + (seconds * 1000);
        while (System.currentTimeMillis() < end) {
            try {
                element = driver.findElement(by);
                break;
            } catch (NoSuchElementException e) {   
                //ignore
            }
        }
       
        if(null == element){
            closeDriverInstance();
            throw new NoSuchElementException("Element not found: " + by.toString());
        }
       
        return element;
    }

Simon Stewart

unread,
Feb 7, 2010, 10:41:49 AM2/7/10
to webd...@googlegroups.com
The danger of ignoring the warning is that you'll end up having to
maintain a lot of xpath (ugh) and your tests will run more slowly than
they otherwise could. Also, we delegate down to the underlying XPath
engine wherever possible. If you do something "clever" with xpaths,
you may find it hard to port your tests between browsers.

As for waiting for elements to appear when using page objects, and the
PageFactory, then take a look at the AjaxElementLocatorFactory[1],
which performs the same function as the piece of code above.

Regards,

Simon

[1] http://selenium.googlecode.com/svn/trunk/docs/api/java/org/openqa/selenium/support/pagefactory/AjaxElementLocatorFactory.html

> --
> You received this message because you are subscribed to the Google Groups
> "webdriver" group.
> To post to this group, send email to webd...@googlegroups.com.
> To unsubscribe from this group, send email to
> webdriver+...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/webdriver?hl=en.
>

Reply all
Reply to author
Forward
0 new messages