Trying to get TRACE level logging from Geckodriver - selenium-webdriver

I'm trying to get trace logging information out of the gecko driver, in order to debug some issues we're having with moving to newer versions of Python, Robot Framework, Selenium, Firefox.
See in the DEBUG robot output below, I'm setting the capabilities to include log level arguments as part of moz:firefoxOptions.
When the capabilities are printed out for Open Browser, the moz:firefoxOptions value has been replaced with the profile info, so I never get tracing set.
20190219 13:07:22.461 - INFO - {'browserName': 'firefox', 'marionette': True, 'acceptInsecureCerts': True, 'moz:firefoxOptions': {'prefs': {'log': {'level': 'trace'}}, 'args': ['-headless', '-profile', '/tmp/tmpimda__f2']}, 'binary': '/usr/bin/firefox'}
20190219 13:07:22.461 - INFO - +--- END KW: BuiltIn.Log (1)
20190219 13:07:22.461 - INFO - +--- START KW: CustomLib.AdminClass.Open Browser [ ${LOGIN URL} | ${BROWSER} | desired_capabilities=${dc} | ff_profile_dir=${profile} ]
20190219 13:07:22.462 - INFO - Opening browser 'Firefox' to base url 'https://HOST:PORT/bmmadmin/login.jsp'.
20190219 13:07:22.480 - DEBUG - POST http://127.0.0.1:38132/session {"capabilities": {"firstMatch": [{}], "alwaysMatch": {"browserName": "firefox", "acceptInsecureCerts": true, "moz:firefoxOptions": {"profile": "UEsDBBQAAAAIAOtoU04+3NKsfgMAAM0MAAAHAAAAdXNlci5qc6VWTW/UMBC98ytQTyCx1rbABU6lFAkJUcSq4mg59mTjrmMbe7zp/nvGyaYsbb6AWz7e89hvZt44RQjcByhfnBXBNfTGlKtZo61yDVOp9gysKAyos1fPMSR4+f5ZesoxAiGEZE/ApTBxBG2hQVGwFAzhzkThEr4rjLC7sym8F1tYtn5EETB5lhmEXE+jKlfDEblgL49Z3O3p5FoBqyM6266it9YFGFmAPqkb+6MVOJ6vmbYY3O33L9Phc1Jqcc9lFSgwjzJoj5wU56jrHPT1epiwDBnAu4BcGMPvIod7CR61s3Ew6XdiL7plmetgLFau+Wyv6NEZGCRZwMaFHZOCKHtY5XjCrAiw13ImrxFkChoPTMa+HEeAj5PTgJFZr9lSm2IyoZTO5xQzizRQqECHC1zIrCBPlNsUERSXEHBYzRNSjClHfCBp+gBhhtXpwa3IsnLYgx0JJLxnySvqVCYSumEFT0CLWk3p6I04gLo+X2++OqRcEuHNCNg11jihqDAtaRzasvlRgd1k9bXdTse6vv1yyfp2m7SjFvmadVmYsy5td1THYKnsycS6JF/MYcmSOo8cB7uyJPxIpfagAEJlZQBB0k4/6RDxkrSQI230YEOihPaZVFtoiqeMWphGhKV2CiLI6lgWc1BTxuRzc/f2PuajEGO2DiSnJBXayi/J27gMIlYzUSowhskK5O4jlCIZ/ND9mabRDImMjm1v7JVxceYoJ+gbyvgwmBQRnXdmVSsQBqvunS1ytgl+8rlVrv+R3Ucvc0F9T3+/feO22/xRdp7+F/vwzmh5YPnjJhW1bvP8v/xv7cfL3w09tAzs0TkTGTmEC8eNT15f8ugjB2sNtG1rRXPGbnlWfjhG9gjbFm5roh878kYSO7vu+XoUXhgnd0ZHnK6IE0Zb4Feu9mTthTZ5AFq9rdAcZqlLLPwp3JJ/l4fb0UY6GfxTqvajvnP51dEHVzRiMcXhlXtKhUjXtkrH6rDKvz3NxJUBu8VsCRdv3/5J65emqUX5MMY1vDhw1XnC4N48WYzHmLub104Js/DmUWoD9MRc0NQWvKvRfJmawUcMWiJ/TBsKlW+NlqywhrqAsGlfR+TKZb7TyI6y9eP0WMqDB+8pCIYiYDiwTor25Bdz2EmRnsID3LXjbHAn916T7VO2WD4y1QS5PzF+Jh3GIvymNJWmMLmLBhi/AFBLAQIUAxQAAAAIAOtoU04+3NKsfgMAAM0MAAAHAAAAAAAAAAAAAACkgQAAAAB1c2VyLmpzUEsFBgAAAAABAAEANQAAAKMDAAAAAA=="}}}, "desiredCapabilities": {"browserName": "firefox", "acceptInsecureCerts": true, "moz:firefoxOptions": {"profile": "UEsDBBQAAAAIAOtoU04+3NKsfgMAAM0MAAAHAAAAdXNlci5qc6VWTW/UMBC98ytQTyCx1rbABU6lFAkJUcSq4mg59mTjrmMbe7zp/nvGya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ID3LXjbHAn916T7VO2WD4y1QS5PzF+Jh3GIvymNJWmMLmLBhi/AFBLAQIUAxQAAAAIAOtoU04+3NKsfgMAAM0MAAAHAAAAAAAAAAAAAACkgQAAAAB1c2VyLmpzUEsFBgAAAAABAAEANQAAAKMDAAAAAA=="}, "binary": "/usr/bin/firefox", "marionette": true}}
Anyone have any ideas what I may be doing wrong here? And/OR some alternative methods of getting trace logging from geckodriver?
BTW: the moz:firefoxOptions I'm setting may not be correct, they're just the latest combination I have tried, but I can't tell what is going to work until the options I set are actually being sent to firefox.
Environment:
Python 3.6.3
geckodriver 0.24.0 ( 2019-01-28)
Mozilla Firefox 60.4.0
PIP:
requests (2.21.0)
robotframework (3.1.1)
robotframework-requests (0.5.0)
robotframework-selenium2library (3.0.0)
robotframework-seleniumlibrary (3.3.1)
selenium (3.141.0)
setuptools (40.8.0)
urllib3 (1.24.1)
ROBOT:
Open Browser To Login Page
${dc}= CustomLib.Get Desired Capabilities ${BROWSER}
Set To Dictionary ${dc}
... binary ${PATH TO MARIONETTE}
Log ${dc}
${profile}= CustomLib.Create FF Profile
Open Browser ${LOGIN URL} ${BROWSER} desired_capabilities=${dc} ff_profile_dir=${profile}
PYTHON:
#keyword
def get_desired_capabilities(browser):
_capabilities = {'ff' : DesiredCapabilities.FIREFOX,
'firefox' : DesiredCapabilities.FIREFOX
}
browserKey = str(browser).lower()
desired = _capabilities[browserKey]
BuiltIn().log("get_desired_capabilities B4 :" + str(desired))
#THIS IS ASSUMING FIREFOX
desired['marionette'] = True
desired['acceptSslCerts'] = True
desired['ignoreProtectedModeSettings'] = True
desired['headless'] = True
desired['moz:webdriverClick'] = True
desired['moz:firefoxOptions'] = {'log':{'level':'trace'}, 'args':['-headless']}
#desired['moz:firefoxOptions']['log'] = {'level':'trace'}
#desired['moz:firefoxOptions']['args'] = ['-headless']
BuiltIn().log("get_desired_capabilities returning :" + str(desired))
return desired
#keyword
def create_ff_download_profile(path):
from selenium import webdriver
BuiltIn().log('PATH: ' + path)
fp = webdriver.FirefoxProfile()
fp.set_preference("browser.download.folderList", 2)
fp.set_preference("browser.download.manager.showWhenStarting", False)
fp.set_preference("browser.helperApps.alwaysAsk.force", False);
fp.set_preference("browser.helperApps.neverAsk.saveToDisk", 'text/xml,text/plain,application/octet-stream')
fp.set_preference("browser.download.dir", path)
fp.update_preferences()
return fp.path
thanks,
Tom

This is how I set the log level to be trace in Firefox:
*** Settings ***
Library SeleniumLibrary
*** Keywords ***
${options}= Evaluate sys.modules['selenium.webdriver.firefox.options'].Options() sys
${options.log.level}= Set Variable trace
${driver}= Create Webdriver Firefox options=${options}
Go To https://www.google.com
It's basically following this python pattern from the firefox docs and translating it into Robotframework
Hope this helps!

Related

In Selenium webdriver, for remote Firefox how to use OSS bridge instead of w3c bridge for handshaking

I am using selenoid for remote browser testing in ruby.
In that I am using 'selenium-webdriver', 'capybara', 'rspec' for automation. And I am using attach_file method for uploading file to browser
I want to upload file on Firefox and Chrome browser but it raises error on both;
In chrome
Selenium::WebDriver::Error::UnknownCommandError: unknown command: unknown command: session/***8d32e045e3***/se/file
In firefox
unexpected token at 'HTTP method not allowed'
So After searching I found the solution for chrome which is to set w3c option false in caps['goog:chromeOptions'] > caps['goog:chromeOptions'] = {w3c: false}
So now chrome is using OSS bridge for handshaking but I don't know how to do it in Firefox. Similar solution is not available for Firefox.
My browser capabilities are following:
if ENV['BROWSER'] == 'firefox'
caps = Selenium::WebDriver::Remote::Capabilities.new
caps['browserName'] = 'firefox'
# caps['moz:firefoxOptions'] = {w3c: false} ## It is not working
else
caps = Selenium::WebDriver::Remote::Capabilities.new
caps["browserName"] = "chrome"
caps["version"] = "81.0"
caps['goog:chromeOptions'] = {w3c: false}
end
caps["enableVNC"] = true
caps["screenResolution"] = "1280x800"
caps['sessionTimeout'] = '15m'
Capybara.register_driver :selenium do |app|
Capybara::Selenium::Driver.new(app, browser: :remote,
:desired_capabilities => caps,
:url => ENV["REMOTE_URL"] || "http://*.*.*.*:4444/wd/hub"
)
end
Capybara.configure do |config|
config.default_driver = :selenium
end
I have found the problem. There is bug in selenium server which run on java so I have to change my selenium-webdriver gem version 3.142.7 and monkey-patch.
You can find more information here about the bug and resolution.
For now I have to change my gem and monkey patch the selenium-webdriver-3.142.7\lib\selenium\webdriver\remote\w3c\commands.rb file. check for below line which is on line no 150.
upload_file: [:post, 'session/:session_id/se/file']
and update it to
upload_file: [:post, 'session/:session_id/file']
i had a similar issue with rails 7. the issue is connected with the w3c standard. the core problem is that the webdriver for chrome uses a non-w3c standard url for handling file uploads. when uploading a file, the webdriver uses the /se/file url path to upload. this path is only supported by the selenium server. subsequently, the docker image provided by selenium works fine. yet, if we use chromedriver, the upload fails. more info.
we can solve this, by forcing the webdriver to use the standard-compliant url by overriding the :upload_file key in Selenium::WebDriver::Remote::Bridge::COMMANDS. since, the initialization of this the COMMANDS constant does not happen when the module is loaded, we can override the attach_file method to make sure the constant is set correctly. here the hacky code:
module Capybara::Node::Actions
alias_method :original_attach_file, :attach_file
def attach_file(*args, **kwargs)
implement_hacky_fix_for_file_uploads_with_chromedriver
original_attach_file(*args, **kwargs)
end
def implement_hacky_fix_for_file_uploads_with_chromedriver
return if #hacky_fix_implemented
original_verbose, $VERBOSE = $VERBOSE, nil # ignore warnings
cmds = Selenium::WebDriver::Remote::Bridge::COMMANDS.dup
cmds[:upload_file] = [:post, "session/:session_id/file"]
Selenium::WebDriver::Remote::Bridge.const_set(:COMMANDS, cmds)
$VERBOSE = original_verbose
#hacky_fix_implemented = true
end
end
In Firefox images we support /session/<id>/file API by adding Selenoid binary which emulates this API instead of Geckodriver (which does not implement it).

Wagtail returns 500 error on Debug = False

I can't seem to get my site to display when I set Debug = False in my settings file after getting it to run using a standard setup of wagtail start . If I runserver it runs fine. I change the setting to Debug = False and my site shows the 500 error page.
Just the basic settings file:
from .base import *
# SECURITY WARNING: don't run with debug turned on in production!
#DEBUG = True
DEBUG = False
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'c1=mbs2t*!^omg&i0xfo_=mle)%_oegt-&#gn)vx5cs0foe%l9'
# SECURITY WARNING: define the correct hosts in production!
ALLOWED_HOSTS = ['*']
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
try:
from .local import *
except ImportError:
pass
Django 2.2.1
Wagtail 2.5
It turns out I had to collectstatic files.
When Django runs with DEBUG=False you won't get the yellow error pages. That doesn't mean those errors are not raised.
Just configure logging and inspect the logs.
https://docs.djangoproject.com/en/2.2/topics/logging/#django-s-default-logging-configuration
Without a traceback it is hard to solve any issue.
This is how you log to a file:
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'handlers': {
'file': {
'level': 'DEBUG',
'class': 'logging.FileHandler',
'filename': '/path/to/django/debug.log',
},
},
'loggers': {
'django': {
'handlers': ['file'],
'level': 'DEBUG',
'propagate': True,
},
},
}
This is the Django default logging: https://github.com/django/django/blob/stable/2.2.x/django/utils/log.py
I think this same thing caught me a while back. It's because the browser looks for a /favicon.ico file and for some reason Wagtail or Django tries to pass it to a view instead of just 404ing. Try to add the following in the head of your base.html template and see how it goes. Correcting the link to the file path below as needed.
<link rel="shortcut icon" href="{% static 'images/favicon.ico' %}"/>

How to disable plain_text.wrap_long_lines option in geckodriver?

Using selenium-webdriver and capybara in rspec, in a features spec I'm trying to get the HTTP response of a plain text request, namely /robots.txt
But instead of getting the plain text response, I get the text response wrapped in HTML:
expected: "User-agent: *\nDisallow:\n\nSitemap: https://prj.org/sitemap.xml\n"
got: "<html><head><link rel=\"alternate stylesheet\" type=\"text/css\" href=\"resource://content-accessible/plaintext.css\" title=\"Wrap Long Lines\"></head><body><pre>User-agent: *\nDisallow:\n\nSitemap: https://prj.org/sitemap.xml\n</pre></body></html>"
When fetching /robots.txt with curl I get the expected plain text response. So I've been through Firefox options, and I found out I needed to disable plain_text.wrap_long_lines option.
And I cannot succeed to pass the option to geckodriver.
I first tried to pass it to the Options object, like this:
Capybara.register_driver :firefox_headless do |app|
options = ::Selenium::WebDriver::Firefox::Options.new
options.headless!
options.add_preference 'plain_text.wrap_long_lines', false
Capybara::Selenium::Driver.new app, browser: :firefox, options: options
end
Then I tried to pass it to a Profile object.
Capybara.register_driver :firefox_headless do |app|
options = ::Selenium::WebDriver::Firefox::Options.new
options.headless!
profile = Selenium::WebDriver::Firefox::Profile.new
profile['plain_text.wrap_long_lines'] = false
Capybara::Selenium::Driver.new app, browser: :firefox, options: options, profile: profile
end
In both cases, the result is the same. Any idea as per why? Thanks!
Using:
selenium-webdriver 3.14.1
capybara 3.7.2
geckodriver 0.22.0
The issue you're seeing here is that when Firefox opens a text file it automatically wraps it in some boilerplate html in order for the browser to be able to display it. You don't show your test code you're using - but whatever you're doing should boil down to something like
# If using minitest
visit('/robots.txt')
find('body > pre').assert_text("User-agent: *\nDisallow:\n\nSitemap: https://prj.org/sitemap.xml\n", exact: true)
# If using RSpec
visit('/robots.txt')
expect(find('body > pr')).to have_text("User-agent: *\nDisallow:\n\nSitemap: https://prj.org/sitemap.xml\n", exact: true)

Target browser must be a string... (selenium, protractor)

i have upgraded my environment to have support for my tests for firefox, chrome, etc.
I have installed:
Node LTS (6.10.0)
Selenium Server Standalone 3.1.0
protractor#5.1.1
npm#4.1.2
When i now want to run my test i receive:
[17:31:32] I/launcher - Running 1 instances of WebDriver
[17:31:32] I/hosted - Using the selenium server at http://localhost:4444/wd/hub
[17:31:32] E/launcher - Error: TypeError: Target browser must be a string, but is <undefined>; did you forget to call forBrowser()?
[...]
[17:31:32] E/launcher - Process exited with error code 100
Process finished with exit code 100
What does that mean?
The config.js is:
var TIMEOUT = 10000;
exports.config = {
...
capabilities: [
{
'browserName': 'firefox', //tried as 'firefox', firefox and "firefox"
//'marionette': true //tried true and false
},
],
...
};
That was tricky and funny :) You know what .. There is no issue with browserName. Problem was you are providing the capabilities object incorrectly. You are providing an array whereas you need to send 1 capabilities object
Remove '[' & ']'
capabilities: [{'browserName': 'firefox'},] - This is incorrect . This means an Array of size 1 with index 0 holding your firefox config
It should be this - capabilities: {'browserName': 'firefox'}

Can't stop Protractor from displaying file download prompt

Problem
I'm testing downloading a file, but when I trigger the download, the "Save as..." prompt appears.
I saw a solution in this SO question but it doesn't seem to work for me.
Config
My protractor config file looks like this (coffeescript):
exports.config =
capabilities:
browserName: "chrome"
shardTestFiles: true
maxInstances: 2
chromeOptions:
args: ['--no-sandbox', '--test-type=browser']
prefs:
download:
prompt_for_download: false
default_directory: '/'
default_content_settings:
popups: 0
More
On chromeOptions.pref webdriver docs states:
See the 'Preferences' file in Chrome's user data directory for examples.
I can't actually see default_directory in my own Chrome preferences file.
"download": {
"directory_upgrade": true,
"prompt_for_download": false
},
System
Protractor: Version 1.5.0 (pretty new)
Node: 0.10.28, 0.11.8 and 0.11.14
Provide an absolute path to an existing directory in default_directory chrome preference.
For me, the Save As prompt was appearing when I mentioned a slash at last in the path for example: A:\Proj\Downloads\ & didn't see any prompt when mentioned without a slash at the end of the path i.e A:\Proj\Downloads

Resources