2

We have a process which creates a report file ready for downloading as a xlsx file when the user clicks the Save button I want to view and verify the contents of this file as part of a selenium regression test. The html for the button is

<a class="n-pad btn" download="test-report" href="blob:https://www.testurl.co.uk/9fe176ea-d339-46eb-ac46-95bf300520e5">
<span class="icon-cp-download"></span>
<span>Save</span>
</a>

And I would like to read the file at this point, can anyone advise of the best method to achieve this please.

Thanks

Kev
  • 121
  • 3
  • 20

2 Answers2

1

Based on your question you want to read an excel file(xlsx). the best way to read the contents of it is to use package called ExcelDataReader. install it in your nuget package. it is free and open source.

get excel sheet row id using exceldatareader into dataset

or use this code as an example.

public static DataTable ReadExcelFile(string filePath)
  {
     using (var stream = File.Open(filePath, FileMode.Open, FileAccess.Read))
      {
         using (var reader = ExcelReaderFactory.CreateReader(stream))
        {
        // Choose the specific sheet in the Excel file (e.g., 
         reader.AsDataSet().Tables["Sheet1"])
        var result = reader.AsDataSet().Tables[0];

        return result;
    }
    }
   }

If does not work,

 string url = "blob:testPie.co.uk/c7a70bf0-7495-48f8-b989-be0035eb79fd";
 string fileName = Path.GetFileName(url);

 using (WebClient client = new WebClient())
{
  client.DownloadFile(url, fileName);
  using (FileStream stream = File.OpenRead(fileName))
  {
    // Use the file stream here as needed
    // ...
  }
   // Once you're done with the file, you can delete it
   File.Delete(fileName);
  }
  • Tried this but getting an error So the location of the file is "blob:https://www.testPie.co.uk/c7a70bf0-7495-48f8-b989-be0035eb79fd" This gets assign to href and then this code is run using var stream = File.OpenRead(@href); This result in the following error System.IO.IOException: 'The filename, directory name, or volume label syntax is incorrect. : 'C:\KevsTest\Debug\net6.0\blob:https:\www.testPie.co.uk\c7a70bf0-7495-48f8-b989-be0035eb79fd'' As you can see the local build folder is being pre-pended, any ideas? – Kev Jun 20 '23 at 09:22
  • you first download the file, right? as i understood it correctly. – Murat Ishenbaev Jun 20 '23 at 14:38
  • No, at the point of clicking th read the file "blob:https://www.testurl.co.uk/9fe176ea-d339-46eb-ac46-95bf300520e5" into memory so the autmation test can validate the contents – Kev Jun 20 '23 at 14:42
  • why don't you download the file with Webclient and validate the contents? i pasted the answer above, let me know if it works. – Murat Ishenbaev Jun 20 '23 at 15:01
0

So we have a way round this

By setting the download path to "C:\agent_work\1\s" the tests are able to locate and read thje downloaded reports.

Cool or what...

Kev
  • 121
  • 3
  • 20