This is a migrated thread and some comments may be shown as answers.

VB Assert() - I am using this correctly in an IF statement

2 Answers 90 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Nigel
Top achievements
Rank 2
Nigel asked on 31 May 2012, 10:26 AM
Hi,

I am having trouble understanding how Assert() works.  Essentially, the below VB subroutine attempts to find out if an HTML table is displaying a particular table cell after entering a search string into an interactive search field (the search occurs automatically without needing to press a [Go] button).  If the table cell is not present then the routine presses a screen button [Add].  If it is present then the routine clears the search field ready for a new search.

Public Sub sa_SetupLookups_SetLookupTextItems()
    Dim row As Integer
    Dim col As Integer
    Dim minCol As Integer = 3   'First column for lookupText values, column C
    Dim maxCol As Integer = 11  'Last column for lookupText values, column K
    Dim input As String = "C:\Users\edwardni\Documents\Test Studio Projects\Accelerate\Data\AccelerateData.xlsx"
    Dim app As New Microsoft.Office.Interop.Excel.Application()
    'Dim inputBook As Microsoft.Office.Interop.Excel.Workbook = app.Workbooks.Open(input, 0, False, 5, "", "", False, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "", True, False, 0, True, False, False) 'This is write
                                                                                         'Object UpdateLinks, Object ReadOnly, Object Format, Object Password, Object WriteResPassword, Object IgnoreReadOnlyRecommended, Object Origin, Object Delimiter, Object Editable, Object Notify, Object Converter, Object AddToMru, Object Local, Object CorruptLoad)
    Dim inputBook As Microsoft.Office.Interop.Excel.Workbook = app.Workbooks.Open(input, 0, True, 5, "", "", False, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "", True, False, 0, True, False, False)    'This is read-only
    Dim inputSheet As Microsoft.Office.Interop.Excel.Worksheet = DirectCast((inputBook.Worksheets.Item("Lookups")), Microsoft.Office.Interop.Excel.Worksheet)
    Dim theLookupCode As Object = GetExtractedValue("CodeText")
    Dim lookupCode As String = theLookupCode.ToString().Trim()
    Dim lookupText As String
    Dim NoMatchingTableCell As HtmlTableCell = Pages.LookupEdit.NoMatchingTableCell
 
    Select Case lookupCode
        Case "City"
            row = 2
        Case "CompanyType"
            row = 3
        Case "ContactType"
            row = 4
        Case "DataSourceType"
            row = 5
        Case "DataType"
            row = 6
        Case "IndustryType"
            row = 7
        Case "SalutationType"
            row = 8
        Case "Screen"
            row = 9
        Case "Size"
            row = 10
        Case "CategoryType"
            row = 11
        Case "PriorityType"
            row = 12
        Case "ProjectClientsType"
            row = 13
        Case "SeverityType"
            row = 14
        Case "StatusType"
            row = 15
        Case Else
            row = 999
    End Select
 
    If (row <> 999) Then
        For col = minCol to maxCol
            lookupText = TryCast(DirectCast(inputSheet.Cells(row, col), Microsoft.Office.Interop.Excel.Range).Text, String)
             
            Log.WriteLine("DEBUG LINE theLookupCode: " + theLookupCode.ToString())
            Log.WriteLine("DEBUG LINE lookupCode: " + lookupCode.ToString())
            Log.WriteLine("DEBUG LINE row: " + row.ToString())
            Log.WriteLine("DEBUG LINE col: " + col.ToString())
            Log.WriteLine("DEBUG LINE lookupText: " + lookupText.ToString())
             
            If (lookupText = ".") Then
                Exit For
            Else
                'Search table to see if lookupText already exists
                Pages.LookupEdit.Text.ScrollToVisible(ArtOfTest.WebAii.Core.ScrollToVisibleType.ElementTopAtWindowTop)
                ActiveBrowser.Window.SetFocus
                Pages.LookupEdit.Text.MouseClick
                Manager.Desktop.KeyBoard.TypeText("", 50, 100)  'Clear search field of any pre-existing text
                System.Threading.Thread.Sleep(1000) 'Give table time to repopulate
                Pages.LookupEdit.Text.MouseClick
                Manager.Desktop.KeyBoard.TypeText(lookupText, 50, 100)   'Enter value held in lookupText (e.g. London) in 'Text'
                System.Threading.Thread.Sleep(1000) 'Give table time to complete the search
                 
                NoMatchingTableCell.Wait.ForExists(10000)
 
                If (Assert.IsTrue(NoMatchingTableCell.IsVisible) = True) Then
                    'lookupText already exists, so empty the search field ready for the next
                    Pages.LookupEdit.Text.ScrollToVisible(ArtOfTest.WebAii.Core.ScrollToVisibleType.ElementTopAtWindowTop)
                    ActiveBrowser.Window.SetFocus
                    Pages.LookupEdit.Text.MouseClick
                    Manager.Desktop.KeyBoard.TypeText("", 50, 100)
                    System.Threading.Thread.Sleep(1000)
                Else
                    Pages.LookupEdit.AddSpan.Click(false)
                    Pages.AddNewLookupItem.HtmlTag.BaseElement.Wait.ForCondition(Function(a_0, a_1) ArtOfTest.Common.CompareUtils.StringCompare(a_0.InnerText, "Add New Lookup Item", ArtOfTest.Common.StringCompareType.StartsWith), false, Nothing, 10000)
                    Pages.AddNewLookupItem.CultureBasedTextTextText.ScrollToVisible(ArtOfTest.WebAii.Core.ScrollToVisibleType.ElementTopAtWindowTop)
                    ActiveBrowser.Window.SetFocus
                    Pages.AddNewLookupItem.CultureBasedTextTextText.MouseClick
                    Manager.Desktop.KeyBoard.TypeText(lookupText, 50, 100)   'Add the new lookupText
                    Pages.AddNewLookupItem.ItemIsActiveCheckBox.Check(true, true)   'Check 'ItemIsActiveCheckBox' to be 'True'
                    Pages.AddNewLookupItem.Submit.Click(false)  'Click 'Submit' (Save button)
                End If
            End If
        Next col
    End If
    'All required text entries for the supplied lookupCode have been made, so exit back to the main Lookups screen
    Pages.LookupEdit.BackToListSpan.Click(false)
     
    'Free up resources:
    row = Nothing
    col = Nothing
    minCol = Nothing
    maxCol = Nothing
    input = Nothing
    app = Nothing
    inputBook = Nothing
    inputSheet = Nothing
    lookupText = Nothing
    theLookupCode = Nothing
    lookupCode = Nothing
    NoMatchingTableCell = Nothing
 
    'Close Excel, preventing any Save prompt, & free up the process
    inputBook.Saved = True
    app.Quit()
    app = Nothing
End Sub

When I execute this it fails on the first loop through, on the line:
          If (Assert.IsTrue(NoMatchingTableCell.IsVisible) = True) Then

Clearly I have not understood how the Assert statement works.  Please would someone help by reviewing the above VB subroutine and advise where I have gone wrong, or suggest a better alternative.

Many thanks,
Nigel Edwards, Transition Computing. 

2 Answers, 1 is accepted

Sort by
0
Accepted
Plamen
Telerik team
answered on 04 Jun 2012, 09:21 AM
Hi Nigel,

It's not allowed to use a method from the Assert class into an "If" statement, because the methods contained in that class don't return a value. Here's how the IsTrue method looks like in VB:
Public Shared Sub IsTrue ( _
    condition As Boolean _
)


In order to make this to work, you can change your code like this: 
If NoMatchingTableCell.IsVisible() Then
    'Do something...
    Else
    'Do something else...
         
End If
Hope this helps!

Greetings,
Plamen
the Telerik team
Quickly become an expert in Test Studio, check out our new training sessions!
Test Studio Trainings
0
Nigel
Top achievements
Rank 2
answered on 06 Jun 2012, 11:20 AM
Okay, Plamen, many thanks for you helpful response.  No I understand how it works I can make appropriate changes.   :)
Tags
General Discussions
Asked by
Nigel
Top achievements
Rank 2
Answers by
Plamen
Telerik team
Nigel
Top achievements
Rank 2
Share this question
or