With QTP11 descriptive programming, how do I verify an element is not present?
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")) <> ""
?Concatenation in VBScript is done with a
&
sign and not with a+
, This statementBrowser("MyApp").Page("MyPage").WebElement("text:=" + DataTable.Value("ExpectedValue")).Exist
will resolve toBrowser("MyApp").Page("MyPage").WebElement(0).Exist
, leading to unexpected results. Use"text:=" & DataTable.Value("ExpectedValue")
instead.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!)
.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.
neontapir
I work as a software development manager of a distributed agile team of Java developers. I spent over a decade as a software engineer, programming primarily in C# and now Java. I have served as a board member in the office of Secretary for Agile Denver, which promotes lean and agile delivery methods across the Colorado Front Range. I am dedicated to constant improvement. As a programmer, I experiment in other languages like Ruby, Node.js, Angular.js, Scala and F#. I obtained an MBA in order to gain a deeper understanding of delivering software that's of value to businesses. When I'm not working, I enjoy board games, hiking, soccer, and tabletop roleplaying games.
Updated on June 17, 2022Comments
-
neontapir about 2 years
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?
-
neontapir over 11 yearsThank you. I will incorporate your suggestions; I agree it should lead me to a solution. (I'll accept later this morning after I have a chance to try it.) As far as #1 is concerned, it is the behavior I want. If data is absent, I want to verify that the "no results found" banner is displayed instead of the results grid.
-
AutomatedChaos over 11 yearsAh, my bad at the first point. I hope you have everything running now? If you have any other question regarding QTP (or VBScript), we have a small, but regulary active group of QTP users here on SO fighting the C# majority.
-
neontapir over 11 yearsThank you, I do have it working! I'll be certain to ask if I run into further trouble.