how to execute custom javascript code in webdriverjs

Posted on

how to execute custom javascript code in webdriverjs

how to execute custom javascript code in webdriverjs ( https://code.google.com/p/selenium/wiki/WebDriverJs )
I found execute method but it`s purpose is completly different.

Solution :

Here you go:

var yourClientJSFunction = function (param1, param2) {
    // the JS code you want to run in the browser 
}

driver.executeAsyncScript(yourClientJSFunction, param1, param2).then(function (res) {
    // deal with the response
});

If you’re using camme/webdriverjs on node, you can use the following snippet:

client
  .execute(function() {
    return $('ul li').length;
  }, [], function (err, result) {
    console.log(result.value); // 4
  })
  .call(done);

Here, we are getting the number of list-items using jquery. We handle the result in the callback function, by accessing result.value.

It’s also available as a gist here: https://gist.github.com/ragulka/10458018

Leave a Reply

Your email address will not be published. Required fields are marked *