Hello,
I need to automate creating a report and that the content of the report matches my desired output.
I have automated previous activities by having a benchmark text file somewhere and comparing it with the newly created text file. I used the sample from the advanced code actions that Ranorex has listed on their site. The difference this time is that the reports are not 100% the same. They are very similiar but each report has the date of when the report was created which fails the validation. I only need to validate a few strings in the text report so they don't have to match each other up to 99%, like they do now.
What would be the best approach to this solution. I read that maybe splitting the text into an array would be good but I don't know if that would be good for comparing two of the same strings against each other.
Thanks,
ATKiwi
Reference parts of two files
Re: Reference parts of two files
Hi,
The easiest thing you can do, is to replace the date string in reference file, with the one created in new file. This is what I do in my tests. Before comparing both files, I'm just reading the date string from new file and then overwriting the date string in reference file. Only after that I'm comparing both files. If you need help with code for your reference file, please upload a sample file. Thanks.
I'm using this code, to get the date string from file:
And this to replace date in ref file:
And here is the ReplaceStringInFile method:
The easiest thing you can do, is to replace the date string in reference file, with the one created in new file. This is what I do in my tests. Before comparing both files, I'm just reading the date string from new file and then overwriting the date string in reference file. Only after that I'm comparing both files. If you need help with code for your reference file, please upload a sample file. Thanks.
I'm using this code, to get the date string from file:
Code: Select all
string pathToFile = "PathToFileString";
string strOutput = @File.ReadAllText(pathToFile);
//obtaining date string from strOutput, using regex pattern
Match match = Regex.Match(strOutput, @regexPattern, RegexOptions.IgnoreCase);
// here we check the Match instance.
if (match.Success)
{
// finally, we get the group value and display it.
findString = match.Groups[0].Value;
Console.WriteLine(findString);
}
Code: Select all
string pathToFile = "PathToFileString";
replaceString = System.DateTime.Now.ToString("M/dd/yyyy");
CodeModules.Common.ReplaceStringInFile(@pathToFile, findString, replaceString);
Code: Select all
/// <summary>
/// This methods replaces reference string in file with new one
/// </summary>
/// <param name="pathToFile">path to file</param>
/// <param name="findString">string to find</param>
/// <param name="replaceString">replace string</param>
public static void ReplaceStringInFile(string pathToFile, string findString, string replaceString)
{
string strFile = File.ReadAllText(@pathToFile);
try
{
File.WriteAllText(@pathToFile, strFile.Replace(findString, replaceString));
}
catch (Exception e)
{
Report.Log(ReportLevel.Warn, "Write to file exception: ", e.ToString());
}
}
Pavel Kudrys
Ranorex explorer at Descartes Systems
Please add these details to your questions:
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
Re: Reference parts of two files
Thanks for the response,
At a high level, I understand your approach. It's a little different for me because the report goes down to the time it was compile (down to the minute) and my understanding of C# isn't that great. If I could just get some clarity so I make sure I got this right.
This chunk reads all the text in pathToFile and looks for @regexPattern. @regexPattern needs to be specifically stated and should equal the string I'm looking for?
Once it finds @regexPattern it will assign all of those to an array called findString.
This one is a bit confusing for me. pathToFile is listed again and I would think that it changes. This time it would be the path to the newly created report?
Attached is the sample reports that I need to compare.
Thanks,
ATKiwi
At a high level, I understand your approach. It's a little different for me because the report goes down to the time it was compile (down to the minute) and my understanding of C# isn't that great. If I could just get some clarity so I make sure I got this right.
Code: Select all
string pathToFile = "PathToFileString";
string strOutput = @File.ReadAllText(pathToFile);
//obtaining date string from strOutput, using regex pattern
Match match = Regex.Match(strOutput, @regexPattern, RegexOptions.IgnoreCase);
// here we check the Match instance.
if (match.Success)
{
// finally, we get the group value and display it.
findString = match.Groups[0].Value;
Console.WriteLine(findString);
}
Once it finds @regexPattern it will assign all of those to an array called findString.
Code: Select all
string pathToFile = "PathToFileString";
replaceString = System.DateTime.Now.ToString("M/dd/yyyy");
CodeModules.Common.ReplaceStringInFile(@pathToFile, findString, replaceString);
Attached is the sample reports that I need to compare.
Thanks,
ATKiwi
You do not have the required permissions to view the files attached to this post.
Re: Reference parts of two files
Hi,
Of course, pathToFile variable must be filled with the path pointing to ref report or newly created report.
As for the regex patter, the one which fits your need is this:
This should find the date/time string in report file.
The only remaining problem is the number of spaces between date and time. In my code, I need to replace just date, so I don't have to deal with spaces. I think that in your case, it would be easier to replace first date, then time. I mean doing the replace in two steps, instead of replacing date/time at once. So then you should use this regex for date:
and this for time:
Of course, pathToFile variable must be filled with the path pointing to ref report or newly created report.
As for the regex patter, the one which fits your need is this:
Code: Select all
((\d{2})\/(\d{2})\/(\d{4}))\s*((\d{2}):(\d{2}))
The only remaining problem is the number of spaces between date and time. In my code, I need to replace just date, so I don't have to deal with spaces. I think that in your case, it would be easier to replace first date, then time. I mean doing the replace in two steps, instead of replacing date/time at once. So then you should use this regex for date:
Code: Select all
((\d{2})\/(\d{2})\/(\d{4}))
Code: Select all
((\d{2}):(\d{2}))
Pavel Kudrys
Ranorex explorer at Descartes Systems
Please add these details to your questions:
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