Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

executing "commands" using selenium-webdriver in node javascript

I'm interested in executing a number of the advanced "Commands" via the javascript API https://code.google.com/p/selenium/source/browse/javascript/webdriver/command.js

If I start with the base code:

var browser = new webdriver
        .Builder()
        .usingServer(server.address())
        .withCapabilities(webdriver.Capabilities.phantomjs())
        .build();

every form of "likely" syntax I've tried to execute has failed. for example:

// does not work
console.log(webdriver.Command('getWindowSize'))
// does not work
console.log(browser.Command('getWindowSize'))

Does anyone know how to execute "get window size", or "set window size" in selenium javascript webdriver?

like image 968
Alex C Avatar asked Sep 30 '13 20:09

Alex C


People also ask

Can we execute JavaScript on browser with Selenium?

Selenium webdriver can execute Javascript. After loading a page, you can execute any javascript you want. A webdriver must be installed for selenium to work.

Which command is used to install Selenium WebDriver in node?

var webdriver = require('selenium-webdriver'); var driver = new webdriver. Builder().


1 Answers

You are probably looking for driver.executeScript.

Example:

var driver = new webdriver.Builder().
   withCapabilities(webdriver.Capabilities.chrome()).
   build();

driver.executeScript('return 2').then(function(return_value) {
    console.log('returned ', return_value)
});

This will log 2 to the console.

I also tested this with:

driver.get('http://underscorejs.org/');

driver.executeScript('return _').then(function(return_value) {
    console.log('returned ', return_value)
});

... which correctly lists all the methods defined on _, so it appears to work.

like image 73
Anonymous Avatar answered Oct 25 '22 18:10

Anonymous