如何通过 JavaScript 在 Selenium RC 中使用 xpath?

发布于 2024-08-26 12:47:35 字数 1271 浏览 5 评论 0原文

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

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

我正在执行以下操作:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

奢欲 2024-09-02 12:47:35

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

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.

染火枫林 2024-09-02 12:47:35

我从未这样做过,但认为您可能需要执行类似的操作,

//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 的两倍。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文