You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

316 lines
10 KiB

namespace SeleniumBrowser.Command
{
using OpenQA.Selenium.Support.UI;
using OpenQA.Selenium;
using System;
using System.Collections.Concurrent;
using System.Threading;
using System.Windows.Forms;
using System.Collections.ObjectModel;
using System.Drawing;
using log4net;
public class CommandInfo
{
private readonly static ILog _log = LogManager.GetLogger(typeof(CommandInfo));
public const string ID = "id";
public const string CLASSNAME = "classname";
public const string XPATH = "xpath";
public const string LINKTEXT = "linktext";
public const string NAME = "name";
public const string EVENT_CLICK = "click";
public const string EVENT_FILL = "fill";
public const string EVENT_SETURL = "seturl";
public const string EVENT_SENDKEY = "sendkey";
public const string EVENT_PASTE = "paste";
public const string EVENT_SLEEP = "sleep";
public delegate string DgHandler(IWebDriver i_uwdBrowser, IWebElement i_iweTarget, ref ConcurrentDictionary<string, object> i_cdGlobalData);
internal CommanderGlobalData BatchProcessData { get; set; }
public string Command { get; set; }
public int WaitTargetSecond { get; set; } = 10;
public int TargetNo { get; set; } = 0;
public string Target
{
get { return _target; }
set
{
_target = value;
if (_target != null)
{
int nIdx = _target.IndexOf('=', StringComparison.OrdinalIgnoreCase);
targetType = _target.Substring(0, nIdx);
targetValue = _target.Substring(nIdx + 1);
}
else
{
targetType = null;
targetValue = null;
}
}
}
private string _target;
protected string targetType { get; private set; }
protected string targetValue { get; private set; }
public string Value { get; set; }
public int SleepAfterEvent { get; set; } = 1;
protected string click(IWebDriver i_uwdBrowser, IWebElement i_iweTarget, ref ConcurrentDictionary<string, object> i_cdGlobalData)
{
string sMsg = null;
do
{
string sFilePath = System.IO.Path.Combine(BatchProcessData.StotrFolder, $"{BatchProcessData.CommandCount:000}.png");
string sStepFilePath = System.IO.Path.Combine(BatchProcessData.StotrFolder, $"Step_{BatchProcessData.CommandCount:000}.png");
lock (i_uwdBrowser)
{
(i_uwdBrowser as ITakesScreenshot).GetScreenshot().SaveAsFile(sFilePath);
}
System.Diagnostics.Debug.WriteLine(i_iweTarget.Location);
StringFormat stringFormat = new StringFormat
{
Alignment = StringAlignment.Center,
LineAlignment = StringAlignment.Center
};
Image i = Image.FromFile(sFilePath);
BatchProcessData.StepCount++;
using (var graphics = Graphics.FromImage(i))
{
int width = 50;
int height = 50;
Rectangle rHeader = new Rectangle() { X = i_iweTarget.Location.X - width, Y = i_iweTarget.Location.Y - height, Width = width, Height = height };
graphics.FillEllipse(new SolidBrush(Color.Red), rHeader.X, rHeader.Y, rHeader.Width, rHeader.Height);
graphics.DrawString($"{BatchProcessData.StepCount}", new Font(FontFamily.GenericMonospace, 24.0F,
FontStyle.Italic, GraphicsUnit.Pixel), new SolidBrush(Color.White), rHeader, stringFormat);
}
i.Save(sStepFilePath);
i_iweTarget?.Click();
}
while (false);
return sMsg;
}
protected string fill(IWebDriver i_uwdBrowser, IWebElement i_iweTarget, ref ConcurrentDictionary<string, object> i_cdGlobalData)
{
string sMsg = null;
do
{
i_iweTarget?.SendKeys(Value);
}
while (false);
return sMsg;
}
public string Process(IWebDriver i_uwdBrowser, ref ConcurrentDictionary<string, object> i_cdGlobalData,
[System.Runtime.CompilerServices.CallerLineNumber] int i_nCodeLine = 0,
[System.Runtime.CompilerServices.CallerMemberName] string i_sMemberName = null,
[System.Runtime.CompilerServices.CallerFilePath] string i_sSourcePath = null)
{
string sMsg = null;
try
{
do
{
if (i_uwdBrowser == null)
{
sMsg = "NO_BROWSER";
break;
}
_log.Debug(Command);
switch (Command)
{
case EVENT_CLICK:
sMsg = wait(i_uwdBrowser, ref i_cdGlobalData, click);
break;
case EVENT_FILL:
sMsg = wait(i_uwdBrowser, ref i_cdGlobalData, fill);
break;
case EVENT_SETURL:
{
i_uwdBrowser.Url = Value;
}
break;
case EVENT_SENDKEY:
{
SendKeys.Send(Value);
}
break;
case EVENT_PASTE:
{
Clipboard.SetText(Value);
SendKeys.Send("^{v}");
}
break;
case EVENT_SLEEP:
{
if (SleepAfterEvent > 0)
{
Thread.Sleep(SleepAfterEvent * 1000);
}
}
break;
default:
sMsg = $"NOT SUPPORT Command '{Command}'";
break;
}
if (sMsg != null)
{
break;
}
if (SleepAfterEvent > 0)
{
Thread.Sleep(SleepAfterEvent * 1000);
}
}
while (false);
}
catch (Exception ex)
{
sMsg = $"{typeof(CommandInfo).Name} unhandle exception. Call from {i_sMemberName} {i_sSourcePath}({i_nCodeLine}).";
#if DEBUG
sMsg += ex.Message;
#endif
}
#if DEBUG
if (sMsg != null)
{
System.Diagnostics.Debug.WriteLine(sMsg);
}
#endif
return sMsg;
}
protected By getTargetBy()
{
By byRes = null;
switch (targetType.ToLower())
{
case ID:
byRes = By.Id(targetValue);
break;
case CLASSNAME:
byRes = By.ClassName(targetValue);
break;
case XPATH:
byRes = By.XPath(targetValue);
break;
case NAME:
byRes = By.Name(targetValue);
break;
case LINKTEXT:
byRes = By.LinkText(targetValue);
break;
default:
throw new NotImplementedException($"Not support type of{targetType}");
}
return byRes;
}
protected string wait(IWebDriver i_uwdBrowser, ref ConcurrentDictionary<string, object> i_cdGlobalData, DgHandler i_dgHandler)
{
string sMsg = null;
IWebElement iweTarget = null;
try
{
do
{
By byItem = getTargetBy();
if (WaitTargetSecond > 0)
{
#if DEBUG
System.Diagnostics.Debug.WriteLine($"[{DateTime.Now}] Waiting for '{byItem}' begin");
#endif
var wait = new WebDriverWait(i_uwdBrowser, TimeSpan.FromSeconds(WaitTargetSecond));
if (targetType == ID)
{
iweTarget = wait.Until((drv => drv.FindElement(byItem)));
}
else
{
ReadOnlyCollection<IWebElement> iwes = wait.Until((drv => drv.FindElements(byItem)));
if (iwes != null && iwes.Count > TargetNo)
{
iweTarget = iwes[TargetNo];
}
}
#if DEBUG
System.Diagnostics.Debug.WriteLine($"[{DateTime.Now}] Waiting for '{byItem}' end");
#endif
}
else
{
if (targetType == ID)
{
iweTarget = i_uwdBrowser.FindElement(byItem);
}
else
{
ReadOnlyCollection<IWebElement> iwes = i_uwdBrowser.FindElements(byItem);
if (iwes != null && iwes.Count <= TargetNo)
{
iweTarget = iwes[TargetNo];
}
}
}
} while (false);
if (iweTarget == null)
{
sMsg = $"WAIT_FAIL({Target}:{TargetNo})";
}
}
catch (Exception ex)
{
sMsg = $"WAIT_FAIL({Target})";
#if DEBUG
sMsg += ex.Message;
#endif
}
if (sMsg == null && i_dgHandler != null)
{
sMsg = i_dgHandler(i_uwdBrowser, iweTarget, ref i_cdGlobalData);
}
return sMsg;
}
}
}