With QTP11 descriptive programming, how to verify an element is not present

qtp

I am a QTP neophyte.

I'm able to write statements like this, using the Object Repository:

If Trim(DataTable.Value("ExpectedValue")) = "" _
  And Not Browser("MyApp").Page("MyPage").WebElement("MissingDataBanner").Exist  Then
    Reporter.ReportEvent micFail, "MissingDataBanner", "Element expected"
End If

While I can use descriptive programming to check properties of elements that do exist…

If Not Browser("MyApp").Page("MyPage").WebElement("text:=" + DataTable.Value("ExpectedValue")).Exist Then
  Reporter.ReportEvent micFail, "My Data Field", "Element does not contain expected value"
End If

when I try something like this:

If Trim(DataTable.Value("ExpectedValue")) = "" _
  And Not Browser("MyApp").Page("MyPage").WebElement("text:=" + DataTable.Value("ExpectedValue")).Exist  Then
    Reporter.ReportEvent micFail, "MissingDataBanner", "Element expected"
End If

of course QTP can't find the web element, and the test errors trying to access the Exist method.

Is there a way to use descriptive programming to check that an element does NOT exist on the page?

Best Answer

  1. take a look at the first if statement and particulary this line: If Trim(DataTable.Value("ExpectedValue")) = "". You are saying 'If ExpectedValue is empty And the object does not exist, then report an error' Is that really what you want or do you want to test the ExpectedValue on 'not empty': If Trim(DataTable.Value("ExpectedValue")) <> ""?

  2. Concatenation in VBScript is done with a & sign and not with a +, This statement Browser("MyApp").Page("MyPage").WebElement("text:=" + DataTable.Value("ExpectedValue")).Exist will resolve to Browser("MyApp").Page("MyPage").WebElement(0).Exist, leading to unexpected results. Use "text:=" & DataTable.Value("ExpectedValue") instead.

  3. QTP has some quirks, testing on .exist property on objects is one of them (I am speaking for QTP10 here). It sounds strange, but in some cases (unfortunately I can not remember which once, if I have a spare minut I'll try to reproduce it), .exist returns a False that is not recognized natively as False in a conditional statement. The best way to test if an object does not exist is explicitly test if the exist property equals false: If Browser("foo").Page("bar").WebElement("xizzy").exist = False then print "Object does not exist!).

  4. tip: To speed up your test, you can use the exist with a timer, if you use .exist(0) it will test the existence of the object immediately with no use of the synchronization timers you have set in your testsettings.

Maybe this is not directly a solution to your exact problem, but it gives more reliable results and will eventually lead you to the solution. Testing if an object does not exist with the .Exist method is the correct way to do this. When you are getting errors there is something else wrong. For example the browser or the page does not exist.

Related Topic