How to call javascript function from Selenium java code

3,711 views
Skip to first unread message

Ripon Al Wasim

unread,
Jan 25, 2012, 12:08:29 AM1/25/12
to seleniu...@googlegroups.com
Hi all,
I have user-extensions.js file which contains the following javascript code:

Selenium.prototype.doTypeRepeated = function(locator, text) {
    // All locator-strategies are automatically handled by "findElement"
    var element = this.page().findElement(locator);

    // Create the text to type
    var valueToType = text + text;

    // Replace the element text with the new text
    this.page().replaceText(element, valueToType);
};

I want to call this javascript function from selenium java code for a text field ("css=input.text"). How can I call this function from Selenium Java code? I have only idea that could be used "getEval()" or "doCommand()" method. What's the way to use those? Please help me.

br,
Ripon Al Wasim

Jayakumar C

unread,
Jan 25, 2012, 6:45:44 AM1/25/12
to seleniu...@googlegroups.com
Try-

1)   Run the Se-Server with userExtensions option-
      java -jar selenium-server-standalone-2.17.0.jar -userExtensions user-extensions.js

2)
     Execute-
     selenium.getEval("selenium.doTypeRepeated('some locator', 'some text');");
(OR)
     Extend the DefaultSelenium and implement the method-

      public void  typeRepeated(String locator, String value){
        commandProcessor.doCommand("typeRepeated", new String[] {locator,value, });       
      }

     Then you get something like this to execute-
     sel_.typeRepeated("some locator", "some text");



Ripon Al Wasim

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To post to this group, send email to seleniu...@googlegroups.com.
To unsubscribe from this group, send email to selenium-user...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/selenium-users?hl=en.



--
Jayakumar

Ripon Al Wasim

unread,
Jan 25, 2012, 11:25:24 PM1/25/12
to seleniu...@googlegroups.com
Hi Jayakumar,

Thanks a lot, I have done according to ur instruction and it is working well.

br,
Ripon Al Wasim

selenium_freak

unread,
Jan 25, 2012, 1:26:43 AM1/25/12
to Selenium Users, seleniu...@googlegroups.com
driver.executescript(filename)

selenium_freak

unread,
Jan 25, 2012, 1:25:07 AM1/25/12
to Selenium Users
driver.executescript(filename)

On Jan 25, 10:08 am, Ripon Al Wasim <riponalwa...@gmail.com> wrote:

Sri

unread,
Feb 6, 2012, 11:44:36 PM2/6/12
to Selenium Users
I am trying to use the executeScript to perform an action 'Click' on
anchor tag. The element is identified/located but never clicked. I
tried the following options,

1) ((JavascriptExecutor) driver).executeScript("return
arguments[0].fireEvent('onclick');", webElement); // This works very
fine for buttons on the same page but not anchor tags.
2) Selenium selenium = new WebDriverBackedSelenium(driver,
SeleniumConstants.BASE_URL);
selenium.fireEvent. selenium.keyPress etc // here i get a JS
error
3) new
Actions(driver).moveToElement(element).clickAndHold().release().perform();

Searched a lot on net but nothing helped. Please respond if u have any
thoughts on y is this happening.
Reply all
Reply to author
Forward
0 new messages