6

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!

2 Answers 2

4
+50

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.

Sign up to request clarification or add additional context in comments.

7 Comments

Maybe this questions are a little bit silly but I'm totally new at javascript and selenium. What do you mean where you say that you want to put the external .js into the DOM? 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.
It's why I created my .js file as Selenium.prototype.doProve.... Because I saw on the Internet that it had to be prepared to the Selenium could read it where doProve it's the name of the function.
@Error404 I have edited my above response.Kindly check and get back if you have any further queries
In the code that you put in your edit. Both functions are the same?
Of course I will do but nowadays I don't have the project so I can't prove it. It's why I give to you the bounty and don't accept the answer. Because I consider that your answer it's very helpful but I want to prove it before accept the answer. When I have a new project in which I can prove it, I will mark as accepted. Don't doubt about it. Happy coding ;)
|
-1

You could store the javascript in a file like a properties or xml file.

Sample File:

clickOnLoginButton=function bclick(){....};bclick();

Sample Code:

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.