Running WebDriver without opening actual browser window
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?
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:
If you google for Selenium and XVFB you'll find lots more info.
It is quite easy to hide the browser without XVFB. Just install PhantomJS. Then, change this line:
driver = webdriver.Firefox()
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.
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
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.
Reminder: if you use HtmlUnitDriver, there is no guarantee the same tests will actually work in a real browser.
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?
The syntax changes from language to language, but the Logic what you are trying to test remains the same
@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.
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)
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.