Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/444.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Selenium RC和JavaScript中使用xpath?_Javascript_Selenium_Xpath_Selenium Rc - Fatal编程技术网

如何在Selenium RC和JavaScript中使用xpath?

如何在Selenium RC和JavaScript中使用xpath?,javascript,selenium,xpath,selenium-rc,Javascript,Selenium,Xpath,Selenium Rc,我将Selenium RC与IE 6一起使用,XPath定位器非常有用 慢点。 所以我想看看javascript xpath是否真的能加快速度 但找不到关于如何使用本机x的足够/清晰的文档- 路径库 我正在做以下工作: protected void startSelenium (String testServer, String appName, String testInBrowser){ selenium = new DefaultSelenium("localhost", 4444

我将Selenium RC与IE 6一起使用,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定位器的速度,但 改进并不一致。在一些跑步中,跑步所需的时间 定位器减半;而有时它的随机高

我是否缺少任何配置步骤?如果有人 在这方面取得成功的世卫组织可以分享他们的观点和方法

谢谢, 尼马尔

解决方案:


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

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

我自己实现了这一点,只需执行selenium.useXpathLibrary(“javascript XPath”)。在我的测试中,IE8上的javascript xpath大约快了7倍。还没有真正测试过其他任何东西,但我们只在IE中使用它。

我目前正在使用CSS选择器。。。但是它们有点复杂,在IE中使用它们时会有更多的问题。我正在探索javascript xpath提供的速度改进,看看它们与CSS选择器相比有多快;发动机是从哪里来的??selenium是否已经包含此js??我找不到任何这样的证据。如果你看一下Selenium的源代码,你会发现javascript-xpath-0.1.11.js包含在common\src\js\core\xpath中,所以我假设它是从中提取出来的。某种原因导致我的测试速度加快了7倍。=)
//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");