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

How can I import an external .js to my Java test with Selenium in Eclipse?

本文关键字:Selenium Java 测试 Eclipse 外部 js 导入      更新时间:2023-09-26

我想将JavaScript函数导入到Eclipse中的Java项目中,并将其与Selenium一起使用,但我找不到这样做的表单。

我尝试使.js文件像这样硒可以识别这段代码:

Selenium.prototype.doProve = function() {
    $("#proveDiv > div > div").each(function(i, obj)
    { 
    $(i).click(function(){});
    });
};

嗯,正如你所看到的,我有3个div,我想做的是访问第三个div,其中我有2个div(这是循环的线索)。在循环的每个div中,我想创建一个click。

我试图在我的Java项目中使用这个函数,但我不能得到任何结果,所以我试图执行这个函数作为一个字符串,然后执行这样的脚本:

String script = "$('"#proveDiv > div > div" +
                    "'").each(function(i, obj){ " +
                    "$(i).click(function(){});})";
//Executing script
 if (driver instanceof JavascriptExecutor) {
        ((JavascriptExecutor) driver).executeScript(script);
 }

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

任何帮助都会很感激。我在这里看到了一些问题,但没有一个对我有用。非常感谢!

它可以工作,但不是很有用,因为我想做一个外部.js包含所有JavaScript函数,并从there, not in a String.

你只能通过将你的外部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服务器上,然后像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';}
Webdriver code:
     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代码封装在一个匿名函数中。所以你的somefunction函数是这个匿名函数的局部函数。由于JavaScript的作用域规则,somefunc不存在于匿名函数之外。所以我们把它赋值给window,使它成为一个全局函数。

EDIT:

我真的不明白你为什么要用window语句。和我正在搜索类似于(JavascriptExecutor)的东西司机)。executeScript("这里是。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();
    });

你说你想把外部的。js放在哪里是什么意思到DOM中?

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

在您编辑的代码中。两个函数是一样的吗?

我只是举了一个例子,我的意思是在execute script中提供的每个脚本将在匿名函数的体中执行。在我们的例子中,我们没有使用executescript来创建somefunction函数而是从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);