javascript
eclipse中java获取js的值_javascript – 如何在Eclipse中使用Selenium将外部.js导入我的Java测试?...
It works, but it’s not very useful, because I want to make an external
.js which contains all the JavaScript functions and call them from
there, not in a String.
您只能通過(guò)將外部js文件加載到DOM中來(lái)實(shí)現(xiàn)此目的
var addscript=window.document.createElement('script');addscript.type='text/javascript';addscript.src='http://localhost/somescript.js';document.getElementsByTagName('body')[0].appendChild(addscript);
注意:大多數(shù)瀏覽器不允許您加載本地資源,因此將外部js文件放在本地Web服務(wù)器中,然后像http://localhost/somescript.js一樣訪問(wèn)它
將js文件加載到DOM后,您可以調(diào)用外部js文件中的javascript函數(shù)
例
假設(shè)我們有一個(gè)名為somescript.js的外部js文件,其中包含以下函數(shù)
//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代碼包裝在anonymous function中.因此,您的somefunc函數(shù)是此匿名函數(shù)的本地函數(shù).由于JavaScript的作用域規(guī)則,somefunc不存在于該匿名函數(shù)之外.所以我們通過(guò)將它分配給窗口使它成為一個(gè)全局函數(shù).
編輯:
And I don’t really understand why you use the window statement. And I
was searching something like ((JavascriptExecutor)
driver).executeScript(“here the .js”); But I don’t know if it is
possible
這是executeScript方法執(zhí)行提供的javascript的方式
The script fragment provided will be executed as the body of an
anonymous function.
例如,如果我們使用以下代碼
((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();
});
What do you mean where you say that you want to put the external .js
into the DOM?
通過(guò)DOM我的意思是構(gòu)建為對(duì)象樹(shù)(簡(jiǎn)稱你的網(wǎng)頁(yè))的頁(yè)面的文檔對(duì)象模型.我們使用javascript將外部js加載到網(wǎng)頁(yè),然后調(diào)用js文件中的函數(shù)并執(zhí)行它們(就像在以上例子).
In the code that you put in your edit. Both functions are the same?
我剛才給出了一個(gè)例子,我的意思是執(zhí)行腳本中提供的每個(gè)腳本都將在匿名函數(shù)的主體中執(zhí)行.在我們的例子中,我們沒(méi)有使用executioncript創(chuàng)建somefunc函數(shù)而是從外部js文件中使用它在dom中我們只使用executioncript方法調(diào)用它,所以你可以使用或不使用window對(duì)象
//simple function which sets the value "test" to the search box
somefunc = function () {document.getElementsByName("s")[0].value='test';}//this will also work
希望這會(huì)對(duì)你有所幫助.如果您有任何疑問(wèn),請(qǐng)回復(fù).
總結(jié)
以上是生活随笔為你收集整理的eclipse中java获取js的值_javascript – 如何在Eclipse中使用Selenium将外部.js导入我的Java测试?...的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: java将数据封装为树结构_JAVA代码
- 下一篇: mini发布特别版车型,外观更加接近黑武