cancel
Showing results for 
Search instead for 
Did you mean: 

test for empty var

Lee_M
Contributor

test for empty var

Keyword tests - how can I check for an empty/null string var (doesn't exist)

 

some functions fail at a later point in my test because the string is empty

I would like to test for this conditions and if found set the varialbe to a single space

 

I have tried create an if statement to test for "" or null but is does appear to work correct and is always skipping over this test

8 REPLIES 8
AlexKaras
Community Hero

Re: test for empty var

Hi,

 

What scripting language your test project is based on?

 

Regards,
  /Alex [Community Hero]
____
[Community Heroes] are not employed by SmartBear Software but
are just volunteers who have some experience with the tools by SmartBear Software
and a desire to help others. Posts made by [Community Heroes]
may differ from the official policies of SmartBear Software and should be treated
as the own private opinion of their authors and under no circumstances as an
official answer from SmartBear Software.
The [Community Hero] signature is used with permission by SmartBear Software.
https://community.smartbear.com/t5/custom/page/page-id/hall-of-fame
================================
Lee_M
Contributor

Re: test for empty var

Alex,

 

I am using Keywords tests but project are Javascript based

 

in the KW test I have if the on-screen object Equals "" Or Equals null but this doesn't work

 

I was looking to avoid writing another code snippet but the JS code would be something like

 

if (!var) {

//do this

 

 

AlexKaras
Community Hero

Re: test for empty var

Hi,

 

I am not an expert with JavaScript, but to my understanding when a variable is assigned empty string or null value then this means that it is not empty.

You may consider aqObject.EmptyVariant or classic check like

if ('undefined' == typeof(<parameter>))

 

P.S.

if ('undefined' == typeof(<parameter>)) ' condition will not be met for null and empty string and will meet if parameter is missed at all

 

if (!var) ' this will work for null but not for empty string if I am not mistaken

I.e. the former check should be used when null is acceptable value for the parameter and the latter when it is not.

 

Regards,
  /Alex [Community Hero]
____
[Community Heroes] are not employed by SmartBear Software but
are just volunteers who have some experience with the tools by SmartBear Software
and a desire to help others. Posts made by [Community Heroes]
may differ from the official policies of SmartBear Software and should be treated
as the own private opinion of their authors and under no circumstances as an
official answer from SmartBear Software.
The [Community Hero] signature is used with permission by SmartBear Software.
https://community.smartbear.com/t5/custom/page/page-id/hall-of-fame
================================
sonya_m
Community Manager

Re: test for empty var

Alex, thank you for the help!

 

@Lee_M Was the advice helpful?


Sonya Mihaljova
Community and Education Specialist

Did my reply answer your question? Give Kudos or Accept it as a Solution to help others. ⬇️⬇️⬇️
Lee_M
Contributor

Re: test for empty var

@sonya_m this answer was helpful is so much as it confirmed what I was thinking

 

The issue I have here is there the DDT loop should be intelligent enough to expect an empty or null string input and not throw an exception, furthermore, we should be given easier ways to test for this in KW rather than resorting to code or wondering why a string vs a string is a type mismatch

 

but thanks for your input guys...

Marsha_R
Community Hero

Re: test for empty var

All the DDT loop does is go through your records.  It doesn't have any job to do other than that.  This makes it very flexible because anything else happening with the records is up to the user and they are not stuck with someone else's requirements.

 

If you'd show us your keyword test and some sample data, we might be able to help more.  We've all been in these tricky spots, but our collective knowledge is useful in getting out of them.   

 

 

Lee_M
Contributor

Re: test for empty var

Apologies, I mean WITHIN the DDT loop - a property checkpoint

 

I set b as a variable of type string and aa is the string iteration from the DDT loop

 

problem is that if aa is empty in the csv (source file) I get a type mismatch when they compare

I was expecting

b == aa

 

but got

 

b == null and the conditional statements would test for this in KW (tried testing for the object as well)

Marsha_R
Community Hero

Re: test for empty var

try comparing b to aqConvert.VarToStr(aa)

 

https://support.smartbear.com/testcomplete/docs/reference/program-objects/aqconvert/vartostr.html

If the value is a null value, VarToStr returns an empty string.

 

New Here?
Join us and watch the welcome video:
Announcements
Top Kudoed Authors