How can I control the browser (ala Selenium) using node.js?

I heard about soda , but it looks like it requires you to subscribe, and there is a limit on the number of minutes (free acct / 200 minutes).

Does anyone know if there is an alternative way to control the browser, or more specifically invoke JS on a web page?

+6
source share
5 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'); }); 
+5
source

Zombie.js may work for you. He is headless and seems really cool.

+4
source

There are currently Selenium bindings for JavaScript that work with Node.js.

The following are some basic steps to get started:

  • 1 Install Node.js, you can find it here.
  • Make sure you have the latest Chrome driver and put it in your path.
  • Use npm install selenium-webdriver to add a module to your project.
  • Write a test, for example:

var webdriver = require('selenium-webdriver');

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

 driver.get ('http://www.google.com');
 driver.findElement (webdriver.By.name ('q')). sendKeys ('simple programmer');
 driver.findElement (webdriver.By.name ('btnG')). click ();
 driver.quit (); </code>

I will explain how to do this with some screenshots and how to use Mocha as a test driver in my blog post here .

+3
source

Here is a clean node.js wrapper around the java API for selenium web server:

https://npmjs.org/package/webdriver-sync

Here is an example:

 var webdriverModule = require("webdriver-sync"); var driver = new webdriverModule.ChromeDriver; var By = webdriverModule.By; var element = driver.findElement(By.name("q")); element.sendKeys("Cheese!"); element.submit(); element = driver.findElement(By.name("q")); assert.equal(element.getAttribute('value'), "Cheese!"); 

Save this in a .js file and run it with node.

The module is a clean shell, so things like sleep or synchronous calls are possible. Here is the current module interface:

 module.exports={ ChromeDriver:ChromeDriver, FirefoxDriver:FirefoxDriver, HtmlUnitDriver:HtmlUnitDriver, By:new By(), ExpectedConditions:new ExpectedConditions(), WebDriverWait:WebDriverWait, Credentials:UserAndPassword, Cookie:Cookie, TimeUnits:TimeUnits, /** * @param {number} amount in mills to sleep for. */ sleep:function(amount){ java.callStaticMethodSync( "java.lang.Thread", "sleep", new Long(amount) ); } }; 

You can see the integration test, which tests the full capabilities here:

https://github.com/jsdevel/webdriver-sync/blob/master/test/integrations/SmokeIT.js

+2
source

wd is "A node.js javascript client for webdriver / selenium 2"

+1
source

Source: https://habr.com/ru/post/887142/


All Articles