java - How can I know the file name after downloading in automation test? -
i using selenium , testng web ui automation test. download files firefox using same way access file download dialog in firefox. file downloaded default name without open/save/cancel dialog successfully.
i repeat test different test data. problems are
- when there file 'abc.pdf' in target 'browser.download.dir', if download file same name, new file saved 'abc (1).pdf'; that's not want. in following test have problem decide open pdf , check content.
(now solution is, write retry method: check if file downloaded, if yes, move folder; if no, check again. there better way?)
- sometimes when click link or button download, file name generated dynamically web system. file .pdf, .eml, .txt, etc. can know file suffix ui. can't know name in advance. same here, need open file , assertion in following test.
how overcome? if need run test in multiple threads? appreciated!
an easy way wait new file created file watcher: https://docs.oracle.com/javase/tutorial/essential/io/notification.html
this example wait .pdf
file created:
// wait pdf downloaded file file = waitfornewfile(download_folder, ".pdf", 100);
/** * waits new file downloaded file watcher */ public static file waitfornewfile(path folder, string extension, int timeout_sec) throws interruptedexception, ioexception { long end_time = system.currenttimemillis() + timeout_sec * 1000; try (watchservice watcher = filesystems.getdefault().newwatchservice()) { folder.register(watcher, standardwatcheventkinds.entry_modify); (watchkey key; null != (key = watcher.poll(end_time - system.currenttimemillis(), timeunit.milliseconds)); key.reset()) { (watchevent<?> event : key.pollevents()) { file file = folder.resolve(((watchevent<path>)event).context()).tofile(); if (file.tostring().tolowercase().endswith(extension.tolowercase())) return file; } } } return null; }
Comments
Post a Comment