Can't open websites (Chrome, Firefox) using Selenium - google-chrome

Since two days it is not possible anymore on my PC to open websites with firefox via Selenium WebDriver (Java, Maven, IntelliJ). A blank page is opened in a new firefox window and nothing happens. I can access the specified website manually in the firefox window that is opened.
This is the code I am using for creating the webdriver (the profile SELENIUM exists; I created it manually):
System.setProperty("webdriver.gecko.driver", "geckodriver.exe");
ProfilesIni profile = new ProfilesIni();
FirefoxProfile ffprofile = profile.getProfile("SELENIUM");
return new FirefoxDriver(ffprofile);
And to open pages I am using:
driver.get(myURL);
The error message from IntelliJ is:
SZgNd84ad1sbgGXU5f5p+46dwuqwWXX8hUp4lAPBZwEAAA0DAAANAAAAAAAAAAAAAAAAALfGQwB4dWxzdG9yZS5qc29uUEsFBgAAAABGAEYAnRUAAFnIQwAAAA (and some million more letters) == does not equal undefined"] (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 0 milliseconds
Today Chrome also stopped working, it only shows "Not secure" next to the URL and data; in the URL bar.
Is there any way to find out what caused this and how this can be fixed?
I am using the latest version of selenium (defined in pom.xml) Chrome Version 57.0.2987.133, Firefox 45.4.0.
Thanks in advance for any help!

I have seen this in a previous project, so basically what happens is that all this is caused by incompatibility between Selenium and Firefox/Chrome versions, but not by any one specific version number. So what you really want is to be 1-2 driver versions behind the newest, if your WebDriver is on the latest version. Otherwise, roll the Firefox/Chrome version back even further if your WebDriver is older, or upgrade Webdriver.
Other options is to use FF profiles/Chrome preferences. This is an example that was working back then:
FirefoxProfile prof = new FirefoxProfile();
//FirefoxProfile prof = profile.getProfile("default");
//prof.setPreference("browser.startup.homepage", pageUrl);
//prof.setPreference("startup.homepage_welcome_url", pageUrl);
//prof.setPreference("startup.homepage_welcome_url.additional", pageUrl);
prof.setPreference("xpinstall.signatures.required", false);
prof.setPreference("toolkit.telemetry.reportingpolicy.firstRun", false);
You can try other combinations of those settings and see which one will work for you.

Related

Permanently blocking a network call / url on IE, Edge, Chrome or Firefox

I have a webpage that I have to work on all day, it's a portal. I need to reload it about 500 times a day, at least.
I find it to be painfully slow, and I have identified that it makes a network call (just to load a profile picture) to a particular website that I cannot list here, and that network call timesout.
Now because it times out, I don't get a profile picture anyway, but it ends up wasting about 30 seconds per reload.
I can block the domain in the "Networks" tab of Chrome Dev Tools, but I am looking for a more permanent fix. I don't want to have the Dev Tools open all the time since it uses precious screen real estate. I haven't found out a way to permanently block that particular network call which will save me hours per day.
There are 3 things that come to my mind right now:
Block all image assets on the webpage
Initialise the chrome instance using Selenium maybe, and pass in the option to block that network call.
Block that particular website? It hasn't worked for me so far, am I doing it wrong?
I'm comfortable with a fix on any browser (Internet Explorer, Edge, Chrome, Firefox).
Any leads on this one? I can't be the only person to face this, and yet I haven't found out a solution for it without using admin access, which I don't have.
Workaround 1: Call the chrome instance from Selenium, and disable all images. Works, but not the best approach.
from selenium import webdriver
chrome_options = webdriver.ChromeOptions()
prefs = {"profile.managed_default_content_settings.images": 2}
chrome_options.add_experimental_option("prefs", prefs)
driver = webdriver.Chrome(chrome_options=chrome_options)
Workaround 2: Launch Chrome Window from Selenium while executing Network.setBlockedURLs. It will only work in Selenium 4 and above, but it's not working as of now on Selenium 4.0.0.b3.
https://chromedevtools.github.io/devtools-protocol/tot/Network/#method-setBlockedURLs
from selenium import webdriver
driver = webdriver.Chrome()
driver.execute_cdp_cmd('Network.setBlockedURLs', {"urls": ["https://www.somelink.com/*"]})
driver.get("www.mywebsite.com")

Start Chrome emulation from command line

I use Google Chrome with Intern to run automated tests and I would like to know if there is a way to launch Chrome in emulation mode from CLI or using a specific flag to test mobile rendering. If not, do you know a good workaround ?
I could directly use the Android Emulator (from Android SDK) with Selenium Webdriver apk or with mobile Chrome but tests are crashing most of the time, emulators don't respond and I have to restart it. Also, I need to test on the largest possible scope, not limited to Android devices.
Chrome on desktop is a lot more stable and even if a test fails, chrome always respond and can be closed automatically by Intern.
I tried a workaround with the "--enable-touch-events" flag and with a custom userAgent but it's producing weird behaviors. Maybe some other flags would help me ?
Thank you in advance for your answer.
This is currently not possible in Chrome.
It's a feature I've been wanting myself too so I've gone ahead and filed a feature request for it at the following link:
https://code.google.com/p/chromium/issues/detail?id=373169&thanks=373169&ts=1400050662
I'm crossing my fingers but it wouldn't hurt if you and other people interested in this went and left a comment on the thread too. The more people asking for it, the higher the chance of it being implemented. And it does seem like it would be trivial to implement since it currently only takes a couple of mouse clicks to enter emulation mode.
Selenium allows users to emulate Chrome on a mobile device using code like this:
Map<String, String> mobileEmulation = new HashMap<>();
mobileEmulation.put("deviceName", "Nexus 5");
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.setExperimentalOption("mobileEmulation", mobileEmulation);
WebDriver driver = new ChromeDriver(chromeOptions);

retaining cache in firefox and chrome browser - Selenium WebDriver

Currently our web application takes around 3 mins to load completely without caching and 10 secs with caching. When I open the app through WebDriver its taking around 3 mins to load i.e. caching is not used. I observed this on Firefox and Chrome browser. Not sure how to enabled the driver to use cache instead of loading each file from server every time I open the app.
Here are the things I tried.
1. disabled clearing cache on browser exit in browser setting.
2. set 'applicationCacheEnabled' desiredcapabilitiy to 'true'
DesiredCapabilities cap = DesiredCapabilities.firefox();
cap.setCapability("applicationCacheEnabled", "true");
WebDriver d = new FirefoxDriver(cap)
But nothing seems to work. Please let me know how can I make webdriver to use caching.
The problem is, that selenium copies every startup a new (firefox/chrome) profile to the temp directory and starts firefox/chrome with it.
However, it is possible to always use the same profile for your test instances.
I think this way you can get it working faster.
For firefox you just need to do these steps:
1. Load your webapp in a selenium firefox instance and don't close it afterwards (not driver.close();).
2. Then go to Help->Troubleshooting Information and open the folder under Profile folder.
3. Copy its content to a new folder near your test code.
4. Load the saved Profile in your test code. You can do it this way:
FirefoxProfile profile = new FirefoxProfile(new File("profile/folder/path"));
WebDriver driver = new FirefoxDriver(profile);
I think you can do this in chrome analogous.
#CacheLookup annotations can be very useful for the elements that do not change on the web page once loaded. These types of elements constitute a majority of elements on the web page. So for those elements, as they will not change during test execution, you should use the #Cachelookup annotation to improve the test speed.
Try below codes:
#FindBy(name="username")
#CacheLookup
private WebElement userName;

Selenium IDE doesn't open webpages that return a json response

I have a big Selenium test suite that's testing a web service. Given an input url, the web service returns either a regular html response or a json response. The test suite is being executed with Firefox's Selenium IDE. The tests call the open command on a given url and then verify stuff on the returned json/html. It used to work great until for some reason Firefox has stopped opening the jsons automatically. Instead of opening the json response as if it were a regular web page, Firefox asks "What should Firefox do with this file" and prompts me to select a program to open the file with.
How do I force Selenium IDE to make Firefox display the json responses as it used to?
Cases like these are usually Firefox & Selenium IDE version incompatibility. This can be from using a much newer version or an old version of Firefox that the IDE doesn't quite support.
In your case it appears to be an older version issue.
The first step you should do is update both the IDE & Firefox and take it from there.
The release notes also detail what version (range) of Firefox it generally supports.

Old Google Chrome automation using Webdriver

I have to automate for Google Chrome old versions such as 5,6,7...till the latest version. I see that chromedriver is available from version 13 onwards. Where can I find for the older version of these?
If I can't automate using webdriver, does selenium 1.0 supports all the older versions of google chrome? Is there a way to merge selenium 1.0 and webdriver?
As stated here (all the way down):
the ChromeDriver is only compatible with Chrome version 12.0.712.0 or newer
That's good news, one more version to work with, it's compatible with Chrome 12+!
Let's read on:
If you need to test an older version of Chrome, use Selenium RC and a
Selenium-backed WebDriver instance:
URL seleniumServerUrl = new URL("http://localhost:4444");
URL serverUnderTest = new URL("http://www.google.com");
CommandExecutor executor = new SeleneseCommandExecutor(seleniumServerUrl, serverUnderTest, DesiredCapabilities.chrome());
WebDriver driver = new RemoteWebDriver(executor);
This is worse, but still good, you can write WebDriver-like code and be backed by Selenium RC. That's written in pure JavaScript and therefore should work in any well configured and JavaScript-friendly browser. Chrome has always been JS-friendly, so chances are you'll get it to work everywhere!