如何在Eclipse中使用Selenium将外部.js导入我的Java测试?
我想将我的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执行,然后执行如下脚本:
String script = "$(\"#proveDiv > div > div" + "\").each(function(i, obj){ " + "$(i).click(function(){});})"; //Executing script if (driver instanceof JavascriptExecutor) { ((JavascriptExecutor) driver).executeScript(script); }
它工作,但它不是很有用,因为我想创建一个包含所有JavaScript函数的外部.js,并从那里调用它们,而不是在String中。
任何帮助,将不胜感激。 我在这里看到了一些问题,但其中任何一个都适合我。 非常感谢你!
它工作,但它不是很有用,因为我想创建一个包含所有JavaScript函数的外部.js,并从那里调用它们,而不是在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代码 :
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语句。 我正在搜索类似((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文件中的函数并执行它们(就像在以上例子)。
在您编辑的代码中。 两个function都一样吗?
我刚才给出了一个例子,我的意思是执行脚本中提供的每个脚本都将在匿名函数的主体中执行。在我们的例子中,我们没有使用executioncript来创建somefunc函数而是从外部js文件中使用它在dom中我们只使用executioncript方法调用它,所以你可以使用或不使用window对象
//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);