How to set relative path of excel file in Usercode.cs?

Best practices, code snippets for common functionality, examples, and guidelines.
Madhumita Ghosh3
Posts: 8
Joined: Mon Dec 19, 2016 8:53 am

How to set relative path of excel file in Usercode.cs?

Post by Madhumita Ghosh3 » Fri Feb 03, 2017 6:18 am

Hi,

I have gone through the posts in this forum which states how to set relative path of excel file in the 'Manage Data Sources' part by checking the 'Copy file to project' checkbox. I have done the same in my testcase. Now I want to set the relative path of the same excel file in my usercode as I need to write some values in my excel sheet from web application. Please follow the code which am using for writing in excel file:

var excelApp = new Excel.Application();
excelApp.Visible = true;
excelApp.Workbooks.Open("C:/Work/Automation/TowCall_D3.xlsx");
Excel._Worksheet worksheet = (Excel.Worksheet)excelApp.ActiveSheet;
worksheet.Cells[2,"AS"] = strcallID;
excelApp.ActiveWorkbook.Save();
excelApp.Quit();

For the line where am setting the excel file path I want to use a relative path just like it happens in 'Manage Data Sources' part by checking the 'Copy file to project' checkbox.

Note: I want to use a relative path because the scripts would be run in different machines and hence users need to create a folder in their local machine (C:/Work/Automation/TowCall_D3.xlsx). Also replace the excel everytime it is updated by 1 user.

Please let me know the solution for this. Thanks in advance.
Attachments
ManageDataSources.jpg
ManageDataSources.jpg (60.71 KiB) Viewed 1133 times

Madhumita Ghosh3
Posts: 8
Joined: Mon Dec 19, 2016 8:53 am

Re: How to set relative path of excel file in Usercode.cs?

Post by Madhumita Ghosh3 » Mon Feb 06, 2017 8:43 am

Please let me know if there is any solution to my above stated problem. Looking forward to your reply.
Thanks in advance.

User avatar
odklizec
Ranorex Guru
Ranorex Guru
Posts: 3930
Joined: Mon Aug 13, 2012 9:54 am
Location: Zilina, Slovakia

Re: How to set relative path of excel file in Usercode.cs?

Post by odklizec » Mon Feb 06, 2017 9:08 am

Hi,

A relative path to what...the test exe? In this case, you can use something like this:

Code: Select all

string getActualDir = TestSuite.WorkingDirectory.ToString(); 
excelApp.Workbooks.Open(getActualDir  + "\\TowCall_D3.xlsx");
...
Hope this helps?

BTW, users should not be needed to create anything manually! I would strongly suggest to use a proper version control system, to distribute your tests (ideally combined with continuous integration system, like Jenkins, Bamboo, etc...). Copying files manually is a certain way to mess and confusion ;)
Pavel Kudrys
Ranorex explorer at Descartes Systems

Please add these details to your questions:
  • Ranorex Snapshot. Learn how to create one >here<
  • Ranorex xPath of problematic element(s)
  • Ranorex version
  • OS version
  • HW configuration