Selenium driver location search path - c#

I'm trying to set up selenium tests in nCrunch which outputs the tests to its own temp folder.
I'm getting the following error when I create the driver (new PhantomJSDriver()):
OpenQA.Selenium.DriverServiceNotFoundException : The PhantomJS.exe file does not exist in the current directory or in a directory on the PATH environment variable.
However I have checked and PhantomJS.exe does exist in the current directory (\bin\debug).
I then tried using new PhantomJSDriver(".\\") which should be the current directory and that does work.
What is the "current directory" Selenium is referring to in this message?

Rather than assuming ".\\", get the current working working directory by Directory.GetCurrentDirectory or System.AppDomain.CurrentDomain.BaseDirectory . Take a look at Get current folder path.

new PhantomJSDriver() will use your bin folder
if PhantomJS.exe does not exist there, try to find where it's located and insert full path in constructor
new PhantomJSDriver("real_path_to_PhantomJS.exe")

Related

Excel files are not being read when executing Selenium C# Scripts on Azure Releases

I have some Selenium C# tests hosted on Azure which they need to look for the pre-built excel file in project tree, stored inside bin folder, to execute some file upload validation scenarios.
When they are executed locally these scenarios pass without any problem, but when it comes to be executed on the Azure they receive the following error.
invalid argument: File not found : D:\a\r1\a_Selenium_Tests\TestApplication\bin\Debug\netcoreapp3.1\Files\SC003_CT014_ActiveEmployees.xlsx
The files do exists in the following path: ...\bin\Debug\netcoreapp3.1\Files...
And the code I use to them is:
string root = Directory.GetCurrentDirectory() + "\\Files\\" + file;
Do you know if there's a missing file configuration or building the filePath in another way?
Thanks for your help :D
Directory.GetCurrentDirection() returns the current working directory, not the folder in which the DLL file resides. The current working directory is a different thing. In your case, the current working directory is probably something like D:\a\r1\. Instead, you need to get the folder in which the test assembly resides:
var binDirectory = Path.GetDirectoryName(GetType().Assembly.Location);
// ^^^^^^^^^
var excelFilePath = Path.Combine(binDirectory, "Files", "SC003_CT014_ActiveEmployees.xlsx");
Note: Replace GetType() with typeof(ClassName) if you are executing your code from a static method, or you would like to specify a path to a different assembly than the one that is currently executing.

Write a file to Roaming folder using NSIS script

I am using NSIS to make exe for a desktop application in c# and i have to write few files to the AppData Roaming folder for the user
I tried the below code
!define ROAMING_FOLDER_ROOT "$APPDATA\APPDUMMY\APPFILES"
MessageBox MB_OK 'AppDATA FOLDER "${ROAMING_FOLDER_ROOT}"' #here i am getting the correct path of the Appdata roaming folder frm variable
Section -Additional
SetOutPath "$ROAMING_FOLDER_ROOT"
SetOverwrite off
File "C:\MYAPPSOURCECODE\BIN\BookStore.sqlite"
SetOverwrite ifnewer
File "C:\MYAPPSOURCECODE\BIN\AppSettings.xml"
File "C:\MYAPPSOURCECODE\BIN\Resources\defData.xml"
File "C:\MYAPPSOURCECODE\BIN\Resources\dummy.html"
SetOutPath "$ROAMING_FOLDER_ROOT\Resources"
File "C:\MYAPPSOURCECODE\BIN\Resources\appjsfile.js"
SectionEnd
While i am trying to do the same with $LocalAppData its writing to the AppDAta Local folder but i want to make it writable to Roaming folder
If you look at the code you posted you see that in the MessageBox call you referenced the ${ROAMING_FOLDER_ROOT} define but when calling SetOutPath you are referencing a variable called $ROAMING_FOLDER_ROOT and this probably produces a compiler warning. Make sure that you use the ${} syntax when accessing defines!
NSIS has a concept called the shell context and the $AppData constant is affected by this:
SetShellVarContext current ; Current is the default
DetailPrint AppData=$AppData ; C:\Users\%username%\AppData\Roaming
SetShellVarContext all
DetailPrint AppData=$AppData ; C:\ProgramData (This is in the All Users folder on XP)
SetShellVarContext current ; Restore it back to the default
Ah, seems,
you are using common shell context
Try to set
SetShellVarContext current
before you are getting $APPDATA.
Var ROAMING_FOLDER_ROOT
MessageBox MB_OK 'AppDATA FOLDER "${ROAMING_FOLDER_ROOT}"' #here i am getting the correct path of the Appdata roaming folder frm variable
Section -Additional
SetShellVarContext current
StrCpy $ROAMING_FOLDER_ROOT "$APPDATA\APPDUMMY\APPFILES"
SetOutPath "$ROAMING_FOLDER_ROOT"
SetOverwrite off
File "C:\MYAPPSOURCECODE\Models\BookStore.sqlite"
SetOverwrite ifnewer
File "C:\MYAPPSOURCECODE\BIN\AppSettings.xml"
File "C:\MYAPPSOURCECODE\BIN\Resources\defData.xml"
File "C:\MYAPPSOURCECODE\BIN\Resources\dummy.html"
SetOutPath "$ROAMING_FOLDER_ROOT\Resources"
File "C:\MYAPPSOURCECODE\BIN\Resources\appjsfile.js"
SectionEnd

Could not find a part of the path 'C:\Program Files (x86)\IIS Express\~\TextFiles\ActiveUsers.txt'

I tried many ways to access a text file in my Visual Studio 2012 Solution from a folder named TextFiles
using (System.IO.StreamWriter file = new System.IO.StreamWriter(#"~/TextFiles/ActiveUsers.txt", true))
{
file.WriteLine(model.UserName.ToString());
}
But it kept on throwing the error
Could not find a part of the path 'C:\Program Files (x86)\IIS
Express\~\TextFiles\ActiveUsers.txt'.
Not sure where I made a mistake
You need to use HttpServerUtility.MapPath which will turn the ~/ portion of the path in to the real location it resildes on your hard drive.
So that would change your code to (assuming you are in one of the IIS classes that expose a Server property to it's methods)
var path = Server.MapPath(#"~/TextFiles/ActiveUsers.txt");
using (System.IO.StreamWriter file = new System.IO.StreamWriter(path, true))
{
file.WriteLine(model.UserName.ToString());
}
I ran into a similar issue and ended up using
string sFileName = HttpContext.Current.Server.MapPath(#"~/dirname/readme.txt");
This is an old question but I just ran into this problem myself and wanted to add what I've just discovered, in case it's helpful to anyone else.
If you have UAC turned off but are not running with elevated permissions, and try to write to restricted files (e.g. the "Program Files" folder) you'll get the "could not find a part of the path" error, instead of the (correct) access denied error.
To eliminate the problem, run with elevated permissions as in this solution: https://stackoverflow.com/a/1885543/3838199
~ is not the "user home" or anything else in Windows. You can still set the path as relative to the working directory (where the executable is) by just not specifying a full path.
For .netcore 3.x
You should make use of IWebHostEnvironment using dependency injection.
You can then use it in your code this way
string wwwRootPath = _hostEnvironment.WebRootPath;
string path = Path.Combine(wwwRootPath, $"TextFiles{Path.PathSeparator}ActiveUsers.txt");
Ensure to use PathSeparator otherwise you might face the same error due to the variance in your hosting environment.

UnauthorizedAccessException when downloading a file using the TFS SDK

When I try to download a file from TFS version control SDK to my computer I receive an 'UnauthorizedAccessException' saying Access to the local path I'm trying to download to is denied. I included a stripped down version of the code I am using below.
var projectCollection = GetProjectCollection();
var versionControl = (VersionControlServer)projectCollection.GetService(typeof(VersionControlServer));
versionControl.DownloadFile('$/path to file', 'local path to download to');
Does anyone know how to resolve this issue?
I found the issue.
The second argument in DownloadFile() needs to be the file name it will be downloaded as and not the parent directory it will be placed in. I thought it just needed the directory name.
So instead of what I originally had
versionControl.DownloadFile("$/Readme.txt", "C:\\Temp");
it needs to be
versionControl.DownloadFile("$/Readme.txt", "C:\\Temp\\Readme.txt");
This is because the process does not have rights to the local path. Make sure the local path has the appropriate right set to the user that is running the process.

Relative path in .NET

I am running my C# application from "D:\App\program.cs". My application needs to execute a file placed in "C:\Program Files\software\abc.exe".
How can I set the relative path in my program to execute the "abc.exe"?
To answer the question how to get a path that shows the relative location of pathB from pathA. You can use the Uri class to get the relative path.
string pathA = #"C:\App\program.cs";
string pathB = #"C:\program files\software\abc.exe";
System.Uri uriA = new Uri(pathA);
System.Uri uriB = new Uri(pathB);
Uri relativeUri = uriA.MakeRelativeUri(uriB);
string relativeToA = relativeUri.ToString();
Console.WriteLine(relativeToA);
This yields "../program%20files/software/abc.exe" for the relative path.
I have changed your example from D to C though because you can't have a relative path for two locations on different drive letters, although the above code still works, just yields the absolute.
OR if the c# bit is a red herring, and as I now understand you want to run a batch file:
in batch file put:
cd c:\program files\software\
abc.exe
abc.exe will then execute from software folder and not folder of the batch file.
You shouldn't be using relative path for referring something from Program Files. I would recommend to use Environment.GetFolderPath (and Environment.SpecialFolder) to get path to Program Files and then use some config setting to get reminder path to the program.
It's not clear what you mean by "set relative path", but if you're using Process and ProcessStartInfo to run the executable, I would suggest that you use an absolute path to specify the executable, and ProcessStartInfo.WorkingDirectory to tell the process where to run (so that relative paths will be evaluated appropriately within the new process).
EDIT: If you want the batch file to run c:\Program Files\Software\abc.exe then the contents of the batch file should be just:
"c:\Program Files\Software\abc.exe"
(Note the quotes to allow for space.)
I don't see what this has got to do with relative pathnames though.
If the application is in Program Files then you can create a batch file like
"%ProgramFiles%\software\abc.exe"

Categories