Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Headless automation with Nodejs Selenium Webdriver

Tags:

I am working with an automation tool which has to be deployed inside an ubuntu server, my wonder is if is possible to use chrome in a silent way with Selenium Webdriver.

I've tried the following code so far, but it keeps opening the browser (I'm doing the tests in a Windows 10):

    var webdriver = require('selenium-webdriver'),         chrome    = require('selenium-webdriver/chrome')         By        = webdriver.By,         until     = webdriver.until,         options   = new chrome.Options();         options.addArguments('--headless');     var path = require('chromedriver').path;     var service = new chrome.ServiceBuilder(path).build();         chrome.setDefaultService(service);     var driver = new webdriver.Builder().forBrowser('chrome').withCapabilities(options.toCapabilities()).build();  driver.get('https://www.google.com'); 

Note that the addArguments('--headless') is the parameter that should make the navigation silent, but apparently it's not working or I am missing something I am not aware of.

If there is something I am missing, please tell me because I don't know if what I want to do is possible, as It is the frist time I work with this kind of technology.

Thanks.

like image 238
avilac Avatar asked May 26 '17 08:05

avilac


People also ask

Does Selenium support NodeJS?

Set up the dependenciesBefore 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.

How do I run headless in Selenium WebDriver?

You can run Google Chrome in headless mode simply by setting the headless property of the chromeOptions object to True. Or, you can use the add_argument() method of the chromeOptions object to add the –headless command-line argument to run Google Chrome in headless mode using the Selenium Chrome web driver.

Does Selenium support headless browser?

Yes, Selenium supports headless browser testing. This can be done with the help of the HTMLUnitDriver. It is the fastest webdriver among other browser drivers and is platform independent. Navigate to the link − https://github.com/SeleniumHQ/htmlunitdriver/releases.

Is Selenium faster in headless mode?

No, a headless browser test doesn't display on the screen, but the full browser is loaded. It's a full version of chrome, and doesn't run any faster or slower.


2 Answers

Updated answer circa FEB-2018.

Referencing the Selenium Webdriver NodeJS Examples (commit 5bf50c4)

const chrome = require('selenium-webdriver/chrome'); const firefox = require('selenium-webdriver/firefox'); const {Builder, By, Key, until} = require('selenium-webdriver');  const screen = {   width: 640,   height: 480 };  let driver = new Builder()     .forBrowser('chrome')     .setChromeOptions(new chrome.Options().headless().windowSize(screen))     .setFirefoxOptions(new firefox.Options().headless().windowSize(screen))     .build(); 

Headless Chrome available since major version 59.0 APR-2017

Headless Firefox available since major version 56.0 SEP-2017

like image 148
Josh Peak Avatar answered Oct 18 '22 13:10

Josh Peak


Try this one:

var webdriver = require('selenium-webdriver'),     chrome    = require('selenium-webdriver/chrome')     By        = webdriver.By,     until     = webdriver.until,     options   = new chrome.Options();     options.addArguments('headless'); // note: without dashes     options.addArguments('disable-gpu') var path = require('chromedriver').path; var service = new chrome.ServiceBuilder(path).build();     chrome.setDefaultService(service); var driver = new webdriver.Builder()     .forBrowser('chrome')     .withCapabilities(webdriver.Capabilities.chrome())      .setChromeOptions(options)                         // note this     .build();  driver.get('https://www.google.com'); 
like image 43
Matti Lehtinen Avatar answered Oct 18 '22 13:10

Matti Lehtinen