How to verify the name of a downloaded file?

Hi!

I need to access the downloads from my web browser to verify the name of a file that I just downloaded but I cannot access page

I try this :

WebUI.openBrowser(’’)

WebUI.navigateToUrl(‘chrome://downloads/’)

Can you help me, please ?

I am not sure what you want to do, as you described your problem too little.

I just guessed what you want to do, and made a reference implementation of my guess. You can copy&paste the following code into a blank Test Case, and run.

import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.Paths
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter;
import java.util.regex.Matcher
import java.util.regex.Pattern
import java.util.stream.Collectors

// get the current timestamp in the format of yyyyMMdd_hhmmss
LocalDateTime now = LocalDateTime.now()
String timestamp = DateTimeFormatter.ofPattern("yyyyMMdd_hhmmss").format(now)

// prepare a regular expression to match the file
Pattern pattern = Pattern.compile('^(myFile\\-)(\\d{8}_\\d{6})(\\.txt)$')

// find the Path of 'Downloads' directory of my OS user
Path userHome = Paths.get(System.getProperty("user.home"))
Path downloads = userHome.resolve('Downloads')
println(downloads.toString())

// get the set of files contained in the Downloads directory now (before insertion)
Set<Path> previousFileSet = Files.list(downloads).collect(Collectors.toSet())

// insert an anonymous file   (equivalent to downloading a file from web)
Path newFile = downloads.resolve("myFile-${timestamp}.txt")
newFile.toFile().text = "Hello, world!"

// get the set of files contained in the Downloads directory now (after insertion)
Set<Path> currentFileSet = Files.list(downloads).collect(Collectors.toSet())

// calculate the Relative Complement of the file sets ("after" minus "before")
Set<Path> differenceSet = new HashSet<Path>(currentFileSet)
differenceSet.removeAll(previousFileSet)

// how many files were inserted? should be 1 or more
assert differenceSet.size() >= 1

// know the file name
Path differenceFile = differenceSet.toList().get(0)
String fileName = differenceFile.getFileName().toString()

// verify the name of inserted file
Matcher matcher = pattern.matcher(fileName)
assert matcher.matches()
assert matcher.group(1) == 'myFile-'
assert matcher.group(3) == '.txt'

// now we know the timestamp part of the file name inserted
println "timestamp was ${matcher.group(2)}"

Sample output in the Console:

...
2021-06-25 23:38:23.824 DEBUG testcase.Downloads                       - 6: println(downloads.toString())
/Users/kazurayam/Downloads
...
2021-06-25 23:38:24.092 DEBUG testcase.Downloads                       - 20: println(timestamp was $matcher.group(2))
timestamp was 20210625_113823

1 Like