Is it possible to update the download location for a file in Chrome in Karate UI to the target folder? [duplicate] - download

Used userDataDir: 'path' in driver configure file but still files are not downloading in the specific location
Expected file to be downloaded in specific location
Can someone help me how to setup a specific path to download file in Karate in chrome like in selenium Chrome preference

You could use the experimental Browser.setDownloadBehavior Chrome DevTools Protocol method.
Here is a full example:
Feature:
Background:
* def waitForDownload = function(downloadPath) { while (!new java.io.File(downloadPath).isFile()) java.lang.Thread.sleep(1000) }
Scenario:
* configure driver = { type: 'chrome' }
* driver 'https://github.com/karatelabs/karate/releases/tag/v1.3.0'
* driver.send({ method: 'Browser.setDownloadBehavior', params: { behavior: 'allow', downloadPath: karate.toAbsolutePath('./someDir') } })
# scroll to bottom of page to ensure download link is created
* script('let x = (document.scrollingElement || document.body); x.scrollTop = x.scrollHeight')
* waitFor("a[href='/karatelabs/karate/archive/refs/tags/v1.3.0.tar.gz']").click()
* call waitForDownload karate.toAbsolutePath('./someDir/karate-1.3.0.tar.gz')

Related

Browser automation downloading to default location

I have browser automation using Watir and it uses chrome to drive it. the downloads needs to be in my app sub folder. But i am not able to change it as it downloads to the windows download folders
download_dir = "#{Dir.pwd}/downloads".tr('/', '\\')
Dir.mkdir download_dir unless Dir.exist ? download_dir
args = % w(--disable - infobars)
prefs = {
download: {
prompt_for_download: false,
download_directory: download_dir
}
}
ie = Watir::Browser.new(: chrome, options: {
args: args
}, prefs: prefs)
They have made some mistake in the recent selenium, that's what it's not working. I filed a bug here
https://github.com/SeleniumHQ/selenium/issues/7917
So please install selenium-webdriver 3.142.6, it's working fine.

Is there a way to download files using headless chromedriver in Centos using Ruby?

I try to download a file using headless chrome and the file doesn't seems to be getting downloaded anywhere. I could see that it's actually a security feature to restrict file download in headless but, is there a workaround for the same in Ruby? Tried the below code but no luck.
download_path = "#{Pathname.pwd}/test-data/downloaded"
options = Selenium::WebDriver::Chrome::Options.new
options.add_argument("--disable-dev-shm-usage");
options.add_argument('--headless') #Declaring the browser to run in headless mode
options.add_preference(
:download, directory_upgrade: true,
prompt_for_download: false,
default_directory: download_path
)
options.add_preference(:browser, set_download_behavior: { behavior: 'allow' })
#driver = Selenium::WebDriver.for :chrome, options: options #Browser object initialization
set_screen_resolution(1400, 900)
$driver = #driver
bridge = #driver.send(:bridge)
path = '/session/:session_id/chromium/send_command'
path[':session_id'] = bridge.session_id
bridge.http.call(:post, path, cmd: 'Page.setDownloadBehavior',
params: {
behavior: 'allow',
downloadPath: download_path
})
I expect the file to be downloaded using headless chrome but it's not happening.
When you click on a download link and if it opens in a separate tab before the file starts downloading, then you need to apply your above mentioned script to the newly opened tab too, because you've set the session id only for the current tab and not for the newly opened tab. So, try apply this script to that newly opened tab before trying to download a file. I'm sure it'll work.
def download_file(label, download_path)
ele = Locator.new(:xpath, "//ul[#class='cnt']//div[text()='#{label}']/..//a")
download_url = get_attribute(ele.get_how, ele.get_what, "href")
#driver.execute_script("window.open()")
#driver.switch_to.window(#driver.window_handles.last)
bridge = #driver.send(:bridge)
path = '/session/:session_id/chromium/send_command'
path[':session_id'] = bridge.session_id
bridge.http.call(:post, path, {
"cmd" => 'Page.setDownloadBehavior',
"params" => {
"behavior" => 'allow',
"downloadPath" => download_path
}
})
#driver.get(download_url)
#driver.close
#driver.switch_to.window(#driver.window_handles.last)
end

Redirect to custom protocol/scheme is not accessible when using headless watir/firefox

I’m working on a set of integration tests for the dropbox oauth sequence which finishes in a series of 302 redirects, the last of which is a custom protocol/scheme. Everything works as expected in the mobile app which the testing mimics, and everything bar this works in the integration tests.
The testing environment runs on ubuntu server (no GUI) and is headless using xvfb.
Objectively, I don’t actually need the custom protocol URI to be followed, I just need to get access to the URI to confirm the contents match expectations.
I have tried everything I can think of to access the URI containing the custom scheme from within watir/selenium, but all the references I can find say that the underlying detail is deliberately hidden by design.
I have also tried all the options I can find for creating a custom protocol handler within the firefox profile, but no matter what happens the script isn’t called.
Nothing useful is being left in the watir/selenium logs.
Any thoughts?
Custom protocol handler snippet:
# initialise headless
headless = Headless.new( reuse: false )
headless.start
# initialise profile
profile = Selenium::WebDriver::Firefox::Profile.new
profile[ 'general.useragent.override' ] = 'agent'
profile[ 'network.protocol-handler.app.biscuit' ] = '/usr/bin/biscuit'
profile[ 'network.protocol-handler.external.biscuit' ] = true
profile[ 'network.protocol-handler.expose.biscuit' ] = true
profile[ 'network.protocol-handler.warn-external.biscuit' ] = false
# initialise client
client = Selenium::WebDriver::Remote::Http::Persistent.new
# initialise browser
browser = Watir::Browser.new :firefox, profile: profile, accept_insecure_certs: true, http_client: client
# run dropbox authentication cycle
# cleanup
browser.close
headless.destroy
After chasing this around for ages, it turns out that most of the documentation for adding custom schemes on the mozilla site and forums is deprecated and there’s nothing new to replace it. Grrr.
Through a process of trial and error, I found that the model profile used by the webdriver does not need to be complete, and anything that is missing it’ll pull from the default profile. So all that is required is a handlers.json file containing the custom scheme/s and no more.
Snippet to demonstrate:
# create a temporary model profile
profilePath = '/tmp/modelProfile'
FileUtils.mkpath profilePath
File.chmod( 0700, profilePath )
FileUtils.chown 0, 0, profilePath
open( profilePath + '/handlers.json', 'w' ) { |file| file.write '{ "defaultHandlersVersion": { "en-US": 4 }, "schemes": { "biscuit": { "action": 2, "handlers": [ { "name": "biscuit", "uriTemplate": "https://www.biscuit.me?url=%s" } ] } } }' }
# create profile
profile = Selenium::WebDriver::Firefox::Profile.new( '/tmp/modelProfile' )
# initialise browser
browser = Watir::Browser.new :firefox, profile: profile, accept_insecure_certs: true

Protractor file download test fails when headless chrome

I am having an issue with a protractor test. It was working, but now (even thought nothing has changed) it is not.
The test is just opening the app (web application) and clicking on a button to download an image. The download should start straight away.
The problem is that the next instruction after the download event throws an exception, Failed: chrome not reachable. I am using the latest chrome and chrome driver versions.
The capabilites section for protractor is like this:
capabilities: {
browserName: 'chrome',
loggingPrefs: { browser: 'ALL' },
chromeOptions: {
args: ['--headless', '--window-size=1240,780'],
},
}
I am reading about using DevTools to enable downloads in headless mode (Page.setDownloadBehavior), but so far no luck.
Does anybody have this issue too? Any clue how to fix it?
Thanks.
There could be another easy way to do it, but this is what I have done in my test suite.
I used got library, however, you can use any library to send an HTTP post request.
Discussion about setting download directory in headless chrome: https://bugs.chromium.org/p/chromium/issues/detail?id=696481
let got = require('got');
let session = await browser.getSession();
let sessionId = session['id_'];
let params = {'cmd': 'Page.setDownloadBehavior', 'params': {'behavior': 'allow', 'downloadPath': downloadDir }}
await got.post('http://localhost:4444/wd/hub/session/'+ sessionId + '/chromium/send_command', {body: JSON.stringify(params)})
If you have not disabled ControlFlow in your protractor config, change ASync/Await to .then.
An easier solution is to add these lines to your protractor.conf.js:
exports.config = {
...
onPrepare() {
...
browser.driver.sendChromiumCommand('Page.setDownloadBehavior', {
behavior: 'allow',
downloadPath: downloadsPath
});
}
};
From: https://bugs.chromium.org/p/chromium/issues/detail?id=696481#c196
Appendix
If you are to lazy to find a Download Path just paste this at the top of your protractor.conf.js:
var path = require('path');
var downloadsPath = path.resolve(__dirname, './downloads');
It will download the file to the e2e/downloads folder. Just use the same code in your tests to find out if the file downloaded.
This works for me:
chromeOptions: {
'args': [
'--headless',
'--disable-gpu',
'--test-type=browser',
'--disable-extensions',
'--no-sandbox',
'--disable-infobars',
'--window-size=1920,1080',
//'--start-maximized'
"--disable-gpu",
],
prefs: {
'download.default_directory': 'C:\\downloads',
'download.prompt_for_download':false,
'download.directory_upgrade':true,
"safebrowsing.enabled":false,
"safebrowsing.disable_download_protection":true
},
},

How I can require script from data folder

I want to load js file from page and require it in background page.
I try use two copy in lib and in data folder, but have problem with review.
I can load it from lib folder in page, but it uncomfortable for other browsers.
I can load it via loader:
mono = require('toolkit/loader').main(require('toolkit/loader').Loader({
paths: {
'sdk/': 'resource://gre/modules/commonjs/sdk/',
'data/': self.data.url('js/'),
'': 'resource:///modules/'
},
name: self.name,
prefixURI: 'resource://'+self.id.slice(1, -1)+'/'
}), "data/mono");
But have problem with:
require('net/xhr').XMLHttpRequest
I try use for options it, but have same problems.
require('#loader/options')
Now I use it, but all require objects I send via arguments.
Have ideas?
upd
Now I use this code, it allow require modules and don't store it in memory, as I think. But need to declare all modules previously.
mono = require('toolkit/loader').main(require('toolkit/loader').Loader({
paths: {
'data/': self.data.url('js/')
},
name: self.name,
prefixURI: 'resource://'+self.id.slice(1, -1)+'/',
globals: {
console: console,
_require: function(path) {
switch (path) {
case 'sdk/timers':
return require('sdk/timers');
case 'sdk/simple-storage':
return require('sdk/simple-storage');
case 'sdk/window/utils':
return require('sdk/window/utils');
case 'sdk/self':
return require('sdk/self');
default:
console.log('Module not found!', path);
}
}
}
}), "data/mono");
I think this blogpost from erikvold addresses the problem you are facing: http://work.erikvold.com/jetpack/2014/09/23/jp-pro-tip-reusing-js.html

Resources