Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I control a browser ( ala Selenium ) with node.js?

Tags:

node.js

I've heard of soda, but it seems like it requires you to signup and there's a limit on the # of minutes ( free acct / 200 minutes ).

Does anyone know if there's some alternative way to control a browser, or more specifically invoke JS on a web page?

like image 354
meder omuraliev Avatar asked May 02 '11 19:05

meder omuraliev


People also ask

How do I run Selenium in NodeJS?

Set up the dependencies Before you can start running your Selenium tests with NodeJS , you need to have the NodeJS language bindings installed. Run the following command on your terminal/command-line to install the required dependencies.

CAN NodeJS be used in a browser?

js is a server-side JavaScript run-time environment. It's open-source, including Google's V8 engine, libuv for cross-platform compatibility, and a core library. Notably, Node. js does not expose a global "window" object, since it does not run within a browser.

Can js code execute outside the browser?

You can run JavaScript console in terminal or any command-line interface using Node. js, an open-source, platform-agnostic runtime that executes JavaScript outside a web browser.

How do I open a browser in node?

To use Node. js to open default browser and navigate to a specific URL, we can use the open package. const open = require("open"); open("http://example.com"); open("http://example.com", { app: "firefox" }); to call open to open the URL we pass into it.


1 Answers

https://github.com/LearnBoost/soda/raw/master/examples/google.js

/**
 * Module dependencies.
 */

var soda = require('../')
  , assert = require('assert');

var browser = soda.createClient({
    host: 'localhost'
  , port: 4444
  , url: 'http://www.google.com'
  , browser: 'firefox'
});

browser.on('command', function(cmd, args){
  console.log(' \x1b[33m%s\x1b[0m: %s', cmd, args.join(', '));
});

browser
  .chain
  .session()
  .open('/')
  .type('q', 'Hello World')
  .clickAndWait('btnG')
  .getTitle(function(title){
    assert.ok(~title.indexOf('Hello World'), 'Title did not include the query');
  })
  .clickAndWait('link=Advanced search')
  .waitForPageToLoad(2000)
  .assertText('css=#gen-query', 'Hello World')
  .assertAttribute('as_q@value', 'Hello World')
  .testComplete()
  .end(function(err){
    if (err) throw err;
    console.log('done');
  });
like image 74
Alfred Avatar answered Oct 08 '22 11:10

Alfred