Coded UI Test Result File - c#

I use coded UI to run test and get the test result file named like qian_machinename 2011-12-21 14_26_10. I want to read the file and send a test report. My question is how can I get the file time every time I run the tests?

TestContext has 3 properties which you can use
1. TestDir
2. TestDeploymentDir
3. TestResultsDirectory.
You can use these properties to navigate to the folder you are interested in and then get the result file for your processing.

QianLi,
Perhaps you can get the proper output file by using a known pre-fix on the test output filename.
In Visual Studio navigate Test->Edit Test Settings->(Select your active .testsettings)->General
In the prompt that displays you will see an area for naming scheme. By default this is set to name your output file "USER#MACHINE DATE TIME". You can create a user defined scheme and use that to locate the file i.e. store "MyTestOuput" as a pre-fix and then later in code you can examine the file creation date/time if necessary to verify you have the correct output.

Use something Like :
FileName=
testContext.ResultsDirectory + "\" + testContext.TestName.ToString()+".extension"
Testname should be the name of the testMethod Like "T1".
Extension could be any valid file type e.g. .xml etc.

[TestCleanup()]
public void MyTestCleanup()
{
string nomfichiersource = "UITestActionLog.html";
string nomTest = TestContext.TestName.ToString();
string sourcefile = System.IO.Path.Combine(TestContext.TestResultsDirectory, nomfichiersource);
string destfile = System.IO.Path.Combine(#"X:\Temp", nomTest + ".html");
System.IO.File.Copy(sourcefile, destfile);
}

Related

Embedding Text File in Resources C#

I'm attempting to do two things. I want to embed a text file into my project so that I can utilise it and modify it, but at the same time I don't want to have to package it when I send the project out to users (I.E included in the exe file).
I've had a look around and there's been multiple questions already but I just cant seem to get any to work. Here's the steps I've taken so far;
Added the text file to my "Resources Folder"
Build action to "Content" and output directory to "Do not copy"
I then try to access the file in my code;
if (File.Exists(Properties.Resources.company_map_template))
{
MessageBox.Show("Test");
var objReader = new StreamReader(Properties.Resources.company_map_template);
string line = "";
line = objReader.ReadToEnd();
objReader.Close();
line = line.Replace("[latlong]", latitude + ", " + longitude);
mapWebBrowser.NavigateToString(line);
}
The MessageBox never appears which to me means that it cannot find the file and somewhere somehow I've done something wrong. How can I add the file into my project so I don't need to distribute with an exe whilst being able to access it in code?
I would use the following:
BuildAction to None (not needed)
and add your file to Resources.resx under files (using DragAndDrop from SolutionExplorer to opened Resources.resx)
Access to your Text:
using YOURNAMESPACE.Configuration.Properties;
string fileContent = Resources.company_map_template;
Then you're done. You don't need to access through StreamReader

how do you get File Listing Details on c#

i'm currently doing an assignment at university and i'm struggling on a specific task
After displaying a file listing i need to prompt the user to enter the number of a file to get more details on that file. The user can then enter the number 0 to skip this step. The extra details shown should be:
File: notepad.exe
Full file name: C:\Windows\notepad.exe
File size: 93536 bytes
Created: 14/07/2009 12:54:24
Last accessed: 10/08/2009 15:21:05
im using C# im wondering if anyone knows how to guide me on the right step? thankyou
For general file information, like size and creation and modification times, use the FileInfo class.
FileInfo f = new FileInfo(#"C:\Windows\Notepad.exe");
long size = f.Length;
DateTime creation = f.CreationTime;
DateTime modification = f.LastWriteTime;
string name = f.Name; //returns "Notepad.exe"
//etc...
Alternatively, for getting a filename from a full path, use the Path class.
string fName = Path.GetFilename(#"C:\Windows\Notepad.exe"); //returns "Notepad.exe"
I'll leave formatting the info string to you.
Be advised that FileInfo depends on the existence of the file, while the Path methods only deal with string manipulation. The file does not need to exist.

Reading and Writing to a Text File within the Release Folder of a C# Console App

I have a C# console application that uses text from a txt file as a parameter for a SQL query. After the query runs, it stores the highest primary key in the text file. The next time it runs, it uses the first line of that text file in the WHERE statement to grab primary keys higher than the previously stored one. Here's the code I'm using:
// get latest primary key
static String mostRecentID= File.ReadAllLines(#"C:\DataStorage\latestID.txt").First().Trim();
// run the query
SELECT * FROM whatever WHERE pk > mostRecentID
// store latest primary key
DataRow mostRecentIDRow= exportTable.Select().FirstOrDefault();
if (mostRecentIDRow!= null)
{
mostRecentID = mostRecentIDRow[0].ToString().Trim();
File.WriteAllLines(#"C:\DataStorage\latestID.txt", new String[] { mostRecentID});
}
I need to be able to read and write to this text file independent of where the program or the file is located. Is there a way to do this while keeping it in the release folder of the program?
Just use this variable:
string myDirectory = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
to get the directory where you program executes.
So when you place your .txt in Release Folder you can use:
File.WriteAllLines(myDirectory + "/" + "latestID.txt", new String[] { mostRecentID});
If you are trying to keep the text file in the root location of the application use this
string file = Path.Combine(
System.Reflection.Assembly.GetExecutingAssembly().Location, "latestID.txt");
you can get the path of the current running assembly like this:
string path = System.IO.Path.GetDirectoryName(
System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase );
and there you can save/load your file

How do I remove version number from file path? - Winforms c#

I am wondering how to remove the version number from a file path in a Windows Form Application.
Currently I wish to save some users application data to a .xml file located in the roaming user profile settings.
To do this I use:
get
{
return Application.UserAppDataPath + "\\FileName.xml";
}
However this returns the following string:
C:\Users\user\AppData\Roaming\folder\subfolder\1.0.0.0\FileName.xml
and I was wondering if there is a non-hack way to remove the version number from the file path so the file path looks like this:
C:\Users\user\AppData\Roaming\folder\subfolder\FileName.xml
Besides parsing the string looking for the last "\", I do not know what to do.
Thanks
Use Directory.GetParent method for this purpose.
get
{
var dir = Directory.GetParent(Application.UserAppDataPath);
return Path.Combine(dir.FullName, "FileName.xml");
}
Also note that I've used Path.Combine instead of concatenating paths, this method helps you to avoid so many problems. Never concatenate strings to create path.

C# getting full path of an input file in win XP

I wrote a simple console tool that reads a file and then writes something out. I intend to just drag and drop files and then out pops the output in the same directory as the input file.
All of the testing works, and when I call it from command-line, everything comes out as expected. However, when I tried dragging and dropping it in explorer, no files were created.
I did a search through the system and found that they were all dumped at Documents and Settings under my user folder, and when I printed out the full path that's what it said.
Which is weird. Wouldn't Path.GetFullPath return the absolute path of the input file? Instead it looks like it just combined that user directory path to the input's filename.
EDIT: here's the code. I feel like I've made a logic error somewhere but can't seem to see it.
filename = System.IO.Path.GetFileName(args[i]);
abspath = Path.GetFullPath(filename);
dirpath = Path.GetDirectoryName(abspath);
....
Console.WriteLine(dirpath);
Path.GetFullPath should return the absolute path of the path string you pass in.
Path.GetFileName(string path) only returns the filename and extension of the file you pass in. For example, System.IO.Path.GetFileName("C:\SomeDirectory\Test.txt"); would just return "Test.txt". You'll want to use the Path.GetDirectoryName to get the path of your input file, like so:
string inputDirectory = System.IO.Path.GetDirectoryName(args[i]);
Alternately, you can use the FileInfo class to retrieve a bunch more information about your input file. For example:
// Assuming args[i] = "C:\SomeDirectory\Test.txt"
FileInfo inputFile = new FileInfo(args[i]);
string inputDirectory = inputFile.DirectoryName; // "C:\SomeDirectory"
string inputFileName = inputFile.Name; // "Test.txt"
string fullInputFile = inputFile.FullName; // "C:\SomeDirectory\Test.txt"

Categories