Validation Exception

Class library usage, coding and language questions.
CookieMonster
Certified Professional
Certified Professional
Posts: 74
Joined: Mon Aug 14, 2006 7:17 pm
Location: CH

Validation Exception

Post by CookieMonster » Fri Mar 27, 2015 2:44 pm

Hi,

May someone can help me out, I have small "cosmetic" problem.

My problem is when Validation.Attribute is called, even if it fails it will enter a report log. But I want only that code line in the exception is called, when it fails!
That means, if the validation passes, then it should write the message which I pass, and if it throws an exception, only the Report.Log should be called.

I hope I was somehow clear, if not please let me know.

Thanks in advance for your help

Cheers
Dan

Ranorex 5.3.0 / Win 7 / IE 11
public string GetAndValidateText(RepoItemInfo repoInfo, string attributeValue, string expectedValue)
{
	string elementValue = string.Empty;
	
	try
	{
		repoInfo.WaitForExists(new Duration(AutomationLayerConstants.thirtySecDuration));
				
		var element = repoInfo.CreateAdapter<Unknown>(true);
		if(base.ValidateElementAttributeNoReport(repoInfo, "Visible", true))
		{
			if(element.Element.FlavorName.Equals("web") && element.Element.PreferredCapability.ShortName.Equals("select"))
			{
				WebElement webElement = element.Element;
				GetOptionTagAttributeValue(repoInfo, webElement, attributeValue, expectedValue, false);
			}
			else
			{
				elementValue = element.Element.GetAttributeValueText(attributeValue);
				Validate.Attribute(repoInfo, attributeValue, expectedValue, "The expected value {3} from item " + repoInfo.Name + " of type: " + element.Element.PreferredCapability.DisplayName + " on attribute {1} could be validated");
			}
		}
				
	}
	catch(ValidationException)
	{
		Report.Log(ReportLevel.Failure, string.Format("The expected value {0}, doesn't match with value {1} found on the validated element", expectedValue, elementValue));
	}
		
	return elementValue;
}

krstcs
Ranorex Guru
Posts: 2683
Joined: Tue Feb 07, 2012 4:14 pm
Location: Austin, Texas, USA

Re: Validation Exception

Post by krstcs » Fri Mar 27, 2015 3:03 pm

Validate will always print to the report.

If you don't want that, then you should use an equality operation instead of the Validate object.
Last edited by krstcs on Fri Mar 27, 2015 3:30 pm, edited 1 time in total.
Shortcuts usually aren't...

CookieMonster
Certified Professional
Certified Professional
Posts: 74
Joined: Mon Aug 14, 2006 7:17 pm
Location: CH

Re: Validation Exception

Post by CookieMonster » Fri Mar 27, 2015 3:29 pm

Hi,

I found a solution and this does exactly what I wanted to do. It only writes a log when it passed otherwise the exception Report will be called, so no equality equation is needed :) . Just add and pass Validate.Options when you call the function Validate.Attribute.

Just change code line from:
Validate.Attribute(repoInfo, attributeValue, expectedValue, "The expected value {3} from item " + repoInfo.Name + " of type: " + element.Element.PreferredCapability.DisplayName + " on attribute {1} could be validated");
To (the magic stuff is at the end):
Validate.Attribute(repoInfo, attributeValue, expectedValue, "The expected value {3} from item " + repoInfo.Name + " of type: " + element.Element.PreferredCapability.DisplayName + " on attribute {1} could be validated", new Validate.Options(true, ReportLevel.Parse("Custom;1")));

Dan

krstcs
Ranorex Guru
Posts: 2683
Joined: Tue Feb 07, 2012 4:14 pm
Location: Austin, Texas, USA

Re: Validation Exception

Post by krstcs » Fri Mar 27, 2015 3:30 pm

Ah, yeah, I always forget about the validation options. Nice!
Shortcuts usually aren't...