如何通过 JavaScript 在 Selenium RC 中使用 xpath?
我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我自己实现了这个,我只需要执行 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.
我从未这样做过,但认为您可能需要执行类似的操作,
您需要将库添加到页面,然后加载它。
不过,我建议使用 CSS 选择器而不是 XPath 选择器,因为它们在 Selenium 中要快得多。您可以在此处了解如何使用不同的定位器策略。我发现测试速度至少是原始 XPath 的两倍。
I have never done this but think that you may need to do something like
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.