Webdriver logs for Firefox - google-chrome

Both Chrome and PhantomJS selenium drivers can log everything that is going on the browser side. By specifying the service log path while initializing the driver, you can control where the logs would be written to. E.g. for chrome (in Python):
from selenium import webdriver
driver = webdriver.Chrome(service_log_path="/tmp/log")
driver.get("http://www.google.com")
driver.close()
After executing the code, /tmp/log file would contain the service logs which is helpful for debugging:
[0.985][INFO]: Launching chrome: ...
[2.620][INFO]: RESPONSE InitSession {
"acceptSslCerts": true,
"applicationCacheEnabled": false,
"browserConnectionEnabled": false,
"browserName": "chrome",
"chrome": {
"userDataDir": "/var/folders/yy/ppdg927x4zv8b0rbzg1f_jzh0000gn/T/.org.chromium.Chromium.ibsof9"
},
"cssSelectorsEnabled": true,
"databaseEnabled": false,
"handlesAlerts": true,
"javascriptEnabled": true,
"locationContextEnabled": true,
"nativeEvents": true,
"platform": "Mac OS X",
"rotatable": false,
"takesHeapSnapshot": true,
"takesScreenshot": true,
"version": "37.0.2062.120",
"webStorageEnabled": true
}
[2.677][INFO]: Waiting for pending navigations...
[2.696][INFO]: Done waiting for pending navigations
[3.290][INFO]: Waiting for pending navigations...
[4.338][INFO]: Done waiting for pending navigations
[4.338][INFO]: RESPONSE Navigate
[4.339][INFO]: COMMAND CloseWindow {
}
[4.451][INFO]: RESPONSE CloseWindow
Is there a way to get the same information but using Firefox web driver?
From what I see in the source code, both Chrome and PhantomJS fire up new services via subprocess and pass the --log-path argument to it. And these services are responsible for the logging. As for Firefox driver, it's implementation is quite different and is based on FirefoxBinary class.
Provided example and links are Python-related, but the question is pretty much generic and language-agnostic. Would appreciate any pointers.

You have to set logging options in firefox profile as in developer tips link - https://code.google.com/p/selenium/wiki/DeveloperTips - for console log you should use:
FirefoxProfile p = new FirefoxProfile();
p.setPreference("webdriver.log.file", "/tmp/firefox_console");
WebDriver driver = new FirefoxDriver(p);
For browser log, you should use
webdriver.firefox.logfile
(https://code.google.com/p/selenium/wiki/FirefoxDriver)
Hope this helps.

I believe logging for FireFox has to be enabled via a profile. Which needs to be base64 encoded.
Something along those lines is mentioned in this bug raised against RemoteWebDriver.
Any help?

Related

ERR_CONNECTION_REFUSED in localhost after GC update to 87.0.4280.66

I work with Visual Studio 2017 and develop asp.net core (2.2) app’s.
I have set IIS-Express as dev webserver and Google Chrome as default browser.
In the project settings, I have set http://localhost:14300 as dev-url.
Since the last chrome update Version 87.0.4280.66 (at 19.11.2020) the app is compiled, the chrome browser is started, but the url seems to be changed “on the fly” from http://localhost:14300 (as defined) to https:localhost (without port).
Therefore, an error message is showed in the browser:
ERR_CONNECTION_REFUSED
The app is loaded - if I paste the url (http://localhost:14300) in another Browser or a new chrome tab, the app is loaded.
So.. I assume a bug in the latest Chrome update or a compatibility issue VS2017 <-> ISS Express <-> GC
I already have searched the Internet and found nothing yet (further, I already sent as issue to Google) .
According to your description, I guess you may set the launchSettings.json's IIS Express's launch Url to "https://localhost".
I suggest you could check it to make sure the launch Url is right.
More details about the launchSettings.json, you could refer to below codes:
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:5761",
"sslPort": 44365
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "https://localhost",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"AngularCore31": {
"commandName": "Project",
"launchBrowser": true,
"applicationUrl": "https://localhost:5001;http://localhost:5000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
It seems as I have found the issue (I see no context, but this has solved my problem):
Note: Here ( 1 ) was https://localhost showed (instead of http://localhost:14300 as on the screenshot below).
After launch with debug to IIS press the (i) ( 1 ) symbol
Then select "Website-Einstellungen" ( 2 ) (website settings)
Then, I had to delete ( 3 ) all data to the website (localhost)
After doing that, the debug url (http://localhost:14300) was loaded correct again.
So... (for me) it seems as the update to Chrome Version 87.0.4280.66 had a problem regarding the already stored data and then (for whatever reason) has redirected
http://localhost:14300
to
https:/localhost

Setting sensors (location) in headless Chrome

Is it possible to set custom location coordinates with Chrome Headless? I can't find it in the
Devtools protocol
API. Is there a workaround available?
I googled it and got many methods. I try one by one, almost all of them turn out outdated. Then I find out a solution, use chrome devtools protocol to achieve that.
The small example code below, that it uses the most common tool selenium to execute chrome devtools protocol command.
import time
from selenium.webdriver import Chrome, ChromeOptions
options = ChromeOptions()
options.add_argument("--headless")
driver = Chrome(options=options)
driver.execute_cdp_cmd(
"Browser.grantPermissions",
{
"origin": "https://www.openstreetmap.org/",
"permissions": ["geolocation"]
},
)
driver.execute_cdp_cmd(
"Emulation.setGeolocationOverride",
{
"latitude": 35.689487,
"longitude": 139.691706,
"accuracy": 100,
},
)
driver.get("https://www.openstreetmap.org/")
driver.find_element_by_xpath("//span[#class='icon geolocate']").click()
time.sleep(3) # wait for the page full loaded
driver.get_screenshot_as_file("screenshot.png")
https://chromedevtools.github.io/devtools-protocol/tot/Emulation#method-setGeolocationOverride
and
https://chromedevtools.github.io/devtools-protocol/tot/Emulation#method-clearGeolocationOverride
... then you'll need to contend with ensuring that the correct location sharing setting is set within the user profile (chrome://settings/content/location - which is difficult to access due to being displayed via shadow dom, so using a preconfigured user profile will likely be easier --user-data-dir).
Edit to add: The above does not seem to be effective when using --headless. To resolve this I used https://chromedevtools.github.io/devtools-protocol/tot/Page#method-addScriptToEvaluateOnNewDocument with the following snippet:
navigator.geolocation.getCurrentPosition = function(success, failure) {
success({
coords: {latitude: <your_lat_float>, longitude: <your_lng_float>},
timestamp: Date.now(),
});
}

Where does Chrome download synced extensions from?

Say I was to sign into a new user on Chrome and had extensions from a previous computer synced to my account. Where are these extensions downloaded from after I sign? I thought maybe update_url in manifest.json but I'm not sure.
Let's examine an actual sync payload through chrome://sync-internals/
"SPECIFICS": {
"encrypted": true,
"extension": {
"disable_reasons": "0",
"enabled": true,
"id": "ijglncoabcgieiokjmgdogpefdblmnle",
"incognito_enabled": false,
"installed_by_custodian": false,
"name": "Desktop Notifications for Stack Exchange",
"remote_install": false,
"update_url": "https://clients2.google.com/service/update2/crx",
"version": "1.6.12"
}
},
From this, Chrome does see the update_url, which it can query for the download URL according to auto-update protocol. Note that the actual extension is not hosted at that URL, that URL merely contains instructions on where to download it from. In this instance, the download will be from the Chrome Web Store.
It will also sync disabled status across instances: a disabled extension will still be downloaded, but will be disabled immediately.
Since non-enterprise versions can't install from anywhere but CWS, and non-CWS enterprise installs take their update_urls from policies and not Sync, I think the answer to your question is almost certainly "from CWS". Can't test it though.

Protractor: launch chrome with network throttling enabled

The network throttling feature from Chrome DevTools is available in ChromeDriver-2.26+ according to this issue. How can I specify this in our protractor config file?
Based on searching around, I've tried variations of a couple things in the config file. I've added a networkConnectionEnabled property and a prefs block to chromeOptions, as below. (Note that I didn't do them both at the same time.)
multiCapabilities: [
{
'browserName': 'chrome',
'platform': 'ANY',
'networkConnectionEnabled': {'type': 'GPRS'},
'chromeOptions': {
args: [
'--lang=en',
'--window-size=1280,1024'
],
prefs: {
'net.throttling_enabled': 'true,50,20'
}
}
}
],
The second option I tried based on what I found here (line 1983). None of these change the behavior of the protractor run, which when I manually test and set the throttling triggers a certain condition in my code.
Edit: also tried adding something like this underchromeOptions: mobileEmulation: {networkConnectionEnabled: true, networkThrottle: '2G'}
According to changelog APIs for manipulating network emulation in Chrome were added in Selenium 3.4.0. And they are available in Protract 5.2.0 or newer.
Network conditions can be configured like below:
browser.driver.setNetworkConditions({
offline: false,
latency: 5, // Additional latency (ms).
download_throughput: 500 * 1024, // Maximal aggregated download throughput.
upload_throughput: 500 * 1024 // Maximal aggregated upload throughput.
});
This code should be placed in protractor.conf.js inside onPrepare function.

Using Google Chrome remote debugging protocol

I need to get the network events from Chrome. I've found this:
https://developer.chrome.com/devtools/docs/debugger-protocol
https://developer.chrome.com/devtools/docs/protocol/1.1/network#command-enable
It seems that Chrome uses a port to get messages, answer and send events, for remote debugging. It says it uses JSON, so I decided to try it.
So, I wrote some simple java code that opens the port that chrome is listening on (ofcourse i've started it by using google-chrome --remote-debugging-port=9222 on my ubuntu machine). I have a thread that writes to stdout anything coming from this port, and then the code writes this to the outputstream of the socket using this line (a sample method from the protocol):
out.println("{\"id\": 1,\"method\": \"Network.enable\"}");
I would expect some answer (according to the protocol) in the input stream but nothing happens.
Does anyone ever done something like this? I can't find anything on the net.
Finally I've got it. Credit goes to https://www.igvita.com/2012/04/09/driving-google-chrome-via-websocket-api/.
First I send an HTTP request to http://localhost:9222/json. This returns a JSON list of open tabs in Chrome, for each I also get a WebSocket uri (webSocketDebuggerUrl):
[
{
"description": "",
"devtoolsFrontendUrl": "/devtools/devtools.html?ws=localhost:9222/devtools/page/C014A09F-BD0A-40BA-B23C-7B18B84942CD",
"faviconUrl": "http://cdn.sstatic.net/stackoverflow/img/favicon.ico?v=00a326f96f68",
"id": "C014A09F-BD0A-40BA-B23C-7B18B84942CD",
"title": "Using Google Chrome remote debugging protocol - Stack Overflow",
"type": "page",
"url": "https://stackoverflow.com/questions/28430479/using-google-chrome-remote-debugging-protocol",
"webSocketDebuggerUrl": "ws://localhost:9222/devtools/page/C014A09F-BD0A-40BA-B23C-7B18B84942CD"
}
]
Then I can use WebSocket to send messages for debugging a specific tab, using this URI. I also found this for using Jetty implementation of WebSocket: javax.websocket client simple example.