Advertisement
Guest User

Untitled

a guest
Apr 15th, 2016
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.22 KB | None | 0 0
  1. using System;
  2. using System.Diagnostics;
  3. using System.IO;
  4. using System.Threading;
  5. using System.Windows.Automation;
  6.  
  7.  
  8.  
  9.  
  10. namespace TestWhiteCalculator
  11. {
  12.  
  13.  
  14.  
  15. class WhiteCalculatorTest
  16. {
  17.  
  18.  
  19. static public ExpandCollapsePattern GetExpandCollapsePattern(
  20. AutomationElement targetControl)
  21. {
  22. ExpandCollapsePattern expandCollapsePattern = null;
  23.  
  24. try
  25. {
  26. expandCollapsePattern =
  27. targetControl.GetCurrentPattern(
  28. ExpandCollapsePattern.Pattern)
  29. as ExpandCollapsePattern;
  30. }
  31. // Object doesn't support the ExpandCollapsePattern control pattern.
  32. catch (InvalidOperationException)
  33. {
  34. return null;
  35. }
  36.  
  37. return expandCollapsePattern;
  38. }
  39.  
  40.  
  41. static public void ExpandCollapseMenuItem(
  42. AutomationElement menuItem)
  43. {
  44. if (menuItem == null)
  45. {
  46. throw new ArgumentNullException(
  47. "AutomationElement argument cannot be null.");
  48. }
  49.  
  50. ExpandCollapsePattern expandCollapsePattern =
  51. GetExpandCollapsePattern(menuItem);
  52.  
  53. if (expandCollapsePattern == null)
  54. {
  55. return;
  56. }
  57.  
  58. if (expandCollapsePattern.Current.ExpandCollapseState ==
  59. ExpandCollapseState.LeafNode)
  60. {
  61. return;
  62. }
  63.  
  64. try
  65. {
  66. if (expandCollapsePattern.Current.ExpandCollapseState == ExpandCollapseState.Expanded)
  67. {
  68. // Collapse the menu item.
  69. expandCollapsePattern.Collapse();
  70. }
  71. else if (expandCollapsePattern.Current.ExpandCollapseState == ExpandCollapseState.Collapsed ||
  72. expandCollapsePattern.Current.ExpandCollapseState == ExpandCollapseState.PartiallyExpanded)
  73. {
  74. // Expand the menu item.
  75. expandCollapsePattern.Expand();
  76. }
  77. }
  78. // Control is not enabled
  79. catch (ElementNotEnabledException)
  80. {
  81. // TO DO: error handling.
  82. }
  83. // Control is unable to perform operation.
  84. catch (InvalidOperationException)
  85. {
  86. // TO DO: error handling.
  87. }
  88. }
  89.  
  90.  
  91. static private void InvokeControl(AutomationElement targetControl)
  92. {
  93. InvokePattern invokePattern = null;
  94.  
  95. try
  96. {
  97. invokePattern =
  98. targetControl.GetCurrentPattern(InvokePattern.Pattern)
  99. as InvokePattern;
  100. }
  101. catch (ElementNotEnabledException)
  102. {
  103. // Object is not enabled
  104. return;
  105. }
  106. catch (InvalidOperationException)
  107. {
  108. // object doesn't support the InvokePattern control pattern
  109. return;
  110. }
  111.  
  112. invokePattern.Invoke();
  113. }
  114.  
  115. static private void InsertText(AutomationElement targetControl,
  116. string value)
  117. {
  118.  
  119. if (value == null)
  120. throw new ArgumentNullException(
  121. "String parameter must not be null.");
  122.  
  123. if (targetControl == null)
  124. throw new ArgumentNullException(
  125. "AutomationElement parameter must not be null");
  126.  
  127. if (!targetControl.Current.IsEnabled)
  128. {
  129. throw new InvalidOperationException(
  130. "The control is not enabled.\n\n");
  131. }
  132.  
  133. if (!targetControl.Current.IsKeyboardFocusable)
  134. {
  135. throw new InvalidOperationException(
  136. "The control is not focusable.\n\n");
  137. }
  138.  
  139. object valuePattern = null;
  140.  
  141. if (!targetControl.TryGetCurrentPattern(
  142. ValuePattern.Pattern, out valuePattern))
  143. {
  144.  
  145. }
  146.  
  147. else
  148. {
  149. if (((ValuePattern)valuePattern).Current.IsReadOnly)
  150. {
  151. throw new InvalidOperationException(
  152. "The control is read-only.");
  153. }
  154. else
  155. {
  156. ((ValuePattern)valuePattern).SetValue(value);
  157. }
  158. }
  159. }
  160.  
  161. static private object GetValueProperty(
  162. ValuePattern valuePattern,
  163. AutomationProperty automationProperty)
  164. {
  165. if (valuePattern == null || automationProperty == null)
  166. {
  167. throw new ArgumentNullException("Argument cannot be null.");
  168. }
  169.  
  170. if (automationProperty.Id ==
  171. ValuePattern.ValueProperty.Id)
  172. {
  173. return valuePattern.Current.Value;
  174. }
  175. return null;
  176. }
  177.  
  178.  
  179. static void Main(string[] args)
  180. {
  181.  
  182. Console.Write("Username:");
  183. var username = Console.ReadLine();
  184. Console.Write("Password:");
  185. var password = Console.ReadLine();
  186. Console.WriteLine("Starting PRO.");
  187. string ExeSourceFile = @"P";
  188. bool startYet = false;
  189.  
  190. var processStartInfo = new ProcessStartInfo(ExeSourceFile);
  191. processStartInfo.FileName = ExeSourceFile;
  192. processStartInfo.WorkingDirectory = Path.GetDirectoryName(ExeSourceFile);
  193.  
  194. var process = Process.Start(processStartInfo);
  195. Thread.Sleep(1000);
  196.  
  197. while (true)
  198. {
  199. try
  200. {
  201.  
  202.  
  203.  
  204.  
  205.  
  206. var mainWindow = AutomationElement.RootElement.FindChildByProcessId(process.Id);
  207. var statusObj = mainWindow.FindChildByCondition(new PropertyCondition(AutomationElement.AutomationIdProperty, "StatusText"));
  208. var menu = mainWindow.FindChildByCondition(new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Menu));
  209. var bot = menu.FindChildByCondition(new PropertyCondition(AutomationElement.NameProperty, "Bot"));
  210. var pathObj = bot.FindChildByCondition(new PropertyCondition(AutomationElement.AutomationIdProperty, "MenuPathScript"));
  211.  
  212. var status = statusObj.GetCurrentPropertyValue(AutomationElement.NameProperty).ToString();
  213. var pathstatus = "";
  214. if (pathObj != null)
  215. {
  216. pathstatus = pathObj.GetCurrentPropertyValue(AutomationElement.NameProperty).ToString();
  217. }
  218.  
  219. //if (false)
  220. if (status == "Offline")
  221. {
  222. Console.WriteLine("PROShine AutoLogin : Logging into PROShine.");
  223. startYet = false;
  224. var conn = menu.FindChildByCondition(new PropertyCondition(AutomationElement.NameProperty, "Connection"));
  225. ExpandCollapseMenuItem(conn);
  226. var login = conn.FindChildByCondition(new PropertyCondition(AutomationElement.NameProperty, "Login"));
  227. InvokeControl(login);
  228. Thread.Sleep(1000);
  229. //This is shit, but nothing else worked
  230. Condition popupCond = new AndCondition(
  231. new PropertyCondition(AutomationElement.NameProperty, "PROShine - Connection"),
  232. new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Window));
  233. var popup = mainWindow.FindFirst(TreeScope.Descendants, popupCond);
  234. var userbox = popup.FindChildByCondition(new PropertyCondition(AutomationElement.AutomationIdProperty, "UsernameTextBox"));
  235. var passbox = popup.FindChildByCondition(new PropertyCondition(AutomationElement.AutomationIdProperty, "PasswordTextBox"));
  236. InsertText(userbox, username);
  237. InsertText(passbox, password);
  238. var loginbutton = popup.FindChildByCondition(new PropertyCondition(AutomationElement.NameProperty, "Login"));
  239. InvokeControl(loginbutton);
  240. Thread.Sleep(5000);
  241. }
  242. else
  243. {
  244.  
  245.  
  246. Condition popupOpenCond = new AndCondition(
  247. new PropertyCondition(AutomationElement.LocalizedControlTypeProperty, "Dialog"),
  248. new PropertyCondition(AutomationElement.ClassNameProperty, "#32770"));
  249. var popupOpen = mainWindow.FindFirst(TreeScope.Descendants, popupOpenCond);
  250.  
  251.  
  252.  
  253. if(startYet == false)
  254. {
  255. ExpandCollapseMenuItem(bot);
  256. }
  257.  
  258. if (pathstatus == "Path: None" && popupOpen == null)
  259. {
  260. Console.WriteLine("PROShine AutoLogin : Choosing path for PROShine.");
  261. InvokeControl(pathObj);
  262. Thread.Sleep(2000);
  263. popupOpen = mainWindow.FindFirst(TreeScope.Descendants, popupOpenCond);
  264. Condition fileCond = new AndCondition(
  265. new PropertyCondition(AutomationElement.NameProperty, "File name:"),
  266. new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Edit));
  267.  
  268. var filebox = popupOpen.FindFirst(TreeScope.Descendants, fileCond);
  269. InsertText(filebox, @"fileloc");
  270. Condition popupCloseCond = new AndCondition(
  271. new PropertyCondition(AutomationElement.AutomationIdProperty, "1"),
  272. Condition.TrueCondition);
  273. Thread.Sleep(2000);
  274. var fileboxclose = popupOpen.FindFirst(TreeScope.Descendants, popupCloseCond);
  275. InvokeControl(fileboxclose);
  276. Thread.Sleep(1000);
  277.  
  278. }
  279. else if(startYet == false)
  280. {
  281. var start = bot.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.AutomationIdProperty, "BotStartMenuItem"));
  282. if (start != null)
  283. {
  284. Console.WriteLine("PROShine AutoLogin : Starting Bot.");
  285. InvokeControl(start);
  286. Thread.Sleep(2000);
  287. startYet = true;
  288. Console.WriteLine("PROShine AutoLogin : Started. Waiting for relog");
  289. }
  290. }
  291.  
  292. }
  293. }
  294. catch (Exception e) { }
  295. }
  296.  
  297.  
  298.  
  299. }
  300. }
  301.  
  302.  
  303.  
  304. public static class AutomationExtensions
  305. {
  306. public static AutomationElement FindChildByProcessId(this AutomationElement element, int processId)
  307. {
  308. var result = element.FindChildByCondition(
  309. new PropertyCondition(AutomationElement.ProcessIdProperty, processId));
  310.  
  311. return result;
  312. }
  313.  
  314. public static AutomationElement FindChildByCondition(this AutomationElement element, Condition condition)
  315. {
  316. var result = element.FindFirst(
  317. TreeScope.Children,
  318. condition);
  319.  
  320. return result;
  321. }
  322. }
  323. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement