Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any way to execute the UI tests in headless mode in Selenium version 4 and heigher?

Recently Selenium released their major version as Selenium 4 and announced there will not be any support further for PhantomJS from Selenium4 onwards. Is that mean Selenium no longer supports for headless automation or is there any way to execute the tests in headless mode in Selenium version 4? Appreciate code examples.

like image 438
Jesrell Ramillo Avatar asked Nov 27 '25 02:11

Jesrell Ramillo


1 Answers

In Selenium 4, native support has been removed for PhantomJS. Still, the users who run the scripts on headless mode using PhantomJS, can use Chrome or Firefox in headless mode as shown below.

const chrome = require('../chrome');
const firefox = require('../firefox');
const {Builder, By, Key, until} = require('..');

const width = 640;
const height = 480;

let driver = new Builder()
    .forBrowser('chrome')
    .setChromeOptions(
        new chrome.Options().headless().windowSize({width, height}))
    .setFirefoxOptions(
        new firefox.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; }));

To learn more about Selenium 4 changes please refer Selenium 4 (alpha) is Released: What’s New?

like image 72
Vijendran Selvarajah Avatar answered Nov 29 '25 14:11

Vijendran Selvarajah



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!