Peter
Peter

Reputation: 65

Selenium java to call java script function

After click the clone link. the page will pop out a same windows to ask input the value. then call the java script function.

this is the page source code for onCloneFilter function

 function onCloneFilter(name) {
    var new_name=prompt('Please type new filter name:','');
        if (new_name == "") {
        return false;
    }

how can I call this function by using java Selenium

Upvotes: 0

Views: 903

Answers (3)

Apoorwa Jayswal
Apoorwa Jayswal

Reputation: 65

To run JavaScript in the context of the currently selected frame or window. There is no need to write separate script to execute JavaScript within the browser using Selenium WebDriver script. Just use predefined interface named 'Java Script Executor'

JavascriptExecutor js = (JavascriptExecutor)driver;
            //Uncomment each scenario by using Ctrl + Shift + \ (backslash) and find the solution

            *//to type text in Selenium WebDriver without using sendKeys() method
            js.executeScript("document.getElementById('some id').value='someValue';");
            js.executeScript("document.getElementById('Email').value='SoftwareTestingMaterial.com';");*/

Upvotes: 1

Bharat DEVre
Bharat DEVre

Reputation: 559

You can use selenium RC API getEval("js code");

selenium.getEval("onCloneFilter");

Upvotes: 0

parishodak
parishodak

Reputation: 4676

Use JavascriptExecutor class to execute Javascript. Assuming this function is already loaded in html.

JavascriptExecutor js=(JavascriptExecutor) driver;
String name=(String) js.executeScript("return onCloneFilter(name)");

Upvotes: 0

Related Questions