如何将外部.js导入到Eclipse中使用Selenium的Java测试中?
How can I import an external .js to my Java test with Selenium in Eclipse?
我想将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);
- WebDriverException:tinyMCE未在selenium Web driver java中定义
- 等待,直到使用Java在selenium网络驱动程序中手动单击登录按钮
- Java Selenium 2.0-在WebElement.click()之前使用内置滚动时处理z索引(navi)
- 使用 Selenium 和 java 测试 React JS 下拉列表
- 如何使用Java在Selenium WebDriver的隐藏字段中键入一些文本
- 使用Java脚本跟踪网页中是否有Ajax请求,或者通过Selenium Web驱动程序拦截XMLHttpRequest
- Selenium-webdriver (Java Script) 等待元素消失
- 当尝试登录instagram时,带有Selenium的Phantomjs不处理iframe Java脚本
- Java Selenium-JavaScriptExecutor-Argument的类型非法:driverFactory
- 如何将JavaScript与Selenium WebDriver Java结合使用
- Selenium Web驱动程序使用Java查找Element
- Selenium Java-通过JavascriptExecutor获取页面源代码
- Javascript 在 Java Selenium Webdriver 中的使用
- JavascriptExecutor(用于Selenium - Java)无法按FF 24.0的预期工作
- 如何使用Selenium Webdriver和Java从href html标签中选择“Page=?” valuse
- Selenium Java CSS Selector bug?
- 为什么我必须添加+ I +到第二个WebElement在Selenium Java循环
- Click()或Submit()不适用于Selenium java代码
- 如何知道javascript代码是否已经在Selenium Java中执行
- 第节中的Selenium Java滚动条