Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is there a way to change or get download directory of browser? #992

Closed
sunaynat opened this issue Dec 20, 2023 · 8 comments
Closed

Is there a way to change or get download directory of browser? #992

sunaynat opened this issue Dec 20, 2023 · 8 comments
Labels
question Questions related to rod

Comments

@sunaynat
Copy link

sunaynat commented Dec 20, 2023

Scenario which I am trying to test is a downloaded document has the same filename as the one I uploaded in earlier part or the test. And when running the test case in different OS the download directory changes. Is there a way to either set the directory to something or get the directory so that I can assert on the downloaded file.

working on v0.114.5

@sunaynat sunaynat added the question Questions related to rod label Dec 20, 2023
Copy link

Please add a valid Rod Version: v0.0.0 to your issue. Current version is v0.114.5

generated by check-issue

@ysmood
Copy link
Member

ysmood commented Dec 20, 2023

The WaitDownload method allows you to specify the dir:

rod/browser.go

Line 504 in d2ad7cb

func (b *Browser) WaitDownload(dir string) func() (info *proto.PageDownloadWillBegin) {

@sunaynat
Copy link
Author

if I specify in waitdownload then my file gets downloaded with alphanumeric characters and not with the original filename. And if I dont use waitdownload then it gets downloaded in downloads folder with the correct filename

@ysmood
Copy link
Member

ysmood commented Dec 20, 2023

Then you need to config the preference of the browser to change the download folder:

func (l *Launcher) Preferences(pref string) *Launcher {

It's a json value like this:

flags.Preferences: {`{"plugins":{"always_open_pdf_externally": true}}`},

The doc of all preference:

https://src.chromium.org/viewvc/chrome/trunk/src/chrome/common/pref_names.cc?view=markup#l1443

@sunaynat
Copy link
Author

sunaynat commented Dec 20, 2023

tried this, it did not work. Still it gets downloaded in downloads folder
launcher.New().Preferences({"download":{"default_directory": +mydir+}}).
Also tried with ""savefile":{"default_directory":"/Users/xyz/"}"
and savefile.default_directory

@ysmood
Copy link
Member

ysmood commented Dec 20, 2023

Another way to find out the correct key is to check a real pref when you config the chrome with the browser's settings page, so you don't need to guess it

@sunaynat
Copy link
Author

savefile.default_directory this mentioned in doc

I added this preference also downloadPrefernce := "{"download.default_directory":"/Users/xyz/"}"
and tried with savefile as well but nothing is working. Also in prefernces file also I checked in my system, it doesn't update the file with any way I tried.

@ysmood
Copy link
Member

ysmood commented Dec 21, 2023

Works fine to me:

package main

import (
	"github.com/go-rod/rod"
	"github.com/go-rod/rod/lib/launcher"
	"github.com/go-rod/rod/lib/utils"
)

func main() {
	pref := `{
  "download": {
    "default_directory": "/Users/yad/Downloads/ttt"
  }
}`

	l := launcher.New().Preferences(pref).Headless(false)

	u := l.MustLaunch()

	rod.New().ControlURL(u).MustConnect().MustPage()

	utils.Pause()
}

CleanShot 2023-12-21 at 14 24 05@2x

@ysmood ysmood closed this as completed Dec 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Questions related to rod
Projects
None yet
Development

No branches or pull requests

2 participants