Selenium Web Driver - How to run Firefox in the Background - java

Every time I run my tests on Firefox browser the browser takes active control and kind of disturbs/interrupts my work. Is there way/command to make it run on the background.

You can set position of the browser outside of your screen:
FirefoxDriver firefoxDriver = new FirefoxDriver();
firefoxDriver.manage().window().setPosition(new Point(-x, -y));
See Also:
How to hide Firefox window (Selenium WebDriver)?
How to run ghostdriver with Selenium using java

As there isn't any accepted answer - you can download firefox addon and install it with your instance of firefox. Addon is named FireTray it can launch firefox minimized in tray so there won't be any poping up windows etc. It can hide 1 window but with litle tinkering with addon you can change that value to anything. Here is some code to help you understand what I'm talkim about :)
FirefoxProfile prof = new FirefoxProfile(new File(FIRE_TRAY_PATH));
WebDriver driver = new FirefoxDriver(prof);
Now you have pseudo-background firefox browser :)

You could try using an HtmlUnitDriver with Firefox capabilities for a 'headless' effect.
new HtmlUnitDriver(DesiredCapabilities.firefox())
HtmlUnitDriver doesn't play nice with javascript interactions all the
time
Alternatively, add a resize to your existing driver on startup to hide the window.
driver.manage().window().setSize(new Dimension(0, 0));
This might have an impact on the driver's ability to resolve
WebElements that are not on the page and change your behavior. I'm
not sure

Related

Running Selenium in headless mode via Linux causes errors

I have a large set of tests running on a site.
When I run the test locally on Windows - they all pass on 100%. The test are designed and running on Google Chrome.
Now, we have started to run the tests on Linux via Jenkins jobs on headless mode. Some tests now fail on 0% or only pass on 20% or even 10%. In my code I'm finding elements by ID, xpath or css and simple click on them. And I use the WebDriverWait object for waiting - both for the element to be present and to be clickable.
Example of my code:
WebDriverWait wait = new WebDriverWait(browser, secondsToWait);
wait.until(ExpectedConditions.presenceOfElementLocated(By.id(elementID)));
lastFoundElement = wait.until(ExpectedConditions.elementToBeClickable(By.id(elementID)));
clickLastFoundElement();
In my report I see mostly that the elements were not found and that I passed the timeout set in the wait object.
How to make headless tests be more stable?
Why the headless state causes so many problems?
It is actually known issue to Selenium community that headless is not stable as it should be, read here about the issue more
Chrome runs very unstable when headless mode is activated.
There are multiple different issues and bugs depending on: Chrome Version, ChromeDriver Version and the executed tests.
Issues and Fixes (occured so far):
Chrome does not start in headless mode
Exception:
No informative Error message. Only a timeout exception when navigate() is called on the driver:
org.openqa.selenium.TimeoutException: timeout
Fix:
options.addArguments("--proxy-server='direct://'");
options.addArguments("--proxy-bypass-list=*");
Chrome is very slow in headless mode
Fix:
options.addArguments("--proxy-server='direct://'");
options.addArguments("--proxy-bypass-list=*");
Chrome does not operate correctly after a certain time, when tests run very long resp. many actions are executed in one test session
Exception:
... Timed out receiving message from renderer: ...
Fix (not tested yet!):
options.addArguments("--disable-browser-side-navigation");
You possibly missing setting the headless window size.
Try this settings:
Map<String,String> prefs = new HashMap<>();
prefs.put("download.default_directory", downloadsPath); // Bypass default download directory in Chrome
prefs.put("safebrowsing.enabled", "false"); // Bypass warning message, keep file anyway (for .exe, .jar, etc.)
ChromeOptions opts = new ChromeOptions();
opts.setExperimentalOption("prefs", prefs);
opts.addArguments("--headless", "--disable-gpu", "--window-size=1920,1080","--ignore-certificate-errors","--no-sandbox", "--disable-dev-shm-usage");
driver = new ChromeDriver(opts);
Here I put much more setting used by me for the headless chrome.
I hope this will be useful for you.
Please include screen size in the chrome option.
ChromeOptions options = new ChromeOptions();
options.addArguments(Arrays.asList("--window-position=0,0"));
options.addArguments(Arrays.asList("--window-size=1840,1080"));

Headless Chrome - getting blank page source

I'm trying to load a website with Chrome browser in headless mode using Selenium web driver. I face an issue with some specific websites. The page is loading, in the first 2-3 seconds it shows a page with "please enable javascript..." and after 3 seconds, page source goes blank.
I'm using Selenium and especially Chrome for long time and I am familiar with the platform. For the purpose of this case, I'm using Chrome Version 73.0.3683.86 , ChromeDriver 2.46.628411 (which is compatible according to Which ChromeDriver version is compatible with which Chrome Browser version?) on a Mac OS. selenium java version is latest - 3.141.59
I suspect that headless Chrome cannot handle specific content-type such as "svg" and any other GUI related HTTP response.
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("--headless");
WebDriver driver = new ChromeDriver(chromeOptions);
driver.get("https://identity.tescobank.com/login");
Thread.sleep(3000);
System.out.println(driver.getPageSource());
driver.quit();
Expected result is to have the page source same as it is showing in non-headless mode.
Headless Chrome should be able to handle everything the normal Chrome can do:
It brings all modern web platform features provided by Chromium and the Blink rendering engine to the command line.
(see https://developers.google.com/web/updates/2017/04/headless-chrome)
Since only the login page of a bank causes you trouble, my guess is that the security of the page detects an anomaly and decides not to serve you.
One way they can do that is by looking at the User Agent string which contains HeadlessChrome.
That said, unless you're writing integration tests for the bank, your behavior is at least suspicious. If you have a valid and legal concern, clear it with the bank first. They might take actions against you, otherwise. Blocking your IP address (which could affect many people) or asking the police to have a word with you.
I was facing similar issue in my script, after login. Somehow refreshing the page resolved the issue.
driver.navigate().refresh();

Disable developer mode extensions error in Selenium Java

I am working with Java Selenium. I came across the following error:
System.setProperty("webdriver.chrome.driver", "D:\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://www.gezinomi.com/");
Picture of error:
This is not an error, since if you click Cancel option, it'll just follow the path normally.
However, it is probably causing the test to fail, once the browser remains waiting the confirmation. You can disable it through Selenium Java code, as already shown here:
ChromeOptions options = new ChromeOptions();
options.addArguments("chrome.switches","--disable-extensions");
System.setProperty("webdriver.chrome.driver", "D:\\chromedriver.exe");
WebDriver driver = new ChromeDriver(options);
driver.get("http://www.gezinomi.com/");
That popup was problematic while running tests remotely on SauceLabs. I tried the ChromeOptions argument "--disable-extensions" which didn't work. I gave up, accepting that a chunk of screen would not be visible.
As soon as I stopped researching, I came across this reference which clearly defines the Chrome option as "--disable-extensions-file-access-check".
I don't know why this was so hard to find, but hopefully it can help others waste LESS of their time.

Selenium 2.32, Java 1.6.0_07, IE Webdriver (32 and 64 Bit), IE9 - getWindowHandles returns only one browser

I am using Selenium 2.32, Java JDK 1.6.0_07, IE9 with Windows 7. Here is the problem
When i use IE WebDriver 32 Bit and click on a link which opens a new browser containing PDF, the PDF is opened in the browser itself which is fine, but the new browser is not identified when i use driver.getWindowHandles(). It always returns only the parent window. When i use the same code with IE8, it works perfectly fine and i am able to get the URL of the new browser.
I thought since it is Windows 7 and IE9, i should use IE Webdriver 64 bit and so i used IE Webdriver version 2.32.3 (64 Bit). With this webdriver, when i click on the link, the new browser pops up but the PDF is not opened in the browser and instead it is opened as a separate PDF file. Even in this case, the new browser is not identified and driver.getWindowHandles() returns only one browser.
Not just the PDF browsers but also the normal browsers are not returned by driver.getWindowHandles()
I am using a wait of 10 seconds for the new browser to load and so the there is no load/sync issue.
I want to identify the new browser and get the URL of the new browser. Please help.
The problem here is that you are making things too complicated. From your comments, it does not seem you are doing things the "typical" and "recommended" way. If you are following advice, then you are doing it the slightly harder route. My advice is still to simplify further.
If I were to guess on your issue though: I notice that you say you are using "IEDriverServer". That tells me that you may be improperly using WebDriver. When you are using a Grid Hub and a separate Grid Node ( see my link here for sample launch instructions: https://gist.github.com/djangofan/5174433 ) then you should be invoking RemoteWebDriver rather than WebDriver, like so (or similar):
WebDriver driver = new RemoteWebDriver (
new URL("http://localhost:4444/wd/hub"),
DesiredCapabilities.firefox()
);
driver.get("http://www.google.com");
This work for me:
Root cause: On IE 7 or higher on Windows Vista or Windows 7, you must set the Protected Mode settings for each zone to be the same value. The value can be on or off, as long as it is the same for every zone. To set the Protected Mode settings, choose "Internet Options..." from the Tools menu, and click on the Security tab. For each zone, there will be a check box at the bottom of the tab labeled "Enable Protected Mode".
Hope it works for you.

How to open a new tab in the same browser by using Selenium WebDriver with Java?

I can open a new window with Selenium WebDriver by using Java and JavaScript. I am using Firefox. The code is as follows:
WebDriver driver = new FirefoxDriver();
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("function createDoc(){var w = window.open(); w.document.open(); w.document.write('<h1>Hello World!</h1>'); w.document.close();}; createDoc();");
How can I open a new tab in the same browser by using WebDriver (Selenium 2)?
Wasim,
cdriver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"t");
You can use above line to open new tab in a same browser (Works in Firefox)
There is no standard support in JavaScript or HTML for opening a link in a tab vs a window. It's depending on browser and settings. Some browsers default to opening in new tabs (like Chrome and Safari). Some browser allows the user to configure the behavior. Bottom line, you shouldn't design your site to rely on opening new windows in tabs as there is no reliable and cross-browser compatible mechanism for doing that.
It is mostly depends on your browser settings and there is no separate methods for opening link in new window or new tab
It also depends on how your browser is configured to open pop-up's.

Categories

Resources