Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Diagnostics;
- using System.IO;
- using System.Threading;
- using System.Windows.Automation;
- namespace TestWhiteCalculator
- {
- class WhiteCalculatorTest
- {
- static public ExpandCollapsePattern GetExpandCollapsePattern(
- AutomationElement targetControl)
- {
- ExpandCollapsePattern expandCollapsePattern = null;
- try
- {
- expandCollapsePattern =
- targetControl.GetCurrentPattern(
- ExpandCollapsePattern.Pattern)
- as ExpandCollapsePattern;
- }
- // Object doesn't support the ExpandCollapsePattern control pattern.
- catch (InvalidOperationException)
- {
- return null;
- }
- return expandCollapsePattern;
- }
- static public void ExpandCollapseMenuItem(
- AutomationElement menuItem)
- {
- if (menuItem == null)
- {
- throw new ArgumentNullException(
- "AutomationElement argument cannot be null.");
- }
- ExpandCollapsePattern expandCollapsePattern =
- GetExpandCollapsePattern(menuItem);
- if (expandCollapsePattern == null)
- {
- return;
- }
- if (expandCollapsePattern.Current.ExpandCollapseState ==
- ExpandCollapseState.LeafNode)
- {
- return;
- }
- try
- {
- if (expandCollapsePattern.Current.ExpandCollapseState == ExpandCollapseState.Expanded)
- {
- // Collapse the menu item.
- expandCollapsePattern.Collapse();
- }
- else if (expandCollapsePattern.Current.ExpandCollapseState == ExpandCollapseState.Collapsed ||
- expandCollapsePattern.Current.ExpandCollapseState == ExpandCollapseState.PartiallyExpanded)
- {
- // Expand the menu item.
- expandCollapsePattern.Expand();
- }
- }
- // Control is not enabled
- catch (ElementNotEnabledException)
- {
- // TO DO: error handling.
- }
- // Control is unable to perform operation.
- catch (InvalidOperationException)
- {
- // TO DO: error handling.
- }
- }
- static private void InvokeControl(AutomationElement targetControl)
- {
- InvokePattern invokePattern = null;
- try
- {
- invokePattern =
- targetControl.GetCurrentPattern(InvokePattern.Pattern)
- as InvokePattern;
- }
- catch (ElementNotEnabledException)
- {
- // Object is not enabled
- return;
- }
- catch (InvalidOperationException)
- {
- // object doesn't support the InvokePattern control pattern
- return;
- }
- invokePattern.Invoke();
- }
- static private void InsertText(AutomationElement targetControl,
- string value)
- {
- if (value == null)
- throw new ArgumentNullException(
- "String parameter must not be null.");
- if (targetControl == null)
- throw new ArgumentNullException(
- "AutomationElement parameter must not be null");
- if (!targetControl.Current.IsEnabled)
- {
- throw new InvalidOperationException(
- "The control is not enabled.\n\n");
- }
- if (!targetControl.Current.IsKeyboardFocusable)
- {
- throw new InvalidOperationException(
- "The control is not focusable.\n\n");
- }
- object valuePattern = null;
- if (!targetControl.TryGetCurrentPattern(
- ValuePattern.Pattern, out valuePattern))
- {
- }
- else
- {
- if (((ValuePattern)valuePattern).Current.IsReadOnly)
- {
- throw new InvalidOperationException(
- "The control is read-only.");
- }
- else
- {
- ((ValuePattern)valuePattern).SetValue(value);
- }
- }
- }
- static private object GetValueProperty(
- ValuePattern valuePattern,
- AutomationProperty automationProperty)
- {
- if (valuePattern == null || automationProperty == null)
- {
- throw new ArgumentNullException("Argument cannot be null.");
- }
- if (automationProperty.Id ==
- ValuePattern.ValueProperty.Id)
- {
- return valuePattern.Current.Value;
- }
- return null;
- }
- static void Main(string[] args)
- {
- Console.Write("Username:");
- var username = Console.ReadLine();
- Console.Write("Password:");
- var password = Console.ReadLine();
- Console.WriteLine("Starting PRO.");
- string ExeSourceFile = @"P";
- bool startYet = false;
- var processStartInfo = new ProcessStartInfo(ExeSourceFile);
- processStartInfo.FileName = ExeSourceFile;
- processStartInfo.WorkingDirectory = Path.GetDirectoryName(ExeSourceFile);
- var process = Process.Start(processStartInfo);
- Thread.Sleep(1000);
- while (true)
- {
- try
- {
- var mainWindow = AutomationElement.RootElement.FindChildByProcessId(process.Id);
- var statusObj = mainWindow.FindChildByCondition(new PropertyCondition(AutomationElement.AutomationIdProperty, "StatusText"));
- var menu = mainWindow.FindChildByCondition(new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Menu));
- var bot = menu.FindChildByCondition(new PropertyCondition(AutomationElement.NameProperty, "Bot"));
- var pathObj = bot.FindChildByCondition(new PropertyCondition(AutomationElement.AutomationIdProperty, "MenuPathScript"));
- var status = statusObj.GetCurrentPropertyValue(AutomationElement.NameProperty).ToString();
- var pathstatus = "";
- if (pathObj != null)
- {
- pathstatus = pathObj.GetCurrentPropertyValue(AutomationElement.NameProperty).ToString();
- }
- //if (false)
- if (status == "Offline")
- {
- Console.WriteLine("PROShine AutoLogin : Logging into PROShine.");
- startYet = false;
- var conn = menu.FindChildByCondition(new PropertyCondition(AutomationElement.NameProperty, "Connection"));
- ExpandCollapseMenuItem(conn);
- var login = conn.FindChildByCondition(new PropertyCondition(AutomationElement.NameProperty, "Login"));
- InvokeControl(login);
- Thread.Sleep(1000);
- //This is shit, but nothing else worked
- Condition popupCond = new AndCondition(
- new PropertyCondition(AutomationElement.NameProperty, "PROShine - Connection"),
- new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Window));
- var popup = mainWindow.FindFirst(TreeScope.Descendants, popupCond);
- var userbox = popup.FindChildByCondition(new PropertyCondition(AutomationElement.AutomationIdProperty, "UsernameTextBox"));
- var passbox = popup.FindChildByCondition(new PropertyCondition(AutomationElement.AutomationIdProperty, "PasswordTextBox"));
- InsertText(userbox, username);
- InsertText(passbox, password);
- var loginbutton = popup.FindChildByCondition(new PropertyCondition(AutomationElement.NameProperty, "Login"));
- InvokeControl(loginbutton);
- Thread.Sleep(5000);
- }
- else
- {
- Condition popupOpenCond = new AndCondition(
- new PropertyCondition(AutomationElement.LocalizedControlTypeProperty, "Dialog"),
- new PropertyCondition(AutomationElement.ClassNameProperty, "#32770"));
- var popupOpen = mainWindow.FindFirst(TreeScope.Descendants, popupOpenCond);
- if(startYet == false)
- {
- ExpandCollapseMenuItem(bot);
- }
- if (pathstatus == "Path: None" && popupOpen == null)
- {
- Console.WriteLine("PROShine AutoLogin : Choosing path for PROShine.");
- InvokeControl(pathObj);
- Thread.Sleep(2000);
- popupOpen = mainWindow.FindFirst(TreeScope.Descendants, popupOpenCond);
- Condition fileCond = new AndCondition(
- new PropertyCondition(AutomationElement.NameProperty, "File name:"),
- new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Edit));
- var filebox = popupOpen.FindFirst(TreeScope.Descendants, fileCond);
- InsertText(filebox, @"fileloc");
- Condition popupCloseCond = new AndCondition(
- new PropertyCondition(AutomationElement.AutomationIdProperty, "1"),
- Condition.TrueCondition);
- Thread.Sleep(2000);
- var fileboxclose = popupOpen.FindFirst(TreeScope.Descendants, popupCloseCond);
- InvokeControl(fileboxclose);
- Thread.Sleep(1000);
- }
- else if(startYet == false)
- {
- var start = bot.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.AutomationIdProperty, "BotStartMenuItem"));
- if (start != null)
- {
- Console.WriteLine("PROShine AutoLogin : Starting Bot.");
- InvokeControl(start);
- Thread.Sleep(2000);
- startYet = true;
- Console.WriteLine("PROShine AutoLogin : Started. Waiting for relog");
- }
- }
- }
- }
- catch (Exception e) { }
- }
- }
- }
- public static class AutomationExtensions
- {
- public static AutomationElement FindChildByProcessId(this AutomationElement element, int processId)
- {
- var result = element.FindChildByCondition(
- new PropertyCondition(AutomationElement.ProcessIdProperty, processId));
- return result;
- }
- public static AutomationElement FindChildByCondition(this AutomationElement element, Condition condition)
- {
- var result = element.FindFirst(
- TreeScope.Children,
- condition);
- return result;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement