如何使用Eclipse中的Selenium将外部.js导入Java测试? [英] How can I import an external .js to my Java test with Selenium in Eclipse?

查看:141
本文介绍了如何使用Eclipse中的Selenium将外部.js导入Java测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将JavaScript的函数导入到Eclipse中的Java项目中,并将其与Selenium一起使用,但是我找不到要执行此操作的表单。



我尝试用这样的.js文件Selenium可以识别这段代码:

  Selenium.prototype.doProve = function(){
$(#证明DIV> div> div)每个(function(i,obj)
{
$ (i).click(function(){});
});
};

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



我试图在我的Java项目中使用这个函数,但是我无法得到任何结果,所以我试图执行此函数为String,然后执行脚本,如下所示:

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

//执行脚本

if(driver instanceof JavascriptExecutor){
((JavascriptExecutor)driver).executeScript(script);
}

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



任何帮助将不胜感激。我在这里看到一些问题,但任何一个都为我工作。
非常感谢!

解决方案


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


只有将外部js文件加载到DOM中才能实现此目的。

  var addscript = window。使用document.createElement( '脚本'); addscript.type = '文本/ JavaScript的'; addscript.src =的 'http://localhost/somescript.js'; document.getElementsByTagName( '主体')[0] .appendChild(addscript ); 

注意:大多数浏览器不允许您加载本地资源,所以放您在本地Web服务器中的外部js文件,然后访问它,如 http://localhost/somescript.js



将js文件加载到DOM中后,您可以调用外部js文件中的javascript函数



示例



假设我们有一个名为somescript.js的外部js文件,其中包含以下功能

  //将简单的函数设置为搜索框

window.somefunc = function(){document.getElementsByName(s)[0 ] .value ='test';}

Webdriver代码 p>

  driver.get(http://www.jquery.com); 

//将外部js文件加载到DOM

((JavascriptExecutor)驱动程序)
.executeScript(var addscript = window.document.createElement('script '); addscript.type =' 文本/ JavaScript的 '; addscript.src =的' http://localhost/somescript.js '; document.getElementsByTagName(' 主体)[0] .appendChild(addscript););

//等待js加载到DOM

((JavascriptExecutor)驱动程序)
.executeScript(return typeof(somefunc))。的toString()等于( 功能)。


//现在您调用JS文件中的JavaScript函数

((JavascriptExecutor)驱动程序)
.executeScript(somefunc(); );

注意:幕后Selenium将您的JavaScript代码转换成匿名功能。所以你的somefunc函数是这个匿名函数的本地的。由于JavaScript的范围规则,somefunc不存在于匿名函数之外。所以我们通过将其分配给窗口来实现全局功能。



编辑


我不太明白为什么你使用window语句。而我
正在搜索((JavascriptExecutor)
驱动程序).executeScript(这里.js);但是我不知道它是否是
可能


这是如何 executeScript 方法执行提供的javascript


提供的脚本片段将作为
匿名函数的正文执行。


示例如果我们使用以下代码

 ((JavascriptExecutor)驱动程序)
.executeScript( somefunc = function(){document.getElementsByName(s)[0] .value ='test';});

((JavascriptExecutor)驱动程序)
.executeScript(somefunc(););

(function(){
somefunc = function(){document.getElementsByName(s)[0] .value ='test';}
})() ;

(function(){
somefunc();
});




你是什么意思,你说你想把外部.js
进入DOM?


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



< blockquote>

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


我刚刚给出了一个例子,我的意思是执行脚本中提供的每个脚本将被执行在我们的例子中,我们没有使用执行脚本来创建somefunc函数,而是使用dom中的外部js文件来使用它,我们只使用executioncript方法来调用它,所以可以使用或不使用窗口对象

  //将简单的函数设置为搜索框

somefunc = function (){document.getElementsByName(s)[0] .value ='test';} //这也将工作

希望这有助于您。如有任何疑问,请回来。


I want to import my function of JavaScript to my Java project in Eclipse and using it with Selenium, but I can't find the form to do this.

I try making .js file like this to Selenium could recognise this code:

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

Well, as you can see I have 3 divs and what I want to do it's to access to the third div in which I have 2 divs more (this is the clue of the loop). In each div of the loop I want to make a click.

I tried to use this function in my Java project but I can't get any result so I tried to execute this function as a String and then executing the script like this:

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

//Executing script

 if (driver instanceof JavascriptExecutor) {
        ((JavascriptExecutor) driver).executeScript(script);
 }

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.

Any help would be appreciated. I saw some questions here, but any of them worked for me. Thank you very much!

解决方案

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.

You can achieve this only by loading your external js file into the DOM

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

Note : Most browsers do not allow you to load local resources so put your external js file in local webserver and then access it like http://localhost/somescript.js

After loading the js file into DOM now you can call the javascript functions in the external js file

Example

Lets say we have a external js file named somescript.js which contains the below function

//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();");

Note : Behind the scenes Selenium wraps your JavaScript code in an anonymous function. So your somefunc function is local to this anonymous function.And due to JavaScript's scoping rules, somefunc doesn't exist outside of that anonymous function. so we have made it a global function by assigning it to window.

EDIT :

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

This is how executeScript method executes the provided javascript

The script fragment provided will be executed as the body of an anonymous function.

Example if we used the below code

((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?

By DOM i mean Document object model of the page constructed as a tree of Objects (in short your webpage).We use javascript to load the external js to the webpage and then call the functions in the js file and execute them(like in the above example).

In the code that you put in your edit. Both functions are the same?

I just gave an example by that what i meant was each script provided in execute script will be executed in the body of an anonymous function.In our case we haven't used executescript to create the somefunc function rather used it from the external js file in dom we only called it using the executescript method so you can do it with or without the window object

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

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

Hope this helps you.Kindly get back if you have any queries.

这篇关于如何使用Eclipse中的Selenium将外部.js导入Java测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆