0

Ok, I'm currently working on UIAutomation with nodeJS and I'm using EDGE.js node module. All works fine (wow), but I have an issue with code re-usability.

I have few mostly identical functions that consist from the same code for more than 50%. Of course I want to move this code to a single place, but problem is that this code placed in js comments (EDGE stuff).

How can I reuse my code to avoid repetitions in EDGE.js?

Yeah.. as a last resort I can put everything into one c# "program" and call different c# functions depending on arguments, but probably there is a way to keep several js functions? Thanks!

Here is an example of 2 functions. I want to keep different only "public async Task" part at the bottom of each block. Any ideas?

BTW: Any suggestions about C# code are also welcome! Cause I'm pretty sure, that it's total crap ^^

    getWindows: function() {

      /*
        using System;
        using System.Windows;
        using System.Windows.Automation;
        using System.Threading.Tasks;
        using System.Collections.Generic;

        public class myRect
        {
          public int width { get; set; }
          public int height { get; set; }
          public int top { get; set; }
          public int left { get; set; }
          public myRect( AutomationElement el ) {
            System.Windows.Rect r = (System.Windows.Rect)(
            el.GetCurrentPropertyValue(
              AutomationElement.BoundingRectangleProperty,
              true));
            width  = (int)r.Width;
            height = (int)r.Height;
            top    = (int)r.Top;
            left   = (int)r.Left;
          }
        }

        public class Winfo
        {
          public string name { get; set; }
          public string automationId { get; set; }
          public int processId { get; set; }
          public myRect window { get; set; }
          public myRect browser { get; set; }
        }


        public class Startup {

          private Winfo getWinInfo( AutomationElement el ) {
            if ( el == null ) return( null );
            Winfo winfo = new Winfo {
              name = el.Current.Name,
              automationId = el.Current.AutomationId,
              processId = el.Current.ProcessId,
              window = new myRect(el)
            };

            try {
              var tmpWeb = el
                .FindFirst( TreeScope.Descendants,
                  new PropertyCondition(
                    AutomationElement.ClassNameProperty,
                    "CefBrowserWindow") )
                .FindFirst( TreeScope.Descendants,
                  new PropertyCondition(
                    AutomationElement.NameProperty,
                    "Chrome Legacy Window"));

              winfo.browser = new myRect(tmpWeb);
            } catch { winfo.browser = null; }

            return(winfo);
          }

          public async Task<object> Invoke(dynamic input) {
            var els = AutomationElement.RootElement.FindAll(
              TreeScope.Children,
              Condition.TrueCondition);

            List<Winfo> windowList = new List<Winfo>{};
            bool all;
            try { all = (bool)input.all; } catch { all = false; };

            foreach (AutomationElement el in els) {
              Winfo winfo = getWinInfo(el);
              if ((winfo!=null) && (all || (winfo.browser!=null))) {
                windowList.Add( winfo );
              }
            }
            return(windowList);
          }
        }
       */
    }

And another one

    waitWindow: function() {

      /*
        using System;
        using System.Windows;
        using System.ComponentModel;
        using System.Windows.Automation;
        using System.Threading.Tasks;
        using System.Threading;
        using System.Collections.Generic;

        public class myRect {
          public int width { get; set; }
          public int height { get; set; }
          public int top { get; set; }
          public int left { get; set; }
          public myRect( AutomationElement el ) {
            System.Windows.Rect r = (System.Windows.Rect)(
            el.GetCurrentPropertyValue(
              AutomationElement.BoundingRectangleProperty,
              true));
            width  = (int)r.Width;
            height = (int)r.Height;
            top    = (int)r.Top;
            left   = (int)r.Left;
          }
        }

        public class Winfo
        {
          public string name { get; set; }
          public string automationId { get; set; }
          public int processId { get; set; }
          public myRect window { get; set; }
          public myRect browser { get; set; }
        }

        public class Startup {
          private static AutoResetEvent waitHandle;

          private Winfo getWinInfo( AutomationElement el ) {
            if ( el == null ) return( null );
            Winfo winfo = new Winfo {
              name = el.Current.Name,
              automationId = el.Current.AutomationId,
              processId = el.Current.ProcessId,
              window = new myRect(el)
            };

            try {
              var tmpWeb = el
                .FindFirst( TreeScope.Descendants,
                  new PropertyCondition(
                    AutomationElement.ClassNameProperty,
                    "CefBrowserWindow") )
                .FindFirst( TreeScope.Descendants,
                  new PropertyCondition(
                    AutomationElement.NameProperty,
                    "Chrome Legacy Window"));

              winfo.browser = new myRect(tmpWeb);
            } catch { winfo.browser = null; }

            return(winfo);
          }

          public async Task<object> Invoke(dynamic input) {

            int t;
            try { t = (int)input.timeout; } catch { t = 0; };
            string wname;
            try { wname = (string)input.name; } catch { wname = ""; };

            AutomationElement el = AutomationElement.RootElement.FindFirst(
              TreeScope.Children,
              new PropertyCondition( AutomationElement.NameProperty, wname ));

            if ( el == null ) {
              waitHandle = new AutoResetEvent(false);
              Automation.AddAutomationEventHandler(
                WindowPattern.WindowOpenedEvent,
                AutomationElement.RootElement,
                TreeScope.Children,
                (sender, e) => {
                  var obj = sender as AutomationElement;
                  if (obj.Current.Name == wname) {
                    el = obj;
                    waitHandle.Set();
                  }
                }
              );

              waitHandle.WaitOne(t);
              Automation.RemoveAllEventHandlers();
            }

            return( getWinInfo(el) );
          }
        }
       */
    }
  };

1 Answer 1

1

You can split the reusable C# code into separate multi-line javascript strings, using the same technique that edgejs uses. Below is a simple example where a function has been broken into two separate variables, Line1 and Line2. You could split your code into multiple functions/variables that contains the re-usable code and then build your code by concatenating the individual bits.

var edge = require('edge');

function getMultilineString(fn){
    return (fn).toString().match(/[^]*\/\*([^]*)\*\/\}$/)[1];
}
var line1 = getMultilineString(function () {/*
    async (input) => {        
*/});

var line2 = getMultilineString(function () {/*
        return ".NET welcomes " + input.ToString(); 
    }
*/});

//var hello = edge.func(function () {/*
//    async (input) => { 
//        return ".NET welcomes " + input.ToString(); 
//    }
//*/});


var hello = edge.func(line1 + line2);

hello('Node.js', function (error, result) {
    if (error) throw error;
    console.log(result);
});
Sign up to request clarification or add additional context in comments.

1 Comment

Ok. It probably works, but it looks soooo ugly, so I'd prefer to have single C# function with switch ... case. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.