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) );
}
}
*/
}
};