automating ie using vb6. agenda automating ie html dom automating web pages question and answers

Download Automating IE using VB6. Agenda Automating IE HTML DOM Automating Web Pages Question and Answers

Post on 18-Jan-2016

215 views

Category:

Documents

0 download

Embed Size (px)

TRANSCRIPT

  • Automating IE using VB6

  • AgendaAutomating IEHTML DOMAutomating Web PagesQuestion and Answers

  • Automating IE

  • COMCOM is Component Object Model.Application developed using COM provides interfaces to third party application for using certain features of the application.Nearly all Microsoft applications are built using COM. Eg. Internet Explorer, All Office applications like Outlook, Excel, Word etcCOM makes it possible to create automation solution for the AUT.

  • Using COM object in VBThere are 2 ways to create a COM object in VBBy referring the ActiveX library in the projectBy creating an object using the ProgID of COM application. By referring the ActiveX library in the projectDim IE as InternetExplorer.ApplicationSet IE = New InternetExplorer.ApplicationIE.Visible = TrueIE.Quit Set IE=NothingBy creating an object using the ProgID of COM application.Dim IE as ObjectSet IE = CreateObject(InternetExplorer.Application)IE.Visible = TrueIE.Quit Set IE=Nothing

  • Adding reference to IE in VBGo to Project->References and check the checkbox for Microsoft Internet Controls and Click OK. Now all the libraries of IE would be available in the project.

  • Viewing object methods/properties using Object Browser

  • Various Method available with IE

    Method NameDescriptionNavigate2Navigates to a specified URL. URL could be any http, https, ftp link or a local/network folder location.StopAborts the navigation.RefreshRefresh the present URLQuitCloses the browser. Closing a browser while its navigating to some URL is not recommended.GoBackNavigates one page backGoHomeNavigates to home pageGoForwardNavigates one page forwardGoSearchOpen the search page

  • Various properties available with IE

    Property NameTypeDescriptionAddressBarBooleanControls whether address bar is shownFullScreenBooleanMaximizes window and turns off statusbar, toolbar, menubar, and titlebar.LocationNameStringGets the short (UI-friendly) name of the URL/file currently viewed.LocationURLStringGets the full URL/path currently viewed.PathStringReturns the path to the application.ResizableBooleanControls whether the window is resizableSilentBooleanControls if any dialog boxes can be shownTypeStringReturns the type of the contained document object.

  • Various properties available with IE contd

    Property NameTypeDescriptionVisibleBooleanDetermines whether the application is visible or hidden.BusyBooleanQuery to see if something is still in progress.ReadyStateLongCan have any of the following valuesREADYSTATE_UNINITIALIZED: The default initialization state.READYSTATE_LOADING: The object is currently loading its properties.READYSTATE_LOADED: The object has been initialized.READYSTATE_INTERACTIVE: The object is interactive, but not all of its data is available.READYSTATE_COMPLETE: The object has received all of its data.

  • Browsing to Website and Waiting for Page to LoadDim IE Set IE = CreateObject(InternetExplorer.Application)IE.Visible = TrueIE.Navigate2 http://www.yahoo.comDoDoEventsLoop while IE.Busy = TRUEMsgbox Page Loaded. Press OK to close the opened IEIE.Quit Set IE=Nothing

  • Enumerating all open IE Method 1

    Dim IEWindows As SHDocVw.ShellWindowsSet IEWindows = New SHDocVw.ShellWindowsDim IEWindowFor Each IEWindow In IEWindowsDebug.Print "Name: " & IEWindow.NameDebug.Print "Visible: " & IEWindow.VisibleDebug.Print "FileName: " & IEWindow.FullNameDebug.Print "LocationURL: " & IEWindow.LocationURLDebug.Print "LocationName: " & IEWindow.LocationNameNext For i = 0 To IEWindows.Count - 1 Set IEWindow = IEWindows.Item(i)Next

  • Enumerating all open IE Method 2Dim objShellDim objShellWindowsSet objShell = CreateObject("Shell.Application")Set objShellWindows = objShell.Windows

    If (Not objShellWindows Is Nothing) Then Dim objEnumItems For Each objEnumItems In objShellWindows MsgBox objEnumItems.LocationURL NextEnd If

    Set objShellWindows = NothingSet objShell = Nothing

  • Closing all open IEDim IEWindows As SHDocVw.ShellWindows Set IEWindows = New SHDocVw.ShellWindows Dim IEWindow()ReDim IEWindow(1 To IEWindows.Count) Dim iFor i = 1 To IEWindows.Count Set IEWindow(i) = IEWindows.Item(i - 1)Next

    For i = LBound(IEWindow) To UBound(IEWindow)If InStr(1, IEWindow(i).FullName, "iexplore.exe", vbTextCompare) Then IEWindow(i).Quit End If Set IEWindow(i) = Nothing Next

  • Getting reference to already open IEPublic Function GetOpenIE(ByVal strUrl As String) As InternetExplorerDim shie As InternetExplorerDim sh As New ShellWindowsFor Each shie In shIf shie.LocationURL Like strUrl ThenSet GetOpenIE = shieExit FunctionEnd IfNextSet GetOpenIE = NothingEnd Function

  • Events in IEEvent is nothing but a change in system stateWe can capture Events in IE using the below code Private WithEvents IE As InternetExplorerAfter adding the above line the IE variable will appear in the controls combo box and all its events appear in the Events combo box

  • HTML DOM

  • DOMThe HTML DOM is the Document Object Model for HTMLThe HTML DOM defines a standard set of objects for HTML, and a standard way to access and manipulate HTML documentsThe HTML DOM is platform and language independent The HTML DOM views HTML documents as a tree structure of elements. All elements, along with their text and attributes, can be accessed and manipulated through the DOM tree

  • DOM TreeEvery TAG in the HTML source represent a node in the DOM treeOnce a TAG is opened, all the tags following it become child nodes of the starting nodeEach TAG can have various attributes. Some are predefined and some are user-defined attributes Eg -
  • DOM Tree Contd

    function ChangeColor(){document.body.bgColor="yellow"}Click on this document!

  • DocumentDocument object represents the whole documentIt is the top node in the DOM TreeDocument node doesnt have any sibling nodesIt provides various collections for Links, Anchors, Scripts, Images etc... in the DocumentIt also provides various functions using which we can access an element using the name of the element

  • Element & Element CollectionElement is an object referring to any particular node in the DOMDepending on the type of node the element refers to, it would give access to methods and properties related to those type of elementsEvery element has the properties outerText, outerHtml, innerText, innerHtml, tagName etc

    Element Collection is a collection of one or more elements. For Eg- Now see the following VBScript code set txt_Boxes=document.getElementsByName(txt_Name) for i=0 to txt_Boxes.Length - 1 txt_Boxes.item(i).value=Tarun txt_Boxes(i).value=Tarun next

  • Collections available in a Document

    Collection NameDescriptionLinksGives collections of all link elements present in the documentAnchorGives collections of all link elements present in the document which have a non blank id or name.AppletsGives collections of all applet elements present in the document.AllGives collections of all the elements present in the documentScriptsGives collections of all the script elements present in the documentstyleSheetsGives collections of all the stylesheet elements present in the documentimagesGives collections of all the image elements present in the document

  • How to get an element from the web page

    Various ways to get this element are

    Mainly used at time of IE4. Are compatible with higher versions but not recommendedSet txt_Elem=Document.All(firstname)Set txt_Elem=Document.All(txt_Name)

    Used with IE 5.0Set txt_Elem=Document.getElementsById(firstname)

    How to check if the elements is present or notIf txt_Elem is Nothing thenMsgbox Element Is not present

  • How to get an element from the web page contd

    Set txt_Elem=Document.getElementsByTagName(INPUT).item(0)Set txt_Elem=Document.getElementsByTagName(INPUT).item(txt_Name)Set txt_Elem=Document.getElementsByTagName(INPUT).item(firstname)

    Set txt_Elem=Document.getElementsByName(txt_Name).item(0)

    The above 2 line would throw error if there is no element having INPUT tag or havingname as txt_Name. To avoid this we can first check the length of the collection -

    If Document.getElementsByName(txt_Name).length0 thenSet txt_Elem=Document.getElementsByName(txt_Name).item(0)End if

  • Various HTML Elements

  • LINK or BUTTONMicrosoft

    Various ways to click on this link or button:document.Links(mslinkid).clickdocument.Links(mssoft).clickdocument.getElementById(mslinkid).clickdocument.getElementsByName(mssoft)(0).clickdocument.all(mslinkid).clickdocument.all(mssoft).click

  • Text Box

    Various ways of changing the value of the text box:document.getElementById(firstname).value=Tarundocument.getElementsByName(name)(0).value=Tarun (If name and id is not available then use the below example code to change value)Set allElems= document.getElementsByTagName(INPUT)For each elem in allElems if elem.myprop=test thenelem.value=TarunExit Forend ifNext

  • Combo box or List BoxCombo box and list box have an array of options that a user can select.Displayed Value 1Value 4

    Set objCombo=document.getElementsByName(demo_ComboBox).item(0)

    numOptions=objCombo.Options.length Would give 3 in our casefirstOptionValue=objCombo.Options(0).value Actual Value 1 in our casefirstOptionText=objCombo.Options(0).text Displayed Value 1 in our caseTo select one of the options use the below codeobjCombo.Options(0).Selected = trueobjCombo.value=Actual Value 1

  • CheckboxA checkbox can be either checked or unchecked

    Set objChkBox=document.getElementsByName(demo_CheckBox).it