Forum Discussion

Don's avatar
Don
Contributor
8 years ago

Getting the Index of Object with 4 processes running

Hello, 

 

I am having problems when I open 4 instances of an Excel Application when Testing. The process are as Follows:

Process("EXCEL",2)

Process("EXCEL",3)

Process("EXCEL",4)

 

The Mapped Name stays under Aliases.EXCEL but I am trying to get the Name to Identify the proper Index of the program.

For Example I want to assign Process("EXCEL",3) to a Variable I have tried the following code with no results.

 

var excel = Aliases.EXCEL.FullName(Sys.Process("EXCEL").Window("XLMAIN","Microsoft Excel", 3));
var excel = Sys.Process("EXCEL").Window("XLMAIN","Microsoft Excel", 3);

var excel = Aliases.EXCEL.Name("Process","EXCEL",2);

 

even when I try getting the index it would only get the Index of the first Excel so it would return a value of 1 instead of 3.

 

I have uploaded a screenshot showing excel having 4 processes opened but unable to find any when running the automation test.

 

Can anyone point me in the right direction that allows me to get the proper index for excel or any other program.

 

Thanks in Advance.

  • JScript sucks compared to Javascript. 

    Edit : okay okay. There's nothing with Jscript. But we'll need TestComplete to upgrade the compiler for their JScript to match their Javascript implementation.

     

    BUT, that's spilled milk and we shall overcome.

     

    In jscript, FindAllChildren returns not an array-like-object but some other kind of ... thing (aka multi-dimensional VB array)

     

    So:

     

     

    function findProc(name) {
       return Sys.FindAllChildren(["ProcessName"],[name],1).toArray()
    }

     

     

    function findExcel(index) {
      var arr = findProc("EXCEL")
      for ( var i = 0 ; i < arr.length ; i++) {
        if (arr[i].Index === index) {
          return arr[i]
        }
      }
    }

     

     

  • Don's avatar
    Don
    Contributor

    Ok so I was able to figure it out with more trial and error. 

     

    var excel = Sys.Process("EXCEL", 3);

     

    I got to this solution by studying and trying different things using the Object Explorer. One thing I was confused about is getting the Property Sys.Process since I was using Aliases.Excel i thought I would have to use both together but it looks like you can do it with one or the other. 

     

    However, if The process EXCEL does not have more than 3 opened processes it will Fail the test as Object not found. I tried using Sys.WaitProcess("EXCEL",3) but this only gets the first process and not the 3rd.

     

    I will continue to research and update as I find more solutions.

     

     

     

    • KSQian's avatar
      KSQian
      Contributor

      I am not exactly sure what you need. Are you looking for the third instance and only the third instance?

       

      I like to think more generally so I'd probably write something extensible like a process finder. 

       

      function findProc(caption) {
          for (let i = 0 ; i < Sys.ChildCount ; i++ ) {
              if (~Sys.Child(i).CommandLine.IndexOf(caption)) return Sys.Child(i) 
          }
      }

      This uses commandline to determine process, but you can probably use others. 

       

      Or you can make it return an array if there are more than 1... 

       

      function findProc(caption) {
          let arr = [] 
          for (let i = 0 ; i < Sys.ChildCount ; i++ ) {
              if (~Sys.Child(i).CommandLine.IndexOf(caption)) arr.push(Sys.Child(i))
          }
          return arr
      }

      so now you have an arr of processes that matches a certain criteria. 

       

      Edit : I just checked excel. Looks like its index.

       

      So here's a specific piece of code just to find excel 3 that uses findProc array.

       

      function excelFinder(index) {
           const excelProcs = findProc("EXCEL") //yay array.
           return excelProcs.filter( function(excel) { return excel.Index === index }) 
      }
      
      const excel3 = excelFinder(3)

       

      Not sure if this helps your case?

       

       

      • Don's avatar
        Don
        Contributor

        Tried the solution as well as trying to modify it a bit. It crashes my TestComplete. Thanks for the Help :)