I have following check in my WebDriver script, where I am pulling a date field out of a page.
IWebElement crInfo = driver.FindElement(By.Id("crInfo"));
string copyDate = crInfo.Text;
// From the converted string now pulling out the year by index and length
string copyYear = copyDate.Substring(2, 4);
// Get the current year
int nowYear = DateTime.Today.Year;
// Converting the year
nowYear.ToString().Trim();
// Make the comparison to be sure the copyright is using the current date
Assert.AreEqual(copyYear,nowYear);
As noted in the snippet what I am trying to do is confirm that the date that appears in the page is the current year, this is just a web front end check that the function put in place is returning the right value. When I run this though what I see in the NUnit console is:
error: Expected: “2012”
But was: 2012
I don’t really get the difference between the two, is the quoted value a string? I added the conversion in my script to be sure that they are the same type and added the trim in case there might be white space.
If I want to make this work, what is it that I am not doing to get the Assetion to pass?
Look at your types and the assertion:
That assertion will never ever pass, whatever the values are. A string is never equal to an integer in C#. While there are languages which do type coercion in this sort of scenario, C# isn’t one of them.
Also note that this line is useless:
Strings are immutable in .NET – methods like
Trim()don’t change the string they’re called on – they return a new string. You’re ignoring that return value… and anyway, you’re calling it on a string you completely ignore afterwards. You could use:… or you could parse
copyYearto anintfirst instead, and compare that instead: