Start Chrome emulation from command line - google-chrome

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);

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")

Chrome extension to enable flash plugin

I am working for a game which runs in Adobe Flash. From Chrome 69, sites using Flash will require explicit permission to run it, every time the user restarts the browser. We are expecting lot of our players will get dropped off as they need to go through click-to-play flow every time they restart the browser.(Flash Roadmap -> https://www.chromium.org/flash-roadmap)
So we came up with an idea of using chrome extension which can enable flash plugin in background for our game website. We used 'chrome.contentSettings' for creating this extension.
Can someone please confirm if there is any roadmap plan from chrome to stop controlling of 'adobe-flash-player' plugin from 'chrome.contentSettings'.
Other words pls tell me how long below api code will work ?
var flashResIdentifier;
chrome.contentSettings.plugins.getResourceIdentifiers(function(resIdentifiers) {
for (var i=0; ihttps://abc.xyz.com/', 'resourceIdentifier':flashResIdentifier, 'setting':'allow'});
This will re-enable Flash manual white-listing
in the latest Chrome 69+ and higher
chrome://flags/#enable-ephemeral-flash-permission
Set to DISABLED
then use Flash manual white-listing by going here:
chrome://settings/content/flash
Then manually add all website URL links
that you need like before
in Chrome 68 and lower
After Chrome 71, you can persistently reenable flash for specific websites on Windows via a registry setting. Go to or create HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Google\Chrome\PluginsAllowedForUrls, and add string values starting with a name of "1" and a value of the URL you wish to enable flash for (so, HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Google\Chrome\PluginsAllowedForUrls\1 = "http://myawesomesite.com/*" to enable Flash for everything under myawesomesite.com).
There is a similar setting for OSX and Android. See here for more information.
I had the same problem in version 72 of Chrome, and the solution I found is the following:
Click the paddlock icon, and go to Site settings:
Then, on Flash option, select Allow
Refresh page, and that's all! :)
Reference: https://www.lifewire.com/enable-flash-in-chrome-4160696

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

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.

Changing Chrome's settings with Selenium

I'm working on Chrome with Selenium and I'm looking to change a few settings within Chrome using the webdriver. Using Google and this site, I was able to get most of the settings working. However, a few more seem to escape me and hopefully I can get the answers here. I'm looking to alter the settings before launching the browser, such as using ChromeOptions, rather than using automation to navigate the settings page.
The settings I'm looking to change are as follows:
Disable Javascript
Disable the microphone
Change the home page
Altering the default search engine in the Omnibox
These 4 are giving me the most issues. Any help?
Perhaps this List of Chromium Command Line Switches will help. E.g.
DesiredCapabilities caps = DesiredCapabilities.chrome();
caps.setCapability("chrome.switches", Arrays.asList("--disable-javascript"));
And
Map<String, Object> preferences = Maps.newHashMap();
preferences.put( "browser.startup.homepage", "http://my.home.page" );
preferences.put( "browser.startup.page", START_WITH_HOME_PAGE );
capabilities.setCapability( ChromeOptions.CAPABILITY, preferences );
ChromeDriver driver = new ChromeDriver( capabilities );
Update
My guess is that the following
"import_search_engine": true
from Configuring other parameters will cause Chrome to ask you to select a search engine when it opens.
Turning off JavaScript makes chrome pretty much a no-op; I do not think that the option is supported. As far as microphones, that is more a system option. A search of about:config for microphone came up empty.

Automate Chrome console from Desktop app?

I'd like to be able to send info to the Chrome developer console from my application.
For example, my app has some json. I'd like this json to show up in either an existing, or newly created instance of the chrome dev tools console.
Is this possible? If so, any pointers to examples? Note that the platform should be any language, not just javascript. And definitely not a site already running in Chrome. I'm interested in implementing this in another process.
Do you thought of running your app in an environment which is pretty much like a browser?
Node.js
or (this is a whole webkit browser)
phantom.js
Otherwise you could call Chrome directly via commandline and try to simulate the dev tools key stroke like explained here:
Is there a command line argument in Chrome to start the developer tools on startup?
The command of displaying something in the Chrome console is e.g. console.log and it is at the end Javascript. All Console commands are described here:
https://developers.google.com/chrome-developer-tools/docs/console-api
The closest I've seen so far is this library:
https://github.com/ccampbell/chromelogger
which seems to allow logging to the Chrome Console from lots of other server side apis, but no desktop APIs.
This can be done on Mac using osascript. Save the following as a script and run it:
#!/usr/bin/osascript -l JavaScript
var chrome = Application('Google Chrome');
//chrome.activate();
chrome.includeStandardAdditions = true;
var currentTab = chrome.windows[0].activeTab()
currentTab.execute({javascript: 'console.log("Hello")'})