73

I'm looking at Selenium Server at the moment, and I don't seem to notice a driver that supports headless browser testing.

Unless I'm mistaken, it doesn't support it. If you're on X, you can create a virtual framebuffer to hide the browser window, but that's not really a headless browser.

Can anyone enlighten me? Does Selenium support headless browser testing?

Ripon Al Wasim
  • 36,924
  • 42
  • 155
  • 176
Chris
  • 4,594
  • 4
  • 29
  • 39

12 Answers12

19

you need not use PhantomJS as an alternative to Selenium. Selenium includes a PhantomJS webdriver class, which rides on the GhostDriver platform. Simply install the PhantomJS binary to your machine. in python, you can then use:

from selenium import webdriver
dr = webdriver.PhantomJS() 

and voila.

Magenta Nova
  • 691
  • 1
  • 9
  • 15
  • Headless Selenium Testing With Python and PhantomJS - https://realpython.com/blog/python/headless-selenium-testing-with-python-and-phantomjs/ – fragles Oct 01 '15 at 08:23
  • 4
    Don't use PhantomJS, it's not very reliable. People should be using Xvfb to run Chrome or Firefox. – ffxsam Nov 07 '15 at 00:14
  • 3
    update: PhantomJS is no longer supported given Chrome 51's `--headless` flag – Jay Wick Apr 19 '17 at 04:52
15

The WebDriver API has support for HTMLUnit as the browser for your testing. Ruby people have been using Capybara for a while for their headless selenium testing so it is definitely doable.

AutomatedTester
  • 22,188
  • 7
  • 49
  • 62
  • sorry, this is where i'm slightly confused - what is the difference between selenium & webdriver? – Chris Oct 02 '11 at 09:35
  • 1
    For a good description of Selenium and WebDriver see Ross Patterson's answer at http://stackoverflow.com/questions/6995016/what-is-the-difference-between-selenium-ide-server-rc-2-0-and-webdriver/6996322#6996322 – Craig Delthony Oct 17 '11 at 21:03
  • 1
    Old question but would be great if you could edit your answer to also include PhantomJS. – Arpit Rai Jul 29 '14 at 13:13
11

I know this is a old post. Thought it will help others who are looking for an answer.

You can install a full blown firefox in any linux distribution using XVFB. This makes sure your testing is performed in a real browser. Once you have a headless setup, you can use webdriver of your choice to connect and run testing.

Matt
  • 74,352
  • 26
  • 153
  • 180
Arun
  • 119
  • 1
  • 3
7

I notice that you say that using an X framebuffer isn't a true headless solution, however, for most, I think it would be acceptable. In addition to that, this service will help get that going for you if you are interested in that as a solution.

General Redneck
  • 1,240
  • 2
  • 13
  • 28
7

Headless browsers are a bad idea. They get you some testing, but nothing like what a real user will see, and they mask lots of problems that only real browsers encounter. You're infinitely better off using a "headed" browser (i.e., anything but HTMLUnit) on a headless environment (e.g., Windows, or Linux with XVFB).

Ross Patterson
  • 9,527
  • 33
  • 48
  • 16
    no, you want continuous integration, so headless is only logic. Why should it mask problems? Headless simply means no graphic device used. – Roland Kofler Dec 04 '12 at 12:14
  • 1
    @Roo The OP was clear in saying "_you can create a virtual framebuffer to hide the browser window, but that's not really a headless browser_" that he didn't mean "_no graphic device used_". In fact, you and I agree - run a real browser without a display and all will be well. – Ross Patterson Dec 04 '12 at 15:33
  • 1
    @Roo, I've seen headless tests fail when in practice the web app work fine in all browsers and the code is okay. And I've seen headless tests succeed when in practice the web app is broken at least in some browsers. I don't trust headless tests. They're better than nothing I suppose but should be combined with automatic tests of the most "important' browsers for the web app in question, think. – KajMagnus Jul 11 '15 at 07:08
  • @KajMagnus I've seen "headless" tests on xvfb fail when in practice they work on a real graphical environment. It's just the pain of having to work with webdriver at this stage of it's development. A lot of bugs, a lot of headaches, no real alternatives... – antimatter Dec 23 '15 at 21:06
2

Selenium does support headless browser testing in a way. Docker Selenium is maintained by SeleniumHQ. Those docker containers come with xvfb support with them out of the box. There are tools like PhantomJS that you can hook up with Selenium. However, it's not officially supported by Selenium itself.

Much like what others have described, PhantomJS isn't really recommended. The whole point of Selenium is to automate browsers. But why automate a browser no one uses? I never understood how that was overlooked so often by developers..

Sean
  • 779
  • 6
  • 18
2

Yes. Selenium support headless browser testing and it's more faster as well as convient for big amount of test-cases execution.

ChromeOptions cromeOptions = new ChromeOptions();

//Location of browser binary/.exe file
cromeOptions.setBinary("/usr/bin/google-chrome-stable");

cromeOptions.addArguments("--headless");
cromeOptions.addArguments("--no-sandbox");
cromeOptions.addArguments("--disable-gpu");
cromeOptions.addArguments("--window-size=1920,1080");

WebDriver webDriver = new ChromeDriver(cromeOptions);
Radadiya Nikunj
  • 988
  • 11
  • 10
0

Yes ,selenium supports headless browser testing...but i found HTMLUnit failing most times...I was searching for an alternative...PhantomJs was really good.you can definitely give it a try it was very fast when compared to other browsers...It is really good for smoke testing...

http://phantomjs.org/

Vicky
  • 2,999
  • 2
  • 21
  • 36
0

With ruby and macOS: brew install phantomjs then:

driver = Selenium::WebDriver.for :phantomjs
Dorian
  • 22,759
  • 8
  • 120
  • 116
0

Here's a "modern answer" on how to use Selenium with xvfb and Firefox driver in an Ubuntu Linux environment running Django/Python:

# install xvfb and Firefox driver
sudo su
apt-get install -y xvfb firefox
wget https://github.com/mozilla/geckodriver/releases/download/v0.19.1/geckodriver-v0.19.1-linux64.tar.gz
tar -x geckodriver -zf geckodriver-v0.19.1-linux64.tar.gz -O > 
/usr/bin/geckodriver
chmod +x /usr/bin/geckodriver

# install pip modules
pip install selenium
pip install PyVirtualDisplay

You can then follow the Django LiveServerTestCase instructions.

To use the driver you just installed, do something like this:

from pyvirtualdisplay import Display
from selenium.webdriver.firefox.webdriver import WebDriver

driver = WebDriver(executable_path='/usr/bin/geckodriver')
display = Display(visible=0, size=(800, 600)).start()

# add your testing classes here...

driver.quit()
display.stop()
Cloud Artisans
  • 4,016
  • 3
  • 30
  • 37
0

Yes Selenium supports headless browser testing.Headless browsers are faster than real time browsers.

Siva K
  • 21
  • 5
0

Install chromeDriver and google-chrome-stable version on the linux server, where the tests will be triggered and add the same binaries in your code.

code snippet:

private static String driverPath = "/usr/bin/chromedriver";
static
{
    System.setProperty("webdriver.chrome.driver", driverPath);
    options = new ChromeOptions();
    options.setBinary("/usr/bin/google-chrome-stable");
    options.addArguments("headless");
    driver = new ChromeDriver(options);
}
gehbiszumeis
  • 3,525
  • 4
  • 24
  • 41