Validate input is (null) or ""

Ask general questions here.
jbg
Posts: 2
Joined: Wed Sep 14, 2016 5:25 pm

Validate input is (null) or ""

Post by jbg » Fri Sep 16, 2016 11:44 pm

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
Last edited by jbg on Mon Sep 19, 2016 6:07 pm, edited 3 times in total.

User avatar
RobinHood42
Posts: 239
Joined: Fri Jan 09, 2015 3:24 pm

Re: Validate input is (null) or ""

Post by RobinHood42 » Mon Sep 19, 2016 12:44 pm

Hi jbg,

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

Cheers,
Robin

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

Re: Validate input is (null) or ""

Post by krstcs » Mon Sep 19, 2016 1:45 pm

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
Shortcuts usually aren't...

jbg
Posts: 2
Joined: Wed Sep 14, 2016 5:25 pm

Re: Validate input is (null) or ""

Post by jbg » Mon Sep 19, 2016 5:57 pm

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
I don't want failures in my validation
Ranorex Null failure 9-19-2016 9-54-33 AM.png (81.74 KiB) Viewed 1434 times

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

Re: Validate input is (null) or ""

Post by krstcs » Mon Sep 19, 2016 6:41 pm

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 ""
Shortcuts usually aren't...