When I execute multiple test simultaneously, i don\'t want to keep Firefox browser window visible.. I can minimize it using selenium.minimizeWindow()
but I don\
I found the easiest way was to use PhantomJS, per Stéphane's suggestion. I downloaded the binary and put phantomjs in my PATH, in my case (Mac OS) in /usr/bin/. I like to retain the option of seeing what's going on so I wrapped it like this (in Python):
def new_driver():
if 'VISIBLE_WEBDRIVER' in os.environ:
return webdriver.Firefox()
else:
return webdriver.PhantomJS()
References:
http://blog.likewise.org/2013/04/webdriver-testing-with-python-and-ghostdriver/
http://www.realpython.com/blog/python/headless-selenium-testing-with-python-and-phantomjs/
Just do (Python):
opts = webdriver.FirefoxOptions()
opts.headless = True
firefox = webdriver.Firefox(options=opts)
In Java, you can use HtmlUnitDriver to launch a headless browser session which will not actually open the browser.
Add the following dependency to your pom.xml (or download and reference the following):
<dependency>
<groupId>net.sourceforge.htmlunit</groupId>
<artifactId>htmlunit</artifactId>
<version>2.15</version>
</dependency>
... and test it it as you would a WebDriver driver instance:
driver = new HtmlUnitDriver();
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
driver.get("http://www.google.com");
// etc..
driver.quit();
Another similar question in SO: Avoid opening browser on remote server during selenium call
Finally I found the solution for those who are using windows Machine for running the Tests using any method. Well, implementation is not in Java, but you can do it very easily.
Use AutoIt
tool. It has all the capability to handle windows. It is a free tool.
Install AutoIt: http://www.autoitscript.com/site/autoit/downloads/
Open the Editor and write below code for Hiding any window.
AutoItSetOption("WinTitleMatchMode", 2)
WinSetState("Title Of Your Window", "", @SW_HIDE)
To Unhide it, you can use below line of code.
AutoItSetOption("WinTitleMatchMode", 2)
WinSetState("Title Of Your Window", "", @SW_SHOW)
WinTitleMatchMode
has different options which can be used to match Windows title.
1 = Match the title from the start (default)`
2 = Match any substring in the title
3 = Exact title match
4 = Advanced mode, see Window Titles & Text (Advanced)
So, what I've done is: I have created an .exe file of a small program and passed a parameter as a command line argument as below.
Runtime.getRuntime().exec("C:/Diiinnovation/HideNSeek.exe 0 \"" + "Mozilla Firefox" + "\"");
in HideNSeek.exe
- I have below AutoIt Code:
AutoItSetOption("WinTitleMatchMode", 1)
if $CmdLine[0] > 0 Then
if $CmdLine[1] == 0 Then
WinSetState($CmdLine[2], "", @SW_HIDE)
ElseIf $CmdLine[1] == 1 Then
WinSetState($CmdLine[2], "", @SW_SHOW)
Else
EndIf
EndIf
$CmdLine[]
is an array, which will have all command line parameters...
$CmdLine[0] = number of Parameter
$CmdLine[1] = 1st Parameter after Exe Name
...
If there is any space in the Window Title, then you have to use double quotes to pass it as a command line parameter like above.
Below Line of code will execute AutoIt exe and if I pass '0' in 1st parameter then it will hide the window and if I will pass '1' then it will unhide windows matching the title.
Runtime.getRuntime().exec("C:/Diiinnovation/HideNSeek.exe 0 \"" + "Mozilla Firefox" + "\"");
I hope this will help you. Thanks!
The default browser of PhantomJS is IE, though many browser features do not work there. If you want to open a headless(hidden) Firefox window, you can use the new feature of Firefox 56+.
With this feature you can get a headless
driver like this:
System.setProperty("webdriver.gecko.driver", firefoxDriverExePath);
FirefoxOptions options = new FirefoxOptions();
options.addArguments("--headless");
FirefoxDriver driver = new FirefoxDriver(options);
New versions of Chrome also have the headless
option.
The easiest way to hide the browser is to 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 or just switch to the Firefox webdriver again.
On Windows, you will have to specify the path to phantomjs.exe:
driver = webdriver.PhantomJS('C:\phantomjs-1.9.7-windows\phantomjs.exe')
Have a look at Ghost Driver: How to run ghostdriver with Selenium using java
C#
How to hide FirefoxDriver (using Selenium) without findElement function error in PhantomDriver(headless browser)?