Page 1 of 1

Validate input is (null) or ""

Posted: Fri Sep 16, 2016 11:44 pm
by jbg
I have looked at many old posts regarding null values and dealing with them in different ways. none of them have helped me.

One of the inputs I have has a value field, the other doesn't, so I am getting null for one and "" for the other. I want to: (I don't want to have failures in my test cases.)
  • validate that the value is null
    validate that the value is an empty string.
I'm trying to create one user code method that will validate that actual text I am looking for and if I am looking for an empty input I would be looking for "" which is ok with me if it has either null or "".

How can I do this?

This is my current code:
public void ValidateTextboxAndText(Ranorex.Adapter repoItem, String text, Boolean disabled = false)
{
	if (text == "") {
		if (repoItem.Element.GetAttributeValueText("Text") == null) {
			Validate.Attribute(repoItem, "Value", ((string)null), "Validating AttributeEqual (Text='(null)') Failure of this is ok", false);
		} else {
			Validate.Attribute(repoItem, "Value", "", "Validating AttributeEqual (Text='') Failure of this is ok", false);
		}
	} else {
		Validate.Attribute(repoItem, "Value", text);
	}
	
	Validate.Attribute(repoItem, "Disabled", disabled.ToString());
    Validate.Attribute(repoItem, "Hidden", "False");
    Validate.Attribute(repoItem, "readonly", BoolToReadOnly(disabled));
}
Thanks,
James

Re: Validate input is (null) or ""

Posted: Mon Sep 19, 2016 12:44 pm
by RobinHood42
Hi jbg,

You could use the following:
if(!String.IsNullOrEmpty(text))
8)

Cheers,
Robin

Re: Validate input is (null) or ""

Posted: Mon Sep 19, 2016 1:45 pm
by krstcs
Robin's method is the preferred method for checking if a string is null or empty in .NET.

I'd mod up if I could! :D

Re: Validate input is (null) or ""

Posted: Mon Sep 19, 2016 5:57 pm
by jbg
I may be missing something, but I want to validate that the value is null or validate that the value is an empty string. I don't want to have failures in my test cases. (I should have included that in my original question. which I just did)
Ranorex Null failure 9-19-2016 9-54-33 AM.png

Re: Validate input is (null) or ""

Posted: Mon Sep 19, 2016 6:41 pm
by krstcs
In usercode do one of these:

Code: Select all

Validate.IsTrue(String.IsNullOrEmpty(myString)); //Validates that the string IS null or ""
Validate.IsTrue(!String.IsNullOrEmpty(myString)); //Validates that the string IS NOT null or ""