Catch JavaScript Errors in Selenium WebDriver – Browser Independent - java

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.

Related

Apply settings to Selenium's FirefoxDriver

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.

Need a selenium java technique to click browser control Block location request button in Firefox

edit:<So I've only managed to get one person to try to help so far, if anyone has any suggestions on how this question, post, title, tags, etc can be improved, please leave a comment. I've been searching for a fix off and on for weeks, and nothing has worked.>
I have a java selenium spring boot cucumber framework set up running tests against Firefox. I have one specific blocker where the location request box pops up and stops the interaction with parts of the site I need to test.
There are a LOT of answers on this site that involve disabling the location functionality in the capabilities/options, and I've tried variations on most of these, and they resemble this layout, but none have had any effect.
firefoxOptions.setCapability("locationContextEnabled", false);
firefoxOptions.setCapability("geo.enabled", false);
firefoxOptions.setCapability("geo.provider.use_corelocation", false);
firefoxOptions.setCapability("geo.prompt.testing", false);
firefoxOptions.setCapability("geo.prompt.testing.allow", false);
I can't use a #Before cucumber tag to try and preload this because of the way the drivers are connected with interfaces, and the interaction with Spring Boot completely breaks the framework.
This is not an "alert", so none of the alert click through functionality will interact with this location widget.
I attempted manipulating "windowHandles()" but that seems to only work with tabs, not direct Firefox controls.
What I REALLY need is a direct way to detect that Firefox Pop Up and directly interact with the "Block" button, assuming that is possible. Since we can grab tab and url data though I have to assume that is the case until someone very knowledgeable says otherwise.
To handle the browser pop up selenium support below method for cancel or block.
driver.switchTo().alert().dismiss();
If you need to allow the pop up then use below code
driver.switchTo().alert().accept();

HtmlUnit with Knockout JS

I am trying to automate a login page which appears to be using Knockout.js.
HtmlUnit doesnt seem to load the full page, it is missing all the input fields which makes it impossible to actually login.
I have tried ensuring that the JavaScript timeouts are set and have also enabled NicelyResynchronizingAjaxController I am waiting after the page has loaded using:
waitForBackgroundJavaScript,
waitForBackgroundJavaScriptStartingBefore
Thread.sleep (just for
good measure)
I have even checked for additional windows (WebClient.getWebWindows), but there just seems to be the one.
It appears Knockout (assuming it is actually Knockout) is creating the inputs, is this just too much for htmlunit or have I missed something?
This is a know problem (see https://github.com/HtmlUnit/htmlunit/issues/37).
Hopefully i will find some time to figure out what is going wrong here.

Downloading Files with ChromeDriver

I have a project where I need to download an audio file in ChromeDriver. The behavior here is different from in regular Chrome, where if I visit the URL, it'll automatically start downloading a file. If I do the same thing manually in ChromeDriver, it will not download the file.
I've tried different configurations of the chrome options/preferences. I've also found options that worked with old versions of chrome, that no longer work anymore.
Here is one of the better resources I found, but it still didn't work, even with their updated blog post
https://dkage.wordpress.com/2012/03/10/mid-air-trick-make-selenium-download-files/
When I attempt to use his solution, my chromedriver abruptly crashes itself in a non chrome-esque way. It just disappears. Not "Something went wrong" page like you'd normally expect. I end up with Java not being able to find my Session, cause it stopped existing.
Has anyone been successful at downloading files through Selenium webdriver in Chrome? If I need to use another browser, I can.
I'm currently using Chrome Canary.
I have the same problem. One solution that might work is to use another library, that is able to operate outside of the browser. I found these stackoverflow post discussiong this issue:
https://sqa.stackexchange.com/questions/2197/how-to-download-a-file-using-seleniums-webdriver
it contains this blogpost wich gives you some sugestions.
https://blog.codecentric.de/en/2010/07/file-downloads-with-selenium-mission-impossible/
Window automation
The first approach smells like “brute force”: when searching the net for a solution to the problem, you easily end up with suggestions, to control the native window with some window automation software like AutoIt. Means you have to prepare AutoIt such, that it waits for any browser download dialog, the point at which Selenium is giving up, takes control of the window, saves the file, and closes the window. After that Selenium can continue as usual.
This might eventually work, but I found it to be techical overkill. And as it turned out, there was a much simpler solution to the problem.
Change the browsers default behaviour
The second possibility is to change the default behaviour of the browser. When clicking on a PDF for example, the browser should not open a dialog and ask the user what to do with the file, but rather save it without comments and questions in a predefined directory. To accomplish that, a file download has to be initiated manually, saved to disk and marked as the default behaviour for these file types from now on.
Well, that could work. You “only” have to assure that all developers, hudson instances, etc. share the same browser profile. And depending on the amount of different file types, that could be some manual work.
Direct download
Taking a step back, why do we want to download the file with Selenium in the first place? Wouldn’t it be much cooler, to download the file without Selenium, but rather with wget? You would have solved the second problem as you go. Seems a good idea, since wget is not only available for Linux but also for Windows.
Problem solved? Not quite: what about files, that are not freely accessible? What, when I first need to create some state with Selenium in order to access a generated file? The solution seems ok for public files, but is not applicable for all situations.

Get client windows username

Can i get the username of the currently looged in person using applets. If yes, how can i use the applet in my dynamic web project?
I know in I.E we can do like this
var objUserInfo = new ActiveXObject("WScript.network");
document.write(objUserInfo.ComputerName+"<br>");
document.write(objUserInfo.UserDomain+"<br>");
document.write(objUserInfo.UserName+"<br>");
var uname = objUserInfo.UserName;
alert(uname);
but i want it using applet so that it will work in any browser.
Be aware that this is in fact an attempt to circumvent the browser security.
look at the answer to this similar (but not excactly the same) question.
http://www.coderanch.com/t/243316/Applets/java/windows-NT-login-applet
ActiveX will not run properly in Chrome / Firefox.
Javascript won't (due to security reasons) allow you to get that information from the operating system.
Flash is cross browser, and can be used for this, but it is difficult to get around chromes sandbox.
so my best suggestion is java.
Here is a similar question that is answered on why you can't without an applet.
https://security.stackexchange.com/questions/36728/can-your-windows-or-linux-username-be-exposed-to-websites
And the provided link from an answer to another similar question points to a java solution, but there are limits to the success:
http://www.rgagnon.com/javadetails/java-0441.html
However I think you should start from there, and then post a solution here when it works ;)
See the problem here is that when the applet tries to interact with local resources the browser must allow it, or block it..
Security settings in IE, may prevent that if set to high, and Chromes sandbox may block it if you do it wrong. I don't know of problems with Firefox or Safari, but I guess you'll find them when testing...
Good luck.

Categories