Skip to Content
0

How to get the coordinates of selected field, at runtime

Jan 18, 2017 at 10:34 PM

822

avatar image

Hi,

I am trying to automate the SAP testing in my organization.
My objective is to download the PSA table details to aspreadsheet.
I have created a code that carries out this task, with the recorded scripts.

However, to make the code generic, the script should read the table name, and download its data.

I have a piece of code in my reocrded script, that records the sap screen coordinates.

session.findById(“wnd[0]/shellcont[1]/shell/shellcont[0]/shell/shellcont[1]/shell/shellcont[1]/shell”).selectItem ” 709″, “COL1”
This is a static entry. Is it possible to get the position of selected field at runtime.

Any help will be much appreciated.

Thanks,
Annie

10 |10000 characters needed characters left characters exceeded

Hi Annie,

Welcome to the SAP Community Network.

You might want to add some additional secondary tags to your question, as there are some related to various test automation tools. As I'm not sure which tools you're using, I can't preselect them for you. You might hunt for something with "test" as a keyword, or have a look through this spreadsheet of all available tags: http://sapassets.edgesuite.net/tools/SAP-Community-Tags.xlsx

Cheers,
Matt

0

Hi Matt,

I am developing a macro code that connects to SAP GUI and executes the recorded scripts.

I couldn't find any related tags from the available ones.

Not able to add user defined tags, as it is throwing me an error as' Error occured'.

If possible, could you please add the tags following tags from your end: 'SAP GUI scripting', 'vbscript', macro

Thanks,

Annie

0

Normally you shouldn't receive an error editing your own question, but sometimes the site hiccups. Anyway, I've added "UI SAP GUI for Windows" as a secondary tag, and "vbscript" and "macro" as user tags (they don't exist as managed tags, nor is there one that I could find for SAP GUI scripting, unfortunately).

It does appear you are getting some good responses, though.

0

Please find the code used below.

Private Sub btnlogin_Click()
'-----------------------Declarations-----------------------'
Dim SAP As Object
Dim conn As Object
Dim SapGuiAuto As Object
Dim SAPguiApp As SAPFEWSELib.GuiApplication
Dim session As Object
Dim Wscript As Object
Dim ws1 As Worksheet
'---------------------------------------------------------'
Set ws1 = Sheets(1)

'-------------Connection settings-------------------------'
Set SAP = CreateObject("SAP.functions")
SAP.connection.RfcWithDialog = True
Set conn = SAP.connection
conn.System = "SYSTEM"
conn.client = "100"
conn.user = ws1.Cells(5, 10).Value
conn.Password = ws1.Cells(7, 10).Value
conn.Language = "EN"
'---------------------------------------------------------'
If conn.logon(0, False) <> True Then
    MsgBox ("Logon not possible")


Else
    'If Not IsObject(SAPguiApp) Then
    If SAPguiApp Is Nothing Then
         Set SapGuiAuto = GetObject("sapgui")
         Set SAPguiApp = CreateObject("Sapgui.ScriptingCtrl.1")
    End If
    
    If IsObject(conn) Then
         Set conn = SAPguiApp.OpenConnectionbyconnectionstring("CONNECTIONSTRING", True)
    End If
        
    If session Is Nothing Then
         Set session = conn.Children(0)
    End If
    
    If Not IsObject(Wscript) Then
         Wscript.ConnectObject session, "on"
         Wscript.ConnectObject App, "on"
    End If
'----------------------Action code------------------------'
    With session
            session.findById("wnd[0]").maximize
            session.findById("wnd[0]/usr/txtRSYST-BNAME").Text = "USERNAME"
            session.findById("wnd[0]/usr/pwdRSYST-BCODE").Text = "PASSWORD"
            session.findById("wnd[0]/usr/pwdRSYST-BCODE").SetFocus
            session.findById("wnd[0]/usr/pwdRSYST-BCODE").caretPosition = 8
            session.findById("wnd[0]").sendVKey 0
            session.findById("wnd[0]/tbar[0]/okcd").Text = "rsa1"
            session.findById("wnd[0]").sendVKey 0
            session.findById("wnd[0]/shellcont[0]/shell/shellcont[1]/shell/shellcont[1]/shell").SelectItem "          8", "1"
            session.findById("wnd[0]/shellcont[0]/shell/shellcont[1]/shell/shellcont[1]/shell").ensureVisibleHorizontalItem "          8", "1"
            session.findById("wnd[0]/shellcont[0]/shell/shellcont[1]/shell/shellcont[1]/shell").topNode = "          2"
            session.findById("wnd[0]/shellcont[0]/shell/shellcont[1]/shell/shellcont[1]/shell").clickLink "          8", "1"
            session.findById("wnd[0]/shellcont[1]/shell/shellcont[0]/shell/shellcont[1]/shell/shellcont[0]/shell").pressButton "%AWB_TREE_SEARCH"
            session.findById("wnd[1]/usr/txtRSAWBN_S_DYNPRO_0500-SEARCH_TERM").Text = ws1.Cells(9, 10).Value
            session.findById("wnd[1]/usr/txtRSAWBN_S_DYNPRO_0500-SEARCH_TERM").caretPosition = 8
            session.findById("wnd[1]").sendVKey 0
            session.findById("wnd[0]/shellcont[1]/shell/shellcont[0]/shell/shellcont[1]/shell/shellcont[1]/shell").SelectItem "       709", "COL1"
            session.findById("wnd[0]/shellcont[1]/shell/shellcont[0]/shell/shellcont[1]/shell/shellcont[1]/shell").ensureVisibleHorizontalItem "       709", "COL1"
            session.findById("wnd[0]/shellcont[1]/shell/shellcont[0]/shell/shellcont[1]/shell/shellcont[1]/shell").itemContextMenu "       709", "COL1"
            session.findById("wnd[0]/shellcont[1]/shell/shellcont[0]/shell/shellcont[1]/shell/shellcont[1]/shell").selectContextMenuItem "MANAGE"
            session.findById("wnd[1]/usr/tblSAPLRSSMCTRL_2600").getAbsoluteRow(0).Selected = True
            session.findById("wnd[1]/usr/tblSAPLRSSMCTRL_2600/lblG_T_2600-VER_STATUS[0,0]").SetFocus
            session.findById("wnd[1]/usr/tblSAPLRSSMCTRL_2600/lblG_T_2600-VER_STATUS[0,0]").caretPosition = 0
            session.findById("wnd[1]/tbar[0]/btn[5]").press
            session.findById("wnd[2]/tbar[0]/btn[5]").press
            session.findById("wnd[2]/usr/txtG_RECORD_MAX").Text = ""
            session.findById("wnd[2]/tbar[0]/btn[0]").press
            session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell").setCurrentCell -1, ""
            session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell").SelectAll
            session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell").contextMenu
            session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell").selectContextMenuItem "&XXL"
            session.findById("wnd[1]/tbar[0]/btn[0]").press


            MsgBox ("Data downloaded")
    End With
'---------------------------------------------------------'
End If
End Sub


0
* Please Login or Register to Answer, Follow or Comment.

3 Answers

Best Answer
Script Man Jan 19, 2017 at 10:50 AM
1

Hi Annie,

my suggestion is as follows:

. . .
session.findById("wnd[0]/shellcont[1]/shell/shellcont[0]/shell/shellcont[1]/shell/shellcont[0]/shell").pressButton "%AWB_TREE_SEARCH"
session.findById("wnd[1]/usr/txtRSAWBN_S_DYNPRO_0500-SEARCH_TERM").Text = ws1.Cells(9, 10).Value
'The following command is not absolutely necessary and could lead to an error if the length of the parameter to be searched is less than 8.
'session.findById("wnd[1]/usr/txtRSAWBN_S_DYNPRO_0500-SEARCH_TERM").caretPosition = 8
session.findById("wnd[1]").sendVKey 0

myKey = session.findById("wnd[0]/usr/cntlCNTL_CONTAINER/shellcont/shell/shellcont[0]/shell/shellcont[1]/shell[1]").getFocusedNodeKey()

session.findById("wnd[0]/shellcont[1]/shell/shellcont[0]/shell/shellcont[1]/shell/shellcont[1]/shell").SelectItem myKey, "COL1"
session.findById("wnd[0]/shellcont[1]/shell/shellcont[0]/shell/shellcont[1]/shell/shellcont[1]/shell").ensureVisibleHorizontalItem myKey, "COL1"
session.findById("wnd[0]/shellcont[1]/shell/shellcont[0]/shell/shellcont[1]/shell/shellcont[1]/shell").itemContextMenu myKey, "COL1"
session.findById("wnd[0]/shellcont[1]/shell/shellcont[0]/shell/shellcont[1]/shell/shellcont[1]/shell").selectContextMenuItem "MANAGE"
. . .

Regards,

ScriptMan

Show 2 Share
10 |10000 characters needed characters left characters exceeded

Hello ScriptMan,

it is a tiny difference between selected and focused nodes. If the nodes are selected they are marked, if a node is focused there is a thin line around the node. Look at the picture in my post, three nodes are selected and the last of the selected nodes is focused. You can select many nodes but only one can be focused. Now it is dependent what Annie means, selected or focused.

Cheers
Stefan

1

Hi Scriptman,

Thanks for the response. I tried the way you suggested.

But it is throwing me the "Object required" error, in the highlighted line.

I assume that, this is probably because, mykey is not being assigned any value.

This is how the selection appears, while running the script.

As Stefan mentioned, seems like the node is selected, but not focused.

Is there any method that fetches the selected node value, to mykey?

Thanks and regards,

Annie

error.png (52.4 kB)
error1.png (18.2 kB)
bw.png (10.7 kB)
0
Script Man Jan 23, 2017 at 09:48 AM
1

Hi Annie,

the variable myKey expects a string and not Object.

myKey = session.findById("wnd[0]/usr/cntlCNTL_CONTAINER/shellcont/shell/shellcont[0]/shell/shellcont[1]/shell[1]").getFocusedNodeKey()

without command set at the beginning... ;-)

And if it still does not work, you could use the following command.

for example:

session.findById("wnd[0]/shellcont[1]/shell/shellcont[0]/shell/shellcont[1]/shell/shellcont[0]/shell").pressButton "%AWB_TREE_SEARCH"
session.findById("wnd[0]/usr/cntlCNTL_CONTAINER/shellcont/shell/shellcont[0]/shell/shellcont[1]/shell[1]").setFocus()
. . .

Regards,
ScriptMan

Show 1 Share
10 |10000 characters needed characters left characters exceeded

yaaay!!! it worked perfectly :)

Thanks Scriptman. I had declared the mykey variable as object. Changed it, and i got what i wanted.

Thanks a ton :)

0
Stefan Schnell Jan 19, 2017 at 10:22 AM
0

Hello Annie,

yes it is possible to get the selected items (nodes).

Dim Nodes As SAPFEWSELib.GuiCollection
Set Tree = session.findById("wnd[0]/shellcont[1]/shell/shellcont[0]/shell/shellcont[1]/shell/shellcont[1]/shell")
Set Nodes = Tree.GetSelectedNodes

With GetSelectedNodes you get all selected nodes, because with the GuiTree of TAC RSA1 is it possible to select multiple items.

Here you see my three selected nodes.

Let us know your results.

Cheers
Stefan

P.S. I think you can find useful information about trees here.


Show 3 Share
10 |10000 characters needed characters left characters exceeded

Hi Stefan,

Thanks for the response.

From the example you provided above, how can i use the value of Item 1 among the selected nodes, at the place of "709" in my code below.

session.findById("wnd[0]/shellcont[1]/shell/shellcont[0]/shell/shellcont[1]/shell/shellcont[1]/shell").SelectItem "       709", "COL1"

If I get this one right, I am sure, the code will work like a charm.

Looking forward for your response.

Thanks and regards,

Annie

0

Hello Annie,

at first you must decide what you need, selected nodes (items) or focussed node. As I write above, with the Tree of RSA1 is it possible to select more as one node.

Here the code to get the node names of selected items on different ways and to get the focussed item:

Sub Test()

  Dim SapGuiAuto As Object
  Dim SapGuiApp As SAPFEWSELib.GuiApplication
  Dim conn As SAPFEWSELib.GuiConnection
  Dim ses As SAPFEWSELib.GuiSession
  Dim Tree As SAPFEWSELib.GuiTree
  Dim Nodes As SAPFEWSELib.GuiCollection
  Dim Nodename As String
  Dim i As Integer
  
  Set SapGuiAuto = GetObject("SAPGUI")
  Set SapGuiApp = SapGuiAuto.GetScriptingEngine
  Set conn = SapGuiApp.Children(0)
  Set ses = conn.Children(0)
  
  Set Tree = ses.FindById("wnd[0]/shellcont[1]/shell/shellcont[0]/shell/shellcont[1]/shell/shellcont[1]/shell")
  Set Nodes = Tree.GetSelectedNodes
  
  Debug.Print "Focused node:"
  Debug.Print Tree.GetFocusedNodeKey
  
  '-You can use For Each loop
  Debug.Print "Selected nodes via For Each loop:"
  For Each Node In Nodes
    Nodename = Node
    Debug.Print Nodename
  Next
  
  '-Or direct access
  Debug.Print "Selected nodes via direct access:"
  For i = 0 To Nodes.Count - 1
    Nodename = Nodes(i)
    Debug.Print Nodename
  Next

End Sub

On the one hand you can use a For Each loop or on the other hand you can use direct access.

And you can use it like this, if one is selected:

session.findById("wnd[0]/shellcont[1]/shell/shellcont[0]/shell/shellcont[1]/shell/shellcont[1]/shell").SelectItem Nodename, "COL1"

Cheers
Stefan

0

Thanks a lot Stefan.

Scriptman's code helped me achieve, what exactly I wanted. So marked it as the answer.

Will definitely try the method you suggested. Thanks a lot for the knowledge shared.

Regards,

Annie

0