selenium-chromedriver-headless-monoIf you want to run C# application (mono) with Selenium ChomeDriver on Ubuntu Server 16.04 in headless mode, you definitely should read this article. We will use Xvfb as X server, this will let us emulate "headless" mode because Xvfb performs all graphical operations in memory without showing any screen output.

Install mono

sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 3FA7E0328081BFF6A14DA29AA6A19B38D3D831EF
echo "deb http://download.mono-project.com/repo/debian wheezy main" | sudo tee /etc/apt/sources.list.d/mono-xamarin.list
sudo apt-get update
sudo apt-get install mono-complete
sudo apt-get install referenceassemblies-pcl

(official docs)

Install Google Chrome

wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | sudo apt-key add - sudo sh -c 'echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google.list'
sudo apt-get update 
sudo apt-get install google-chrome-stable

Install Xvfb

apt-get install xvfb imagemagick

Run ChromeDriver in headless mode

We will use following application for test:

class Program
{
    static void Main(string[] args)
    {
        var driver = new ChromeDriver();
        driver.Navigate().GoToUrl("http://ivanderevianko.com");

        Console.WriteLine("Started. Press Enter to exit");
        Console.ReadLine();
    }
}
chmod +x chromedriver

For example, you compiled your app to /home/druss/Selenium/TestChromeDriver.exe

xvfb-run --server-args='-screen 0, 1920x1080x24' --auth-file=/home/druss/.Xauth -a mono /home/druss/Selenium/TestChromeDriver.exe

You should see an output from the program.

To run your program in background mode:

xvfb-run --server-args='-screen 0, 1920x1080x24' --auth-file=/home/druss/.Xauth -a mono /home/druss/Selenium/TestChromeDriver.exe &> /dev/null &

Make screenshots

You can make a screenshot of the running process. Every time you start xvfb-run with -a parameter it searches for free display (starts from 99) and uses it as an output.

To access this data you should set one variable:

--auth-file=/home/druss/.Xauth

After this you can take screenshot:

DISPLAY=:99 import -window root testChromeDriver.png