简体   繁体   中英

wildcard in URL for webdriver

Hi i have a small problem with URL, i need to verify PDF file, which i must first download. I use this code below:

    URL TestURL2 = new URL("file:///C:/Users/Ludek/Downloads/katalog_dokument_53766_test.pdf");
    BufferedInputStream TestFile = new BufferedInputStream(TestURL2.openStream());
    PDFParser TestPDF2 = new PDFParser(TestFile);
    TestPDF2.parse();
    String TestText2 = new PDFTextStripper().getText(TestPDF2.getPDDocument());
    try {
        assertTrue(TestText2.contains("Text PDF"));
        System.out.println("it's  OK");
    } catch (Error e) {
        verificationErrors.append(e.toString());
        System.out.println("not OK!!!");
    }

But numbers 53766 in file name are everytime different. Can i use some wildcard or something else? Can anyone advise me please? Thanks

If there is only one file in your folder - your pdf, you can use a code similar to this one:

 string[] files = System.IO.Directory.GetFiles(@"C:/Users/Ludek/Downloads/", "*.pdf");

Before the download process, you can always delete all files from the folder or even always specify a unique download folder, this way only your file will be there and you can be sure when you verify it.

I used this solution, it's simple and works.

  File f = new File("C:/Users/Ludek/Downloads/");
        FilenameFilter textFilter = new FilenameFilter() {
            public boolean accept(File dir, String name) {
                return name.toLowerCase().endsWith(".pdf");
            }
        };
        File[] files = f.listFiles(textFilter);
        for (File file : files) {
            String URL =  file.getCanonicalPath().substring(25);
            URL TestURL2 = new URL("file:C:/Users/Ludek/Downloads/" + URL);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM