Quantcast
Channel: SCN : All Content - All Communities
Viewing all articles
Browse latest Browse all 8212

Automation of SAP Netweaver portal

$
0
0

'Automating the SAP netweaver portal - The Everest of Automation!

'

'There are a few articles on the web discussing the automation of the SAP Portal - some even saying its a myth.

'

'Ok it is possible using VBA..(Excel). Could be adapted for vb script.

'

'This assumes you have some knowledge of HTML DOM and general automation.

'

'

'1. Get the URL of your portal. Sometimes the portal window does not display the address bar.

' 1st try to get the URL by right clicking on IE and select properties the URL should be displayed.

' If not, this code will also get the URL of a window title containing "Portal" (depending on your env).

'Open a portal and run this function.

 

 

Debug.Print oGetPortal.LocationURL

 

Function oGetPortal( _

Optional sWindowTitle AsString = "Portal" _

) AsObject

 

 

   Set objShellApp = CreateObject("Shell.Application")

   ForEach objWindow In objShellApp.Windows

  DoEvents

  Debug.Print objWindow.locationname ' Displays window titles

   If InStr(LCase(objWindow.locationname), LCase(sWindowTitle)) > 0 Then

   Set oGetPortal = objWindow

   ExitFor

   EndIf

   Next

   Set objShellApp = Nothing

   Set objWindow = Nothing

EndFunction

 

 

'2 For automation you probably want to create a new instance of the IE portal rather than attach to

'an existing window. The Portal often runs on an intranet. Use the URL you get from oGetPortal.LocationURL

' Your must set a reference in the VBA IDE to Microsoft internet explorer controls (VBA IDE -> Tools -> references

' For intranet portals InternetExplorerMedium is needed - if this does not work see oGetLatestPortal following


Function oOpenPortal( _

  Optional sURL As String = "http://myportalpath/portal") As Object

' set a reference to Microsoft internet explorer controls

   Dim IE As InternetExplorerMedium

   Set IE = New InternetExplorerMedium

 

  IE.Navigate sURL

 

   Set oOpenPortal = IE

End Function




' Skip this section if the above works

' In windows 7 the IE object automatically detaches from intranet pages (see above code fixes this issue) 

'So the below code works around this issue & should work on earlier windows versions and non intranet pages

'it also opens up the portal in an IE window with an address bar and is easier to get access to the IE developer tools.

 

 

Debug.Print oGetLatestPortal.LocationURL

 

PrivateDeclareSub Sleep Lib"kernel32" (ByVal dwMilliseconds AsLong)

PrivateDeclareFunction GetForegroundWindow Lib"user32" () AsLong

Function oGetLatestPortal( _

  Optional sURL AsString = "http://myportalpath/portal#", _

  Optional sWindowTitle AsString = "Home - SAP NetWeaver Portal") AsObject

 

   Dim IE As InternetExplorer

   Set IE = New InternetExplorer

 

  IE.Navigate sURL

   Dim hwnd AsLong

  hwnd = GetForegroundWindow() ' get latest IE window

   Set IE = Nothing  ' work around for windows 7 issue

 

  i = 0

   DoWhile i < 10And IE IsNothing

 

  i = i + 1

   Set objShellApp = CreateObject("Shell.Application")

   ForEach objWindow In objShellApp.Windows

  DoEvents

   If LCase(objWindow.LocationName) = LCase(sWindowTitle) Then

       If objWindow.hwnd = hwnd Then'active IE window if more than one with same title

                Set IE = objWindow

       EndIf

     EndIf

   Next

  DoEvents

  Sleep 100

   Loop

 

 

   If IE IsNothingThen

  MsgBox "nothing"

ExitFunction

  End If

 

   SetoGetLatestPortal = IE

 

EndFunction

 

'3. Wait for IE page to load. You cannot do any automation until after

'the page is loaded. This function returns true if the page or frames top level is loaded (frames discussed later)

 

Debug.Print lWait(oGetLatestPortal)

 

Function lWait(LOIE AsObject) AsBoolean

  Dim t1 AsLong

  Dim t2 AsLong

  OnErrorResumeNext

  t1 = Timer

  t2 = t1 + 60

  Do

   If Timer > t2 Then lWait = False: ExitFunction

  If VarType(LOIE.Document) = vbString ThenExitDo

  Loop

  Do

   If Timer > t2 Then lWait = False: ExitFunction

  If LOIE.Busy = FalseThenExitDo

  Loop

  Do

   If Timer > t2 Then lWait = False: ExitFunction

  If VarType(LOIE.Document.readyState) = "C"ThenExitDo

  Loop

  Do

   If Timer > t2 Then lWait = False: ExitFunction

  If LOIE.Document.readyState = "complete"ThenExitDo

  Loop

  lWait = True

EndFunction

 

 

'4. Find what you wat to click on by using developer tools in IE8+ (F12) with the window you just opened using the above code.

'Use the IE inspect tool (arrow) to click on the field or link/field/button you want to automate. This will

'expand the HTML DOM and show the HTML tag corresponding to the field. If you can see the tag you can automate

'it! But it can be tricky.

'

'5. Clicking on a link. Now that you have open a portal window you want to do something usually click on a link.

'Using the developer tools in IE. Find the link you want. If the link you want is inside a HTML form or frame

'tag see further on.

 

 

lWait IE

Set link = IE.Document.getElementById("MyLink_1_1") ' Can be a div or anchor

link.focus

link.click

 

' Waiting for elements to appear.

''

' Even though you call lWait and IE is ready, the portal may be doing things. eg.

' displaying spinners (etc) '

 

' ' Wait for spinner  - if a spinner appears you need to wait until it goes (if not skip this) . The spinner is also known as 'wheel of death' by end users. 

' The parameter you pass will usually be the document of the frame see frames (below). Make sure the spinner id is  "ur-loading"

''Again use IE developer tools to find the spinner it can be a little tricky to do. Tip : open IE developer tools F12 and view something that is

'slow like the change history then click on the spinner using the developer tools arrow. You may have to move your mouse around a bit and highlight it.

 

Function WaitForSpinner(iedoc AsObject) AsBoolean

' note, IEdoc can be IE.Document, SubFrame.contentWindow.Document or SubForm.Document (discussed later)

  Dim ctrl AsObject

  Do

Set ctrl = iedoc.getElementById("ur-loading")

if ctrl is nothing ThenExitFuntion

If ctrl.getAttribute("style").display = "none" ThenExitDo

sleep 500

  Loop

EndFunction

'

.

' Sometimes you must wait for the element to appear

'Note elements can be forms, frames, links or fields or anything.

' safer way to get a element that will wait until it appears

Dim element AsObject

Dim t1 AsLong

Dim t2 AsLong

 

t1 = Timer

t2 = t1 + 10' secs

 

While element IsNothing

  DoEvents

   If Timer > t2 Then

  MsgBox "timeout"

   ExitDo

   EndIf

' note, IEdoc can be IE.Document, SubFrame.contentWindow.Document or SubForm.Document (discussed later)

 

   Set element = IEdoc.getElementById("elementid")

 

Wend

 

'If you still have problems try to get an element preceeding the element you want

'Use the VB debugger and also put sleeps in and use while loops as shown also you must get any

' forms or frames preceeding the element.

'

'

'

'6. Elements in Forms or Frames

'If you still can't get the element by elementbyid or by looping through getElementsByTagName,

'It could be because the field you want is in a form or frame.

'You should get a reference to any forms or frame preceeding the element first. Use F12 developer tools

'to find out if there are any precedding forms or frames.

 

 

'6 a.HTML frames

'Get an obj reference to a html frame

 

Dim subframe1 AsObject

' Assuming you have any preceeding Forms or Frames

Set subframe1 = IE.Document.getElementById("frameid") ' example

 

lWait subframe1.contentWindow

 

'Once you get a obj reference to a frame you must get the sub document

'before you can do any further searches

 

Dim myframedocument AsObject

Set myframedocument = subframe1.contentWindow.Document

 

Set subframe2 = myframedocument.getElementById("frameid2") ' example

 

'

'6b HTML forms

'

'A HTML Form is similar, again you must get any preceeding Forms or Frames

Dim subform AsObject

Set subform = IE.Document.getElementById("formid") ' example

 

Dim myformdocument AsObject

Set myformdocument = subform.Document

 

Set mylink = myformdocument.getElementById("mylink") ' example

 

'6c info popups'

' Sometimes a message box appears - eg like 'Changes have been made do you want to save?'

' Which requires a 'button' to be pressed. These message boxes aren't javascript alerts they are Html based which means

' we can automate them! Once the popup appears again hit IE8+ F12  (developer tools) and move you mouse over the popup

' and select a field item to get the element id. Note, you might have try a few times as can be hard for developer tools to highlight popup items.

' Once you select an item on the popup you will notice in the DOM in developer tools that the popup is actually an iframe, get the frame's id..

 

Dim subframe1 AsObject

Set subframe1 = IE.Document.getElementById("frameid") ' id of 'popup'

lWait subframe1.contentWindow

 

' Next get document DOM of iframe

Dim myframedocument AsObject

Set myframedocument = subframe1.contentWindow.Document

 

' Next press a button - popup buttons are anchors.

''To get the button id you might have to use developer tools , to dig through the DOM if you can't select it with a mouse.

' Buttons will be and anchor at the botton of the iframe inside a div with footer in the id.

'If you still can't get the element by elementbyid try using getElementsByTagName,

 

Set link = myframedocument.getElementById("anchor id") ' example

link.focus

link.click

 

'7.Dynamic Content. In the portal HTML elements id's can change depending on the context ie whether certain

'sections are data driven or due to different user's role, certain elements are displayed. In the previous

'example Link_1_1 might be a different menu option on one users login to another. So in these cases

'you need to write more generic code. 2 examples.

'

' using a tags attributes

For k1 = 0To IE.Document.links.Length - 1

   If IE.Document.links.Item(k1).getAttribute("title") = "Show Attachments"Then

 

 

IE.Document.links.Item(k1).Click

ExitFor

 

   EndIf

 

Next k1

 

 

' Loop through a table looking for a link's screen text

Set oTbl = IE.Document.getElementById("myNavigationTable")

Set oTBody = oTbl.getElementsByTagName("TBODY").Item(0)

Set oTRow = oTBody.getElementsByTagName("TR").Item(0)

Set oTds = oTRow.getElementsByTagName("TD")

 

ForEach oTd In oTds

  DoEvents

   If InStr(oTd.innertext, "Link Text") > 0Then

   Set link = oTd.getElementsByTagName("A").Item(0)

   ExitFor

   EndIf

 

Next oTd

link.Click

 

 

 

'8.If you hit a back button element on a page you might have to

're-establish VB objects in your code as sometimes they lose reference.

'

'9.Finally this is not a turn key solution - ideally a function could be written to find a control

'by searching the DOM tree handling all frames and forms and waits

'

'I hope this post helps


Viewing all articles
Browse latest Browse all 8212

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>