Page 1 of 1

Verify that content of a cell is either (null) or ""

Posted: Mon Dec 17, 2012 3:09 pm
by jdk
Hello all,

I'm looking for a way to extend a piece of user code.

A short description of the problem and solution so far:

I want to verify the contents of a cell in a table.

First attempt was without user code. That worked fine when there is something in that cell.
But... then I noticed that when the cell was empty, its value was actually (null).
So, I created the following user code (variables and repository items anonimised; sorry if I created unusable code in doing so):
Public Sub VerifyCellValue()
' If the cell is empty, we expect "", but the actual content of the cell is (null)
  If (ExpectedValue = "") Then
    Report.Log(ReportLevel.Info, "Validation", "Validating AttributeEqual (Text='(null)') on item 'Repo.CellContents'.",repo.Repo.CellContents)
    Validate.Attribute(repo.Repo.CellContents, "Text", (DirectCast(Nothing, String)))
  Else
' In all other cases, just validate if the expected and found text matches
    Report.Log(ReportLevel.Info, "Validation", "Validating AttributeEqual (Text=$ExpectedValue) on item 'Repo.CellContents'.", repo.Repo.CellContents)
    Validate.Attribute(repo.Repo.CellContents, "Text", ExpectedValue)
  End If
End Sub
No problems so far, works like a charm.

But... when I continued testing, using this code, it turns out that in SOME cases, if a cell is empty, its value is not (null), but “”. :-(

This means I need some extra code within the If/Then branch. The Else branch is OK the way it is. For the If branch, if I expect an empty cell, I am happy when the internal value is either (null) or “”.

So, within the If-branch I want to handle a check against (null); if that matches, then the test case is OK; if it fails, try a match against "". If that matches, the test is still OK. If that fails, the test should fail.

I don't know how to do this, though. The Validate doesn't return any value, it just sets the testcase to failed somewhere deep in the Ranorex framework.
I hope there is some other method than the Validate; one that helps me to achieve this.
And I hope someone can point me in the direction of that method, or give me some other ideas on how to handle this.

Re: Verify that content of a cell is either (null) or ""

Posted: Tue Dec 18, 2012 10:51 am
by Support Team
Hi,

You could use the following code:
If(Validate.Attribute(repo.Repo.CellContents, "Text", (DirectCast(Nothing, String)), "Validating AttributeEqual (Text='(null)')", false) = false)
        		''' place your code here:
End If
for more detailed information about the Validate class please take a look at the following links:
Using Validate Class
Validate Attribute.

Regards,
Markus

Re: Verify that content of a cell is either (null) or ""

Posted: Tue Dec 18, 2012 11:25 am
by jdk
Thanks for the answer.

After posting my question and a good night's sleep, I had the following solution myself:
' An empty cell contains either an empty string or null.
Public Sub VerifyCellValue()  
    ' Check if the null case applies (empty cell expected and cell contains (null)). If so, handle it
      If (ExpectedValue = "" and Attribute.Equals(repo.Repo.CellContents, (DirectCast(Nothing, String)))) Then  
        Report.Log(ReportLevel.Info, "Validation", "Validating AttributeEqual (Text='(null)') on item 'Repo.CellContents'.",repo.Repo.CellContents)  
        Validate.Attribute(repo.Repo.CellContents, "Text", (DirectCast(Nothing, String)))  
      Else
     ' In all other cases, just validate if the expected and found text matches. This includes verification of empty string against empty string
        Report.Log(ReportLevel.Info, "Validation", "Validating AttributeEqual (Text=$ExpectedValue) on item 'Repo.CellContents'.", repo.Repo.CellContents)  
        Validate.Attribute(repo.Repo.CellContents, "Text", ExpectedValue)  
      End If  
    End Sub
Seems to work OK. I'll have a look at your suggestion as well.