Web app - Chrome - File downloads do not work - When chrome capabilities set programatically

chrome

#1

Hi,

I am trying to download a file from a web app ( using chrome and chromedriver )
I need to verify the file has been downloaded or not in the test.
So I customize chrome driver to automatically download the file in a particular location , programatically , so that code can go there and check.

I know we can do Project > Desired Capabilities > Web UI > Chrome > and add capabilities
and add prefs download.default_directory

but this will be a fixed location in the system
as the code can run in any other machine , i set this programatically as below to always consider a relative path to the project directory

ChromeDriver driver ;
System.setProperty(“webdriver.chrome.driver”, DriverFactory.getChromeDriverPath());

HashMap<String, Object> chromePrefs = new HashMap<String, Object>();
chromePrefs.put("download.default_directory", "local_directory/downloadPath");

ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("prefs", chromePrefs);

DesiredCapabilities cap = DesiredCapabilities.chrome();
cap.setCapability(ChromeOptions.CAPABILITY, options);

driver =  new ChromeDriver(cap);
driver.manage().timeouts().implicitlyWait(GlobalVariable.globalWaitTime, TimeUnit.SECONDS);
DriverFactory.changeWebDriver(driver);

Here am creating new driver instance and passing that to Katalone webdriver.
Everything worrks
but when a file download action happens, by clicking on a link or a button … the file download is always getting errored out

as in the attached picture

Katalone version : 6.2.1 build 2
Google Chrome : 75.0.3770.100
I think this was working in the earlier versions of Katalon


#2

Hello @kazurayam @Russ_Thomas

Have you seen this issue ?

Thannks


#3

I can’t think of any way to find out what happened during the download. But, if you are certain this exact same code worked in an earlier version of Katalon, it would probably help if you can find out which version.


#4

Please check the error log emitted by Katalon Studio when “file downloads did not work”.

You can check it by
Katalon Studio GUI Tool bar > Help > Error Log

Possibly you would find some IOException with stack trace. That will give you a clue to resolve your issue.


#5

Kaz, he says the problem is in the browser - he says it all seems to work from the test…

IOW, I don’t think Katalon is aware there is a problem/error.


#6

Yes, I don’t see any thing unusual in the logs

The chrome browser was updated to latest 75.0.3770.100 couple of days back
I am thinking does that has any link to this issue


#7

If you follow my advice (try an older version, or two) it will help the devs help you.


#8

sure @Russ_Thomas , let me try

Looks like there is no issue with Chrome latest version too

the below piece of code is the reason

chromePrefs.put(“download.default_directory”, downloadPath);

If i comment this line, the download works fine , but it will get downloaded to users downloads folder
The selenium version used by katalon is 3.141.59
i think some thing wrong there


#9

@devalex88 @ThanhTo


#10

Hi @musaffir.puthukudi

Before we can conclude there’s a problem elsewhere, can you check if the path string is properly escaped ?

There’s this answer on StackOverflow that may give us some hints:

Cheers !


#11

I narrow down the root cause of this
It looks like the issue is not with the below line of code too

chromePrefs.put(“download.default_directory”, downloadPath);

Its wrong in the downloadPath value
If I pass a fixed path there with double quotes, download works and file gets saved

I get the project directory first by below line

String fileDir = RunConfiguration.getProjectDir();

and then forming Download paths by below lines. The issue is here in how it separates the Download folder from the project directory

Tried many options so far like File.separator which should ideally give OS specific file separator
all seems to give probs

Thanks


#12

Where is your “below lines”? I can not find it in your post.


#13

RunConfiguration.getProjectDir() gives the project directory path with single forward slash – “/”

But File.Seperator groovy on windows give single backward slash “”

so file download path in Chrome browser was getting formed something as

so fixed this with replacing it with, escaping the slashes and making it unique

String fileDir = RunConfiguration.getProjectDir().replaceAll("\/","\\")
String downloadPath = fileDir + fileSeparator + “Downloads”;
and passing this downloadPath to chromedriver as options

Thank you all


#14
@kazurayam

Apologies , I missed that line earlier.
Updated it in my above answer

I have fixed the issue as above
RunConfiguration.getProjectDir() gives project path as Single forward slash, and not identical to File.Seperator one. That was causing prob here
Just did a replaceAll and it works now. Thanks again


#15

I would recommend to you to study the NIO2 Path API. It is much cleaner/better API than using java.io.File + java.lang.String + java.io.File.separator.

Have a look at the following article.

See the following example from the section “8. Joining Paths”:

@Test
public void givenTwoPaths_whenJoinsAndResolves_thenCorrect() {
    Path p = Paths.get("/baeldung/articles");
 
    Path p2 = p.resolve("java");
  
    assertEquals("\\baeldung\\articles\\java", p2.toString());
}

Using java.nio.file.Path.resolve(), you are no longer be annoyed by the difference of File separator characters (\ or /) depending on which platform you are on.


#16

Thank you so much @kazurayam

One question here, how do you make these external libraries available to katalon env then ?
You should download the jar and put it in the ./configuration (or) ./plugins folder of Katalon installation ?

Warm Regards
Musaffir


#17

The java.nio.file.Path is the core part of Java since Java 7.
https://docs.oracle.com/javase/8/docs/api/java/nio/file/Path.html

Katalon Studio bundles the Java 8 runtime. You can use it right now. You don’t have to download any external jar to use java.nio.file.Path.