ChatGPT解决这个技术问题 Extra ChatGPT

Does Selenium support headless browser testing?

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?


M
Magenta Nova

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.


Headless Selenium Testing With Python and PhantomJS - realpython.com/blog/python/…
Don't use PhantomJS, it's not very reliable. People should be using Xvfb to run Chrome or Firefox.
update: PhantomJS is no longer supported given Chrome 51's --headless flag
A
AutomatedTester

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.


sorry, this is where i'm slightly confused - what is the difference between selenium & webdriver?
For a good description of Selenium and WebDriver see Ross Patterson's answer at stackoverflow.com/questions/6995016/…
Old question but would be great if you could edit your answer to also include PhantomJS.
M
Matt

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.


R
Ross Patterson

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).


no, you want continuous integration, so headless is only logic. Why should it mask problems? Headless simply means no graphic device used.
@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.
@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 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...
G
General Redneck

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.


S
Sean

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..


R
Radadiya Nikunj

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);

V
Vicky

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/


D
Dorian

With ruby and macOS: brew install phantomjs then:

driver = Selenium::WebDriver.for :phantomjs

S
Siva K

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


g
gehbiszumeis

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);
}

C
Cloud Artisans

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()