如何在Eclipse中使用Selenium将外部.js导入我的Java测试?

Fra*_*ero 6 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(){});
    });
};
Run Code Online (Sandbox Code Playgroud)

好吧,你可以看到我有3个div和我想要做的是访问第三个div,其中我有2个div更多(这是循环的线索).在循环的每个div中,我想点击一下.

我尝试在我的Java项目中使用此函数,但我无法获得任何结果,因此我尝试将此函数作为String执行,然后执行如下脚本:

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

//Executing script

 if (driver instanceof JavascriptExecutor) {
        ((JavascriptExecutor) driver).executeScript(script);
 }
Run Code Online (Sandbox Code Playgroud)

它工作,但它不是很有用,因为我想创建一个包含所有JavaScript函数的外部.js,并从那里调用它们,而不是在String中.

任何帮助,将不胜感激.我在这里看到了一些问题,但其中任何一个都适合我.非常感谢你!

Vic*_*cky 4

它可以工作,但不是很有用,因为我想创建一个包含所有 JavaScript 函数的外部 .js 并从那里调用它们,而不是在字符串中。

您只能通过将外部 js 文件加载到 DOM 中来实现此目的

var addscript=window.document.createElement('script');addscript.type='text/javascript';addscript.src='http://localhost/somescript.js';document.getElementsByTagName('body')[0].appendChild(addscript);
Run Code Online (Sandbox Code Playgroud)

注意:大多数浏览器不允许您加载本地资源,因此请将外部 js 文件放在本地网络服务器中,然后像http://localhost/somescript.js一样访问它

将 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';}
Run Code Online (Sandbox Code Playgroud)

网络驱动程序代码

     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();");
Run Code Online (Sandbox Code Playgroud)

注意:Selenium 在幕后将您的 JavaScript 代码包装在匿名函数中。所以你的 somefunc 函数是这个匿名函数的本地函数。并且由于 JavaScript 的作用域规则, somefunc 不存在于该匿名函数之外。所以我们通过将它分配给window来使其成为一个全局函数。

编辑

我真的不明白你为什么使用窗口声明。我正在搜索类似 ((JavascriptExecutor) driver).executeScript("here the .js"); 的内容 但我不知道是否可能

这就是executeScript方法执行提供的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();
    });
Run Code Online (Sandbox Code Playgroud)

你说要将外部 .js 放入 DOM 中是什么意思?

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

在您放入编辑的代码中。两个功能一样吗?

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

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

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

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