Apply settings to Selenium's FirefoxDriver - java

I am trying to run the org.openqa.selenium.firefox.FirefoxDriver in Java.
I would like to open a browser window without the address bar and other functionalities like page-forward and so on.
Actually I would like to have a plain window with the displayed web content and even disabled context menu.
How to archive that.
I tried with the code:
FirefoxOptions op = new FirefoxOptions()
.addPreference("browser.startup.page", 1)
.addPreference("browser.startup.homepage", "https://google.com");
WebDriver driver = new FirefoxDriver();
but could not find any good, easy understandable documentation for preferences or arguments (addArgument(...)).
Could anyone give me a hint here?
These resources also did not give me a good understandable help:
https://developer.mozilla.org/en-US/docs/Web/WebDriver/Capabilities/firefoxOptions
https://searchfox.org/mozilla-central/source/modules/libpref/init/all.js
How to correctly use FirefoxOptions() and its arguments to pass it to the FirefoxDriver() constructor
Other threads I found on the internet are many years old.
As bonus, the same links for Chrome would also be appreciated.

This is a solution what I almost perfectly want. It does not deal with Selenium/Selenide, which is fully ok with me. I made it work nicely with Chrome browser.
See: https://github.com/greyshine/javaspring-embedded-browser
Most interesting files are the index.html and BrowserAdaptor.java.
What I still miss:
A nice way independent on the OS on how to locate the executable for the browser; see application.properties
Firefox does not open with a pure screen but still has tabs and some browser functions. What would be the startup parameters (see application.properties) for firefox to correctly start.
Same story for the Edge browser. Startup parameters, etc...
Bringing Selenide into the question was based, that I seen that this must be somehow possible.

Related

Selenium Webdriver Modal not appearing when automated

I am writing automation code using Selenium Webdriver Java against an application for Point of Sales systems.
When the application opens up at first there is a modal dialog which appears for the user to select an item on. This modal appears no problem when testing manually. Until just recently it also appeared when running automation.
It no longer does. The behavior is different between manual and automated runs.
I'm running against Chrome and I've set the "disable-popup-blocking" option for the Chrome driver, but it doesn't seem to be helping.
The developers also have no idea what might have changed. I am at a loss and just looking for any thoughts about where to look for clues.
Thanks.
Let me try to address your query:
The behavior is different between manual and automated runs is factually incorrect statement until & unless you are controlling the WebDriver instance with arguments.
As you mentioned there is a modal dialog which significantly means that the code for the dialog is present in the DOM. Maybe it's within​ a frame which we have to find out.
"disable-popup-blocking" option for the Chrome - The purpose of this option have nothing to do with element present in the HTML DOM.
Amidst all the confusion, the best solution may be to take help of Selenium Builder & put an end to all guesses.
Let me know if this answers your question.

Selenium/ Java not working with IE on Windows

Can anyone please help me with Selenium + Java + IE + Windows code and settings to be done step by step? There are so many conflicting answers over the web and none of those is working (due to lack of my understanding perhaps, I am basically shell and python programmer and shifted to test automation recently). Please help me with code and required settings. Screenshots of settings will be highly appreciated (I am new to windows as a programming environment, I have extensive command line programming experience on *nix systems on shell and python as mentioned earlier).
The problem is that the "Internet Explorer" window is opened, however the test cases are not executed at all and after waiting for some time the Eclipse console shows error.
Thanks,
P Ashwin
Ensure you download the IE Driver Server. You'll need 32 bit or 64 bit to match your OSes "bitness."
The executable should be on your path. You can point to it in code as Aramin called out above; however, I prefer to have the executable on the path--doing it in code is just one more configuration mess you have to deal with if you run on different systems.
Note that you'll need to restart any shells/command windows after you add the server's executable to your system environment variables.
After that it's just a matter of firing it up in code and looks like any other WebDriver:
WebDriver browser;
public void initialize() {
browser = new InternetExplorerDriver();
browser.navigate().to("http://the-internet.herokuapp.com");
}
Note: This is from a canned set of demos I use for training. It runs under Cucumber, normally with an #Before tag. I took that out to avoid confusion.
HTH
Code :
System.setProperty("webdriver.ie.driver", "D:\\IEDriverServer.exe");
WebDriver driver= new InternetExplorerDriver();
driver.get("http://www.w3schools.com");
Along with this you need to enable scripting for IE(Internet Options->Security->Custom level ) as shown in ]1
Hope this will help you..

How to interact with Chrome extension installation dialog in Selenium? [duplicate]

I am using selenium for some browser automation. I need to install an extension in the browser for my work. I am doing it as follows:
import selenium
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
executable_path = "/usr/bin/chromedriver"
options = Options()
options.add_extension('/home/TheRookie/Downloads/extensionSamples/abhcfceiempjmchhhdhbnkbimnfpckgl.crx')
browser = webdriver.Chrome(executable_path=executable_path, chrome_options=options)
The browser is starting fine but I am prompted with a pop-up to confirm that I want to add the extension as follows:
and after I get this pop-up, Python soon returns with the following exception:
selenium.common.exceptions.WebDriverException: Message: u'unknown
error: failed to wait for extension background page to load:
chrome-extension://abhcfceiempjmchhhdhbnkbimnfpckgl/toolbar.html\nfrom
unknown error: page could not be found:
chrome-extension://abhcfceiempjmchhhdhbnkbimnfpckgl/toolbar.html\n
(Driver info: chromedriver=2.12.301324
(de8ab311bc9374d0ade71f7c167bad61848c7c48),platform=Linux
3.13.0-39-generic x86_64)'
I tried handling the popup as a regular JavaScript alert using the following code:
alert = browser.switch_to_alert()
alert.accept()
However, this doesn't help. Could anyone please tell me how do I install this extension without the popup or a way to accept the popup? Any help would be greatly appreciated. Thanks!
Usually, you cannot test inline installation of a Chrome extension with just Selenium, because of that installation dialog. There are a few examples in the wild that show how to use external tools outside Selenium to solve this problem, but these are not very portable (i.e. platform-specific) and rely on a state of Chrome's UI, which is not guaranteed to be consistent.
But that does not mean that you cannot test inline installation. If you replace chrome.webstore.install with a substitute that behaves like the chrome.webstore.install API (but without the dialog), then the end-result is the same for all intents and purposes.
"Behaves like chrome.webstore.install" consists of two things:
Same behavior in error reporting and callback invocation.
An extension is installed.
I have just set up such an example on Github, which includes the source code of the helper extension/app and a few examples using Selenium (Python, Java). I suggest to read the README and the source code to get a better understanding of what happens: https://github.com/Rob--W/testing-chrome.webstore.install.
The sample does not require the tested extension to be available in the Chrome Web store. It does not even connect to the Chrome Web store. In particular, it does not check whether the site where the test runs is listed as a verified website, which is required for inline installation to work.
I had some really big code which I would have to re-write if I had to use Java. Luckily, python has a library for automating GUI events called ldtp. I used that to automate the clicking on the "Add" button. I did something on the following lines:
from ldtp import *
from threading import Thread
import selenium
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
def thread_function():
for i in range(5):
if activatewindow('Confirm New Extension'):
generatekeyevent('<left><space>')
break
time.sleep(1)
def main():
executable_path = "/usr/bin/chromedriver"
options = Options()
options.add_extension('/home/TheRookie/Downloads/extensionSamples/abhcfceiempjmchhhdhbnkbimnfpckgl.crx')
thread.start()
browser = webdriver.Chrome(executable_path=executable_path, chrome_options=options)
Hope it helps somebody.

How to use Selenium WebDriver to Download APK Files

I am trying to download an .apk file using SeleniumWebDriver on FireFox.
I have set the profile to auto-save, but when I click the .apk file download link, it opens the download confirmation dialogue.
How should I proceed moving forward?
Here is my code:
FirefoxProfile fprofile = new FirefoxProfile();
fprofile.setPreference("browser.download.folderList", 2);
fprofile.setPreference( "browser.download.manager.showWhenStarting", false );
fprofile.setPreference("browser.download.dir", "D:\\WebDriverdownloads");
fprofile.setPreference("browser.helperApps.alwaysAsk.force", false);
fprofile.setPreference("browser.helperApps.neverAsk.saveToDisk",
"application/vnd.android.package-archive;"); //MIME Type for APK files
driver = new FirefoxDriver(fprofile);
As far as I know there is no easy way to download files with Selenium because browsers use native dialogs. Check this link.
AutoIT was helpful for me, hope it helps you too.
In my code, I just have following preferences and it works fine. Looks like you do have these options already.
"browser.download.folderList": 2,
"browser.download.dir": "/Users/nilesh",
"browser.helperApps.neverAsk.saveToDisk": "text/csv"
If you search online, people will be posting bunch of solutions using AutoIT and Robot class. I strongly recommend you avoid that. AutoIT to my knowledge is windows only. My experience with Robot didn't give consistent results. So both solutions are flimsy and brittle.
Also if you care about supporting multiple browsers, you would have to deal with this problem on per browser basis and NOT just firefox. I recommend you reading this blog which describes how to download files in Selenium and why you shouldn't. Author of the blog provides a solution in Java on querying for the file using a HTTP Get request. That's the best way to deal with this problem if you want to support multiple browsers.

Catch JavaScript Errors in Selenium WebDriver – Browser Independent

I am wondering if anyone could expand on any of these attempts or has any other ideas for catching JS errors using WebDriver that will work in Firefox, Chrome, Internet Explorer, and Safari.
Here is what's been tried so far:
Attempt – Problem:
JSErrorCollector.jar - Works fine, but is a Firefox only solution.
Inject JS into page source – I injected window.onerror code into the page’s source code using WebDriver, but any initial errors are missed because the injection is too late.
BrowserMob – I can intercept the HTTP response and planned to inject the window.onerror code into response body, but the author has not implemented the getBody() method yet, so only headers can be modified, that I am aware of. The body is always null for all responses. (I was on a webpage where the author talked about implementing getBody() but it hasn’t happened yet and I cannot find it again)
Fiddler – JS will inject correctly, but Fiddler is Windows only so Safari won’t work.
Parent/Child windows – I use javascript to open and store a reference to the test page’s window. The window.onerror code is contained in the parent window so it will not miss startup errors in the child window. I cannot get this to work in anything but Firefox and Chome somewhat. I already asked a question about it here.
Selenium RC – I haven’t tried it because all my tests use WebDriver, but I know it has some kind of method like captureNetworkTraffic(), but I don’t think it can be used in WebDriver.
IE error popup – I was going to use the parent/child solution for Firefox/Chrome and then look for the IE error popup. This popup displays when the setting is checked to display it. The popup is a native Window window (I think) so I cannot use selenium to access it.
Read browser console – I could not find a way to do this in all browsers. In Chrome I found a way to save the console log to a file and then read the file. That is as far as I got.
I would like a solution similar to BrowserMob since it seems like it would be a cross browser solution. Are there any other proxies that can be put in the test and intercept the response? It would have been excellent if the getBody() method was implemented. I also like the parent/child solution because it also seems like a simple, cross browser solution, but it is not working for IE (parent/child question again).
Thanks for any help.
I don't know of any way to directly catch Javascript code errors by a test framework. If I were to guess, I would use PhantomJS. Or, maybe something like MITM Proxy would work?
As a sidenote, if you run Selenium2 Grid Hub with a separate Node, you can pass a Java option to the JVM of the node like this that will allow a proxy through Fiddler to work. Fiddler listens (by default) on port 8888. With this method you can watch packets.
:: batch script: Set JAVA_OPTS java options to JVM
SET "JAVA_OPTS=-Dwebdriver.chrome.^
driver=%CHROMEDRIVER%"
IF "%PROXY_TO_FIDDLER%"=="true" SET "JAVA_OPTS=%JAVA_OPTS% -DproxySet=true^
-Dhttp.proxyHost=127.0.0.1 -Dhttp.proxyPort=8888"
I created scripts you can use to start your grid and node here. It seems to me that you could use this method to also talk to BrowserMob proxy on port 8080? I have not tried that.

Categories