7

我在 IE 6 中使用 Selenium RC,而 XPath 定位器非常慢。所以我想看看 javascript-xpath 是否真的加快了速度。

但是找不到关于如何使用本机 x-path 库的足够/清晰的文档。

我正在执行以下操作:

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");
}

这导致 XPath 定位器的速度提高,但改进并不一致。在某些运行中,定位器所需的时间减半;虽然有时它随机高。

我在这里缺少任何配置步骤吗?如果在这方面取得成功的人可以分享他们的观点和方法,那就太好了。

谢谢,尼尔马尔

解决方案:

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");
}
4

2 回答 2

4

我自己实现了这个,我只需要做 selenium.useXpathLibrary("javascript-xpath")。在我的测试中,javascript xpath 在 IE 8 上快了大约 7 倍。还没有真正测试过其他任何东西,但我们只将它用于 IE。

于 2010-03-29T20:30:19.093 回答
0

我从来没有这样做过,但认为你可能需要做类似的事情

//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");

您需要将库添加到页面然后加载它。

但是,我建议使用 CSS 选择器而不是 XPath 选择器,因为它们在 Selenium 中要快得多。您可以在此处查看如何使用不同的定位器策略。我已经看到测试的速度至少是原始 XPath 的两倍。

于 2010-03-29T19:43:57.407 回答