Webdriver - open browser on full screen - firefox

I use Webdriver remotely with firefox.
I want to open my browser on full screen. The browser is opened on full screen, but immediately minimize and moves to other program which open on my OS. When I run my webdriver locally, the broser is opened on full screen, and doesn't minimize (it stays in the browser, and doesn't move to other program). I want that my browser would open on full screen, and stay in the browser, even if I run my test remottley.
The reason is that I used Java Robot, and I have to be in the browser in order that my action would be performed.
Thank you.

I don't know I really understood your question, but have a look at
driver.manage().window().maximize();

https://technicaltesting.wordpress.com/category/webdriver/#3
Especially handy when combining Sikuli and WebDriver, you need to be absolutely sure that as much as possible is visible on the screen.
1
((FirefoxDriver)driver).getKeyboard().pressKey(Keys.F11);
If you want to hide the header, please add this in the code.
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("browser.fullscreen.autohide",true);
profile.setPreference("browser.fullscreen.animateUp",0);
WebDriver driver = new FirefoxDriver(profile);
((FirefoxDriver)driver).getKeyboard().pressKey(Keys.F11);
driver.manage().window().setSize(new Dimension(1024, 768));
((JavascriptExecutor)driver).executeScript("window.focus()");
Robot robot;
try {
robot = new Robot();
robot.mouseMove(300,300);
robot.mouseMove(250,300);
} catch (AWTException e1) {
e1.printStackTrace();
}

Try this instead:
driver.manage().window().maximize();
((JavascriptExecutor) driver).executeScript("window.focus();");

In Python, you can do something like this. Make sure you use latest Selenium package.
driver = webdriver.Firefox()
driver.maximize_window()
driver.implicitly_wait(30)

in 2022:
from selenium import webdriver
f = webdriver.Firefox()
f.get('https://duckduckgo.com')
f.fullscreen_window()

I stumbled upon the same problem, but I found #Chen 's F11 profile not reliable enough, so I came up with my own solution: by simply making the browser window larger than the screen so the actual content matches up perfectly with the size of the screen.
WebDriver driver = new FirefoxDriver();
java.awt.Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Window window = driver.manage().window();
window.setPosition(new Point(-7, -87));
window.setSize(new Dimension((int) screenSize.getWidth() + 14, (int) screenSize.getHeight() + 94));

Related

Switch to Firefox browser using Selenium

Firefox browser only launches on Dock on my Mac but active screen shown is still Eclipse.
How can the focus be shifted to Browser?
public class FirefoxFirst {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.setProperty("webdriver.gecko.driver","/Users/varunnadimpalli/Downloads/geckodriver");
WebDriver driver = new FirefoxDriver();
driver.get("https://google.com");
selenium :3.3.1
Mac:`10.12.1
Strange why it wouldn't swap focus to firefox, if it is not in focus but still open try
((JavascriptExecutor) webDriver).executeScript("window.focus();");
If it is minimized you can try going through the windows handles
for(String winHandle : driver.getWindowHandles()){
driver.switchTo().window(winHandle);
}
See if that is able to bring the firefox window up.
Not sure whether this works. Try maximizing the browser,
driver.manage().window().maximize();
Also, don't forget to add implicit wait after initiating the browser.
Note - I don't have enough points to comment this. else i have commented instead of writing this as answer.

Firefox: Disable automatic safe mode after crash

How to prevent firefox showing the safe mode dialog after the crash?
It blocks the automatic selenium tests.
I have no idea how you got this and what your testing flow is. So I can't reproduce and test the solution. But Firefox Safe Mode can be disabled by setting the key toolkit.startup.max_resumed_crashes in about:config to -1.
Here's how to start Firefox with that preference set in C# binding:
FirefoxProfile profile = new FirefoxProfile();
profile.SetPreference("toolkit.startup.max_resumed_crashes", "-1");
IWebDriver driver = new FirefoxDriver(profile);
Since Firefox 23, you can also use the environment variable MOZ_DISABLE_AUTO_SAFE_MODE to disable this dialog and the automatic safe mode.
Did you take a look at the configuration options in about:config, there are options for safebrowsing which might be useful.
The solution by #mmm worked until the recent Tor Browser Bundle (which uses Firefox).
If you need an alternative, you can always delete the line
user_pref("toolkit.startup.recent_crashes", 4);
from prefs.js, for example like
sed -i '/toolkit\.startup\.recent_crashes/d' /path/to/prefs.js
or call this as
os.system("sed -i '/toolkit\.startup\.recent_crashes/d' " +
os.path.join('/path/to' + 'prefs.js"))

How to make Firefox open all links opened via WebDriver in the same window?

I want to open all links in the same window instead in new window.
I tried
profile.setPreference("browser.link.open_newwindow", 1)
but the result is:
WARNING: traffic.loop 0 error: Preference browser.link.open_external may not be overridden: frozen value=2, requested value=1
Is there an another way to open the links in the same window ?
You should modify the firefox profile parameters:
profile = webdriver.FirefoxProfile()
profile.set_preference("browser.link.open_newwindow", 3)
profile.set_preference("browser.link.open_newwindow.restriction", 0)
driver = webdriver.Firefox(firefox_profile=profile)
if this methode does not work, you can set perference using firefox Options:
from selenium.webdriver.firefox.options import Options
opts = Options()
opts.set_preference("browser.link.open_newwindow.restriction", 0)
opts.set_preference("browser.link.open_newwindow", 3)
driver = webdriver.Firefox(firefox_options=opts)
(A) browser.link.open_newwindow - for links in Firefox tabs :
3 : divert new window to a new tab (default)
2 : allow link to open a new window
1 : force new window into same tab
(B) browser.link.open_newwindow.restriction - for links in Firefox tabs
0 : apply the setting under (A) to ALL new windows (even script windows)
2 : apply the setting under (A) to normal windows, but NOT to script windows
with features (default)
1 : override the setting under (A) and always use new windows
I've found a workaround!
JavascriptExecutor js = (JavascriptExecutor) driver;
String script = "document.getElementById('yourFormOrAnchorId').target=''";
js.executeScript(script);
After that you can select your anchor or any of the form elements and click or submit it. The target page will open in the same tab.
This basically changes the current HTML page so that anchors and forms don't force the browser to open new tabs or windows. For testing this might be suboptimal, but it simplifies the writing of tests a lot.
Try this out...
Modify FireFox profile parameters "browser.link.open_newwindow.restriction" and "browser.link.open_newwindow".
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("browser.link.open_newwindow.restriction", 0);
profile.setPreference("browser.link.open_newwindow", 1);
If you are using Google Chrome then simply install this extension and it will take care of the rest of the task. This extension is also handy to open pop-ups in new tabs which usually opens in new windows. (First you need to download the extension .crx file from given location.)
ChromeOptions options = new ChromeOptions();
options.addExtensions(new File("/path/to/extension.crx"));
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
ChromeDriver driver = new ChromeDriver(capabilities);
In the selenium config file:
C:\Python27\Lib\site-packages\selenium\webdriver\firefox\webdriver_prefs.json
change the following line from:
"browser.link.open_newwindow": 2,
to:
"browser.link.open_newwindow": 3,
I test it and it worked
According to Selium docs (https://code.google.com/p/selenium/wiki/FirefoxDriver) the following property webdriver.firefox.profile controls the firefox profile used.
Which is where firefox gets the browser.link.open_newwindow on start up from. To create a new profile for your tests you can follow the instructions here https://support.mozilla.org/en-US/kb/profile-manager-create-and-remove-firefox-profiles detailed configuration of the profile can be done either by editing the profile's pref.js or firing up the profile and editing it via about:config.
hope this of help!
Actually, Selenium is not responsible of the page opens in a new window or in a same window. It is fully depends upon the Browser settings which you used for execution.
For a sake take Firefox browser
If you want to open all the links in a new window. Do these steps
Open Tools
Click Options
Click Tabs menu
Check the box of Open new windows in a new tab instead.
Now click the link which opens a window. It will opens in a new tab of same window.

Close a newly opened tab or window in Selenium IDE

Using Selenium IDE with Windows7 and Firefox, an automatic click on a link may produce either a new tab or a new window.
close() closes the original window or tab, not the new one. Maybe if I had the ID of the newly created one I could select it and then close it but I don't know how to do this automatically. I've asked on the Selenium forum and read the questions here, but they focus on WebDriver, not the IDE. Any help would be appreciated!
Stig
I had same problem and found a solution:
click on link that opens the new tab
add command waitForPopUp
set focus to new tab via command selectPopUp
make your verifications or other commands regarding to your content in new tab
use command close to close tab
use command selectWindow to set focus to the old window
Screenshot of my Selenium IDE commands:
That works for me.
Use selectWindow(windowID) command to switch to new window in Se IDE. You can select new window by its windowID/name/title.
Hope this helps...
Agreed with surya use selectWindow(windowID), you can get the window title by right click on the page check the option verifyTitle and in front of that you have your Title. Hope it helps out.
selectWindow(windowID) has option to mention title of the webpage/window as a way to identify the target window.
syntax = selectWindow title=My Special Window
note: title = title of webpage that is visisble in webpage's title bar. Or right click on webpage and select menu "Page source". In source doc, select the text between ....
So if the title of webpage = My Special Window, you will see My Special Window.
Hope this will help.
Swasati Paul
You could use the iterator to iterate amongst the windows, close the new window and come back to the originalWindow. And you can put this under the try block because it will only iterate if a new window opens. Or else, it will continue executing normally in the current window.
try{
Set <Strings> ids = driver.getWindowHandles();
Iterator <String> it = ids.iterator();
String currentPage = it.next();
String newPage = it.next();
driver.switchTo().window(newPage);
driver.close(); //it will close the new window and automatically come back to the currentPage
}
finally
{
//continue with your script here
//this script will run regardless of the execution of the execution of the try block
}

close firefox automatically when download is complete

I'm using selenium webdriver to do some downloading using firefox. At the moment my script waits for a specific time after download has inititated and then close the firefox. I want to know if there is a way to configure firefox to automatically close on download completions? Or using selenium webdriver, can I check if download has been completed? I don't want to use any add on, as it might add dependency in my script. I cant use wget/curl etc to download the files.
Thanks in advance
What Ignacio Contreras said. Polling the download path is possibly the best (most robust) solution.
Alternative #1:
Use a FirefoxProfile with Download Statusbar addon. It has a handy option to "Continue downloading in Download manager after window has been closed" (or something very similar), so that it will keep Firefox running in the background until the download has been finished.
Alternative #2:
Download the file directly using this (or any other similar WebDriver-friendly tool) ... or this, if you can. That will totally cut Firefox out of the process.
You can use Selenium WebDriver API's WebDriverWait class to do the polling using the following code:
(new WebDriverWait(driver, 180, 10)).until(new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver d) {
return !downloadedFilePart.exists();
}
});
The above code checks for file with .part extension program is downloading for every 10 seconds until either download completed or timed out after 3 minutes.
I'm not sure if this would work, but have you considered exploring trhough Firefox the download location? polling it until you see the download has been completed.
If I'm not wrong, when the file it's been downloaded, there should be an extra file with the extension .part.
Something like this (pseudocode):
...
WebDriver poller = new FirefoxDriver()
poller.get("path to download folder");
while ("file with .part extension is present") {
// Wait/sleep some time
// Refresh poller
}
// close downloading firefox instance
firefox.quit();
// close the polling instance
poller.quit();
Hope it helps

Resources