19

I've heard rumors that Selenium WebDriver can run without opening a real browser window, meaning it runs in the background. I am using the C# Client Drivers, FirefoxDriver within the automated tests, and launching a class library project (containing my code) using NUnit.

  • If so, how can this be done?
  • Also, what is the benefit to doing it this way?
  • Do you still have to keep the waits (thread.sleep and implicit waits) in the code since the browser isn't open?
13

One option is HTMLUnit, which is headless but has its own proprietary JavaScript rendering engine, so it is possible that it will behave differently than existing browsers (If you do use HTMLUnit, don't forget to enable JavaScript when you instantiate it).

The second option is to use XVFB; this will run the tests in a virtual frame buffer environemt. This has been reported quite a few times on the Selenium users mailing list as well as the Selenium blog, see this post for some XVFB implementations.

Linking directly to the tutorials:

  1. functional-headless-ui-testing-django-selenium
  2. Watch player_embedded

If you google for Selenium and XVFB you'll find lots more info.

15

It is quite easy to hide the browser without XVFB. Just install PhantomJS. Then, change this line:

driver = webdriver.Firefox()

to:

driver = webdriver.PhantomJS()

The rest of your code won't need to be changed and no browser will open. For debugging purposes, use driver.save_screenshot('screen.png') at different steps of your code.

  • is it the same for Chrome tests? – someone Jan 15 '16 at 18:20
  • But if you have javascript you may have issues. If you use Chrome you may have cross-browser scripting issues. @nexoma's answer might address this but not sure what the deal is with javascript for chrome headless. – Michael Durrant May 29 '17 at 18:10
  • I believe PhantomJS has JS in its name for a reason; it perfectly handles Javascript – Not sure why Chrome is being mentioned here – Stéphane Bruckert May 29 '17 at 18:22
4

You can attempt to run your tests inside an instance of the HtmlUnitDriver. I attempted to look at the documentation on this, but the chm file inside of the latest release seems to be botched.

There is also WebkitDriver, but that seems to be Java only for the time being.

  • 3
    Reminder: if you use HtmlUnitDriver, there is no guarantee the same tests will actually work in a real browser. – user246 Feb 8 '12 at 18:46
  • I read on the WebDriver wiki today that HTMLUnitDriver only works with the Java client drivers, so I won't be able to use this approach, since we use the C# client drivers. :-\ But I appreciate the input! – MacGyver Feb 9 '12 at 16:21
4

You can RUN the Test using HTMLUnitDriver, its the fastest and light weight of most of webdrivers

  • We use the C# drivers and NUnit, so I would have to re-write everything in Java, wouldn't I? – MacGyver Feb 9 '12 at 16:19
  • The syntax changes from language to language, but the Logic what you are trying to test remains the same – saikrishna Feb 13 '12 at 7:22
  • 1
    @MacGyver: No, you don't have to switch the language, think of HTMLUnit as the browser that is started, not as a testing framework. It does not matter for you in what language it was written, like it does not matter for firefox etc. At the very minimum you can always start the selenium server and connect to it using the webdriver bindings, specifying you want HTMLUnit. In C#: IWebDriver drive = new RemoteWebDriver(DesiredCapabilities.HtmlUnit()); There might be also a HTMLUnitDriver (for connecting directly) in the C# bindings, as C# belongs to the four officially supported languages. – zpea Jul 11 '12 at 18:47
4

You used to be able to use PhantomJS for this, but since PhantomJS itself is discontinued, so is the Selenium PhantomJS driver.

The good news is, that you can simply use Chrome. Chrome itself has gotten the feature of being able to run without an actual browser window.

So after instantiating your chrome driver, you can simply add instructions to work headless

options = webdriver.ChromeOptions();
options.add_argument('headless');
options.add_argument('window-size=1200x600'); // optional

source

2

simply use the below code to run your current chosen browser as a headless browser, headless browser means that it will not open a browser when you run your code.

    from selenium.webdriver.chrome.options import Options
    options = Options()
    options.add_argument("--headless")
    self.driver = webdriver.Chrome(options=options)
2

Alternatively,

We need to import atleast the following packages:

from selenium import webdriver   # for webdriver
from selenium.webdriver.support.ui import WebDriverWait  # for implicit and explict waits
from selenium.webdriver.chrome.options import Options  # for suppressing the browser

In the program:

option = webdriver.ChromeOptions()
option.add_argument('headless')
driver = webdriver.Chrome('path/to/chromedriver',options=option)

Then simply continue your task as usual.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.