We use PhantomJS as simple test runner like this:
phantomjs path/to/test.js
Is there a similar way with headless chrome?
Selenium WebDriver is a NPM package that can help you to run a headless browser.
Try the below example:
const chrome = require('selenium-webdriver/chrome');
const {Builder, By, Key, until} = require('selenium-webdriver');
const width = 640;
const height = 480;
let driver = new Builder()
.forBrowser('chrome')
.setChromeOptions(
new chrome.Options().headless().windowSize({width, height}))
.build();
driver.get('http://www.google.com/ncr')
.then(_ =>
driver.findElement(By.name('q')).sendKeys('webdriver', Key.RETURN))
.then(_ => driver.wait(until.titleIs('webdriver - Google Search'), 1000))
.then(
_ => driver.quit(),
e => driver.quit().then(() => { throw e; }));
According to the API, the driver methods return Promises and, as a result, can be called using the async/await syntax:
const chrome = require('selenium-webdriver/chrome');
const {Builder, By, Key, until} = require('selenium-webdriver');
async function test() {
const width = 640;
const height = 480;
let driver = new Builder()
.forBrowser('chrome')
.setChromeOptions(
new chrome.Options().headless().windowSize({width, height}))
.build();
await driver.get('http://www.google.com/ncr')
await driver.findElement(By.name('q')).sendKeys('webdriver', Key.RETURN))
await driver.wait(until.titleIs('webdriver - Google Search'), 1000))
await driver.quit()
}
test();
require('..') makes it hard to know what I should install first.Builder, By, Key and until come from selenium-webdriver. Chrome and Firefox are required from "selenium-webdriver/chrome" and "selenium-webdriver/firefox"as explained on developers.google you have option of using Node or Selenium, in either case you will have to re-write part of your test cases to use the new api instead of the phantomjs api.
The following options are based on Node:
I suggest you to head over to their github page for installation and usage instructions.
Your test case should be written using the Puppetteer api and run with:
node path/to/test.js
There is a list of puppeteer resources, you can use puppeteer with jest, mocha, angular, e2e..
chrome-remote-interface is a lower-level library than Puppeteer's API. I recommend it if you want to be close to the metal and use the DevTools protocol directly.
You need to start chrome in headless mode or use lighthouse or nodejs:
node --inspect=9222 path/to/test.js
write your javascript test case using the chrome DevTools protocol and follow the instructions on their github page to install and run your tests.
The other option is using Selenium configured to run headless Chrome. The following options use Selenium: