Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/372.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
Javascript 如何在Eclipse中使用Selenium将external.js导入Java测试?_Javascript_Java_Eclipse_Selenium_Selenium Webdriver - Fatal编程技术网

Javascript 如何在Eclipse中使用Selenium将external.js导入Java测试?

Javascript 如何在Eclipse中使用Selenium将external.js导入Java测试?,javascript,java,eclipse,selenium,selenium-webdriver,Javascript,Java,Eclipse,Selenium,Selenium Webdriver,我想将JavaScript函数导入Eclipse中的Java项目,并与Selenium一起使用,但找不到执行此操作的表单 我尝试制作如下的.js文件,以便Selenium能够识别以下代码: Selenium.prototype.doProve = function() { $("#proveDiv > div > div").each(function(i, obj) { $(i).click(function(){}); }); }; 正如你所看

我想将JavaScript函数导入Eclipse中的Java项目,并与Selenium一起使用,但找不到执行此操作的表单

我尝试制作如下的.js文件,以便Selenium能够识别以下代码:

Selenium.prototype.doProve = function() {
    $("#proveDiv > div > div").each(function(i, obj)
    { 
    $(i).click(function(){});
    });
};
正如你所看到的,我有3个div,我想做的是访问第三个div,其中我还有2个div(这是循环的线索)。在循环的每个div中,我都要单击

我尝试在Java项目中使用此函数,但没有得到任何结果,因此我尝试将此函数作为字符串执行,然后执行如下脚本:

String script = "$(\"#proveDiv > div > div" +
                    "\").each(function(i, obj){ " +
                    "$(i).click(function(){});})";

//Executing script

 if (driver instanceof JavascriptExecutor) {
        ((JavascriptExecutor) driver).executeScript(script);
 }
它可以工作,但不是很有用,因为我想创建一个包含所有JavaScript函数的外部.js,并从那里调用它们,而不是在字符串中

任何帮助都将不胜感激。我在这里看到了一些问题,但其中任何一个都对我有用。 多谢各位

它可以工作,但不是很有用,因为我想做一个外部 .js,其中包含所有JavaScript函数并从 在那里,不是一串

只有将外部js文件加载到DOM中,才能实现这一点

var addscript=window.document.createElement('script');addscript.type='text/javascript';addscript.src='http://localhost/somescript.js';document.getElementsByTagName('body')[0].appendChild(addscript);
注意:大多数浏览器不允许您加载本地资源,因此请将外部js文件放入本地Web服务器,然后像这样访问它

在将js文件加载到DOM中之后,现在可以调用外部js文件中的javascript函数

示例

假设我们有一个名为somescript.js的外部js文件,其中包含以下函数

//simple function which sets the value "test" to the search box

window.somefunc = function () {document.getElementsByName("s")[0].value='test';}
网络驱动程序代码

     driver.get("http://www.jquery.com");

     //Load the External js file into DOM

     ((JavascriptExecutor) driver)
      .executeScript("var addscript=window.document.createElement('script');addscript.type='text/javascript';addscript.src='http://localhost/somescript.js';document.getElementsByTagName('body')[0].appendChild(addscript);");

     //wait for the js to be loaded to the DOM

     ((JavascriptExecutor) driver)
      .executeScript("return typeof(somefunc)").toString().equals("function");


     //Now you call the JavaScript functions in the JS file

     ((JavascriptExecutor) driver)
      .executeScript("somefunc();");
注意:幕后Selenium将JavaScript代码包装在一个文件中。因此,您的somefunc函数是该匿名函数的本地函数。由于JavaScript的作用域规则,somefunc不存在于该匿名函数之外。因此,我们通过将其分配给window,使其成为一个全局函数

编辑

     driver.get("http://www.jquery.com");

     //Load the External js file into DOM

     ((JavascriptExecutor) driver)
      .executeScript("var addscript=window.document.createElement('script');addscript.type='text/javascript';addscript.src='http://localhost/somescript.js';document.getElementsByTagName('body')[0].appendChild(addscript);");

     //wait for the js to be loaded to the DOM

     ((JavascriptExecutor) driver)
      .executeScript("return typeof(somefunc)").toString().equals("function");


     //Now you call the JavaScript functions in the JS file

     ((JavascriptExecutor) driver)
      .executeScript("somefunc();");
我真的不明白你为什么要用window语句。而我 正在搜索类似((JavascriptExecutor)的内容 executeScript(“这里是.js”);但我不知道是不是 可能的

这就是方法执行提供的javascript的方式

提供的脚本片段将作为 匿名函数

例如,如果我们使用下面的代码

((JavascriptExecutor) driver)
      .executeScript("somefunc = function () {document.getElementsByName("s")[0].value='test';}");

((JavascriptExecutor) driver)
      .executeScript("somefunc();");

(function() {
        somefunc = function () {document.getElementsByName("s")[0].value='test';}
    })();

    (function() {
        somefunc();
    });
你说要把external.js放在哪里是什么意思 进入DOM

DOM是指页面的文档对象模型,构建为对象树(简而言之,就是你的网页)。我们使用javascript将外部js加载到网页中,然后调用js文件中的函数并执行它们(如上面的示例)


在编辑时输入的代码中。两种功能相同吗

我只是举了一个例子,我的意思是executescript中提供的每个脚本都将在匿名函数体中执行。在我们的例子中,我们没有使用executescript创建somefunc函数,而是从dom中的外部js文件使用它,我们只使用executescript方法调用它,以便您可以使用或不使用它来执行窗口对象

  //simple function which sets the value "test" to the search box

somefunc = function () {document.getElementsByName("s")[0].value='test';}//this will also work

希望这对您有所帮助。如果您有任何疑问,请返回。

您可以将javascript存储在类似属性或xml文件的文件中

示例文件:

clickOnLoginButton=function bclick(){....};bclick();
示例代码:

FileInputStream file;
Properties properties = new Properties();

// load the file handle for properties file
file = new FileInputStream(filename);

// load all the properties from this file
properties.load(file);

// we have loaded the properties, so close the file handle
file.close();

String mainExecutor = properties.getProperty(parameter);
WebDriver dr = initalizeWebDriver();
JavascriptExecutor js = (JavascriptExecutor) dr;

js.executeScript(mainExecutor);

也许这个问题有点傻,但我对javascript和selenium完全不熟悉。你说要将external.js放入DOM是什么意思?我真的不明白你为什么要用window语句。我在搜索类似于
((JavascriptExecutor)driver.executeScript(“这里是.js”)但我不知道这是否可能。这就是为什么我将.js文件创建为
Selenium.prototype.doProve…
。因为我在互联网上看到它必须准备好,Selenium可以在
doProve
中读取它,这是函数的名称。@Error404我已经编辑了我的上述回复。如果您在编辑中输入的代码中有任何进一步的问题,请检查并返回。这两个功能是一样的?当然我会做,但现在我没有这个项目,所以我不能证明它。这就是为什么我给你赏金却不接受答案。因为我认为你的答案很有帮助,但我想在接受答案之前先证明一下。当我有一个新的项目,我可以证明它,我会标记为接受。别怀疑。快乐编码;)