Exporting the DISPLAY
variable is definitely the solution but depending on your setup you may have to do this in a slightly different way.
In my case, I have two different processes: the first one starts Xvfb, the other one launches the tests. So my shell scripting knowledge is a bit rusty but I figured out that exporting the DISPLAY
variable from the first process didn't make it available in the second process.
Fortunately, Selenium WebDriver allows you to 'redefine' your environment.This is my function for creating a driver for Chrome in JS. Pretty sure the equivalent exists for your programming language:
const caps = require('selenium-webdriver/lib/capabilities');const chrome = require('selenium-webdriver/chrome');const chromedriver = require('chromedriver');module.exports = function (cfg) { let serviceBuilder = new chrome.ServiceBuilder(chromedriver.path); let options = chrome.Options.fromCapabilities(caps.Capabilities.chrome()); let service; let myENV = new Map(); // 're-export' the `DISPLAY` variable myENV.set('DISPLAY', ':1'); serviceBuilder.setEnvironment(myENV); service = serviceBuilder.build(); options.addArguments('disable-setuid-sandbox'); options.addArguments('no-sandbox'); options.addArguments('allow-insecure-localhost'); options.excludeSwitches('test-type'); return chrome.Driver.createSession(options, service);};