locked
Which Method Is Better? RRS feed

  • Question

  • Hi,
       I have found 2 methods for completing the same automation task, my question is, which method is the best way of completing the automation task? They both work just fine, its just that there appears to be multiple ways of completing tasks, how do we know which way is correct?

    Thanks,
    Confused newbie

    First Method:

    Code Snippet

    Dim theElementCollection1 As HtmlElementCollection = WebBrowser1.Document.GetElementsByTagName("input")
            For Each curElement As HtmlElement In theElementCollection1
                Dim controlName As String = curElement.GetAttribute("name").ToString
                If controlName = "Email" Then
                    curElement.SetAttribute("Value", "username")
                ElseIf controlName = "Passwd" Then
                    curElement.SetAttribute("Value", "password")
                End If
            Next
            Dim theElementCollection2 As HtmlElementCollection = WebBrowser1.Document.GetElementsByTagName("input")
            For Each curElement As HtmlElement In theElementCollection2
                If curElement.GetAttribute("name").Equals("signIn") Then
                    curElement.InvokeMember("click")
                End If
            Next


    Second Method:

    Code Snippet

    Me.WebBrowser1.Document.GetElementById("Email").Focus()
            Me.WebBrowser1.Document.GetElementById("Email").InnerText = ("username")          
            Me.WebBrowser1.Document.GetElementById("Passwd").Focus()
            Me.WebBrowser1.Document.GetElementById("Passwd").InnerText =  ("password")          
            Me.WebBrowser1.Document.GetElementById("signIn").RaiseEvent("onClick")
            SendKeys.Send("{ENTER}")


    Saturday, July 5, 2008 12:40 PM

Answers

  • The second method has the advantage of throwing an exception if the page doesn't contain the elements you depend on.  The first method will just fail silently.
    Saturday, July 5, 2008 3:02 PM
    Moderator

All replies

  • The second method has the advantage of throwing an exception if the page doesn't contain the elements you depend on.  The first method will just fail silently.
    Saturday, July 5, 2008 3:02 PM
    Moderator
  • Thanks nobugz,

    How would I use the second method in a loop like the first?


    Saturday, July 5, 2008 3:35 PM
  • You don't need a loop.
    Saturday, July 5, 2008 5:00 PM
    Moderator
  • sorry forgot to say that the login process is the first of about ten steps that the application completes so the code needs to be able to proceed to next step.


    Saturday, July 5, 2008 7:02 PM
  • Hi V7web,

    First you need to add the logic to judge whether the html element exists in the web page.  If it doesn't exist, popup the messagebox to notify the user. What next step is your decision here? Exit or make the user to select the new login page.

    Second if it exists, you need to parse the web page again to know whether it logins successfully.

    Best regards,
    Riquel

    Wednesday, July 9, 2008 12:42 PM
    Moderator