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 prefsdownload.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
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
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.
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
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
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
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.
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 ?