18 January 2026:


This forum is now archived and is in read-only mode. Please continue discussions on our improved new Sahi Pro Community forum.



Sahi Pro is an enterprise grade test automation platform which can automate web, mobile, API, windows and java based applications and SAP.

Alternate option to download a text file

MeghaMegha Members
edited November -1 in Sahi - Open Source
HI,

I am trying to download a text file by adding text/plain MIME type in my download_contenttypes.txt file. Though this worked, it screwed up the other scenarios.

Please guide me, if download of text file can be done in an alternate way.

Thanks,
Megha

Comments

  • narayannarayan Administrators
    You can add the url of the file to be downloaded in download_urls.txt (from Configure link on Sahi Dashboard). Also remove text/plain from download_contenttypes.txt

    For example if your file is called downloadMe.txt, add

    .*downloadMe[.]txt.*

    Save, restart Sahi and check.

    Regards,
    Narayan
  • MeghaMegha Members
    Hi Narayan,

    I used the code suggested by you in download_urls.txt. Restarted my twist. But it doesnot work for me. Here is the code i have used.

    public void verifyIfFileGotGenerated() throws Exception {
    File textFile = MiscFunctions.lastDownloadedFile("Anemia.txt",browser);
    assertTrue(textFile.exists());
    assertTrue(textFile.getTotalSpace() > 0);
    }

    public static File lastDownloadedFile(String fileName, Browser browser) {
    File oldFile = new File(SAHI_USERDATA_DOWNLOAD_LOCATION + fileName);
    if (oldFile.exists()) {
    oldFile.delete();
    }

    String path = SAHI_TEMP_DOWNLOAD_LOCATION + fileName;
    browser.saveDownloadedAs(path);
    return new File(SAHI_USERDATA_DOWNLOAD_LOCATION + fileName);
    }

    Please guide me if this is the right way to handle the download of a text file.


    Thanks,
    Megha
  • _dkulkarni_dkulkarni Members
    edited June 2011
    Hello Megha,

    Sahi already has a method to fetch the name of the last downloaded file. You can use that instead.
            assertEquals("Anemia.txt", browser.lastDownloadedFileName());
    

    For reference, this is how I handled my file download.
    public void testDownload() throws Exception {
            browser.navigateTo("http://****");
            String absolutePath = "/home/user/sahi/userdata/Anemia_new.txt";
            File file = new File(absolutePath);
            if (file.exists()) file.delete();
            assertTrue(!file.exists());
            browser.link("Anemia.txt").click();
            assertEquals("Anemia.txt", browser.lastDownloadedFileName());
            String filePath = "Anemia_new.txt";
            browser.saveDownloadedAs(filePath);
            assertTrue(file.exists());    
            if (file.exists()) file.delete();
        }
    
    Regards,
    dkulkarni
Sign In or Register to comment.