views:

589

answers:

2

I am using Selenium RC with IE 6 and XPath locators are terribly slow. So I am trying to see if javascript-xpath actually speeds up things.

But could not find enough/clear documentation on how to use native x- path libraries.

I am doing the following:

protected void startSelenium (String testServer, String appName, String testInBrowser){
    selenium = new DefaultSelenium("localhost", 4444, "*" +testInBrowser, testServer+ "/"+ appName + "/");
    echo("selenium instance created:"+selenium.getClass());
    selenium.start();
    echo("selenium instance started..." + testServer + "/" + appName +"/");

    selenium.runScript("lib/javascript-xpath-latest-cmp.js");
    selenium.useXpathLibrary("javascript-xpath");
    selenium.allowNativeXpath("true");
}

This results in speed improvement of XPath locator but the improvements are not consistent. On some runs the time taken for a locator is halved; while sometimes its randomly high.

Am I missing any configuration step here? Would be great if someone who has had success with this could share their views and approach.

Thanks, Nirmal

Solution:

protected void startSelenium (String testServer, String appName, String testInBrowser){
    selenium = new DefaultSelenium("localhost", 4444, "*" +testInBrowser, testServer+ "/"+ appName + "/");
    echo("selenium instance created:"+selenium.getClass());
    selenium.start();
    echo("selenium instance started..." + testServer + "/" + appName +"/");

    selenium.useXpathLibrary("javascript-xpath");
}
A: 

I have never done this but think that you may need to do something like

//Add the library to the page since runScript just does an eval on the JS
selenium.runScript("document.body.append(document.createElement('script')).src = 'path/to/lib');"); 
selenium.useXpathLibrary("javascript-xpath");
selenium.allowNativeXpath("true");

You will need to add the library to the page and then load it.

However, I would recommend using CSS Selectors instead of XPath Selectors as they are a lot faster in Selenium. You can see how to use different locator strategies here. I have seen tests become at least twice as fast as the original XPath.

AutomatedTester
I am currently using CSS Selectors... but they are a bit more complicated and there are more problems when using them with IE.Am exploring the speed improvements offered by javascript-xpath to see it they compare with CSS selectors.
Nirmal Patel
A: 

I implemented this myself and I only had to do selenium.useXpathLibrary("javascript-xpath"). In my tests, the javascript xpath was about 7x faster on IE 8. Haven't really tested on anything else, but we only use it for IE.

Dan at Demand
Hi Dan,If we do not explicitly add the script; where does the engine come from?? Does selenium already include this js?? i wasnt able to find any such evidence.
Nirmal Patel
If you look at the Selenium source code, you'll see javascript-xpath-0.1.11.js is include in common\src\js\core\xpath, so I'm assuming it's pulling from that. SOMETHING is resulting in my tests going 7x faster. =)
Dan at Demand