GPAL.Application drives Windows desktop applications the same way GPAL.Browser drives a browser: open the app, define selectors against its UI Automation tree, and chain actions like InsertFrom, AppendFrom, FillInFrom, and key combinations. This tutorial opens Notepad++, types and edits text, then hovers over a toolbar button and a menu item.
Here's the whole workflow, start to finish. Each piece is broken down and explained below.
using GenerallyPositive;
using GenerallyPositive.Application;
using static GenerallyPositive.Application.Application;
using static GenerallyPositive.Enums;
GPAL.WithExceptionHandler(ExceptionEventHandler)
.WithInformationHandler(ExceptionEventHandler);
Selector notepadPane = (Selector)GPAL.Selector
.WithSelectorName("NotepadPane")
.WithXPath("/Window/Pane[1]")
.WithOffsetX(10)
.WithOffsetY(10);
Selector indentGuideButton = (Selector)GPAL.Selector
.WithSelectorName("IndentButton")
.WithXPath("/Window/Pane[2]/ToolBar/Button[21]");
Selector cSharpMenuItem = (Selector)GPAL.Selector
.WithSelectorName("C#MenuItem")
.WithXPath("/Window/MenuBar/MenuItem[6]/Menu/MenuItem[4]/Menu/MenuItem[2]");
Application application = GPAL.Application
.WithParameter(@"C:sdiebay.words.txt")
.Open(@"C:Program FilesNotepad++
otepad++.exe")
.WaitFor(5000)
.Maximize
.LeftClick()
.ToGPALObject();
application
.WithSelector(notepadPane)
.CallIfFound(CallIfFound)
.CallIfNotFound(CallIfNotFound)
.Hover()
.InsertFrom("Insert text")
.AppendFrom("Append Text")
.WaitFor(5000);
application.Minimize
.WaitFor(5000);
application
.Restore
.FillInFrom("Overwrite Text");
application
.PressModifierKey(ModifierKeys.Control)
.SendString("z")
.SendString("z")
.ReleaseModifierKey(ModifierKeys.Control);
application
.WithSelector(indentGuideButton)
.CallIfFound(CallIfFound)
.Hover()
.WaitFor(5000);
application
.WithSelector(cSharpMenuItem)
.CallIfFound(CallIfFound)
.Hover()
.WaitFor(5000);
application.Close();
public static CallIfStatus CallIfFound(IApplication application, List<GPALAutomationElement> foundElements, List<GPALAutomationElement> matchedElements, Selector selector, bool matchedAll)
{
CallIfStatus retVal = CallIfStatus.NotHandled;
foreach (GPALAutomationElement element in foundElements)
Debug.WriteLine($"<{element.Ae.Current.Name}> : {element.Ae.Current.LocalizedControlType}");
return retVal;
}
public static CallIfStatus CallIfNotFound(IApplication application, List<GPALAutomationElement> foundElements, List<GPALAutomationElement> matchedElements, Selector selector, bool matchedAll)
{
Debug.WriteLine($"{selector.Name} <{selector.SelectorPaths[0].SelectorPath}> : Not Found.");
return CallIfStatus.NotHandled;
}
GPAL.Application is a fluent factory just like GPAL.Browser. WithParameter passes a command-line argument to the executable (here, a file to open in Notepad++), Open launches it, WaitFor gives it time to start, and Maximize/LeftClick bring it into a known state. ToGPALObject() returns the live IApplication you'll drive for the rest of the workflow.
Application application = GPAL.Application
.WithParameter(@"C:sdiebay.words.txt")
.Open(@"C:Program FilesNotepad++
otepad++.exe")
.WaitFor(5000)
.Maximize
.LeftClick()
.ToGPALObject();
Desktop selectors target the Windows UI Automation tree rather than the DOM. An XPath like /Window/Pane[2]/ToolBar/Button[21] walks down from the application's main window to a specific toolbar button. WithOffsetX/WithOffsetY shift the click point within the matched element - useful when the element's bounding box is larger than the clickable area.
Selector notepadPane = (Selector)GPAL.Selector
.WithSelectorName("NotepadPane")
.WithXPath("/Window/Pane[1]")
.WithOffsetX(10)
.WithOffsetY(10);
Selector indentGuideButton = (Selector)GPAL.Selector
.WithSelectorName("IndentButton")
.WithXPath("/Window/Pane[2]/ToolBar/Button[21]");
Unlike a browser's DOM, there's no devtools inspector built into the OS - tools like Visual Studio's UI Automation inspector help map out a desktop app's UI Automation tree to discover these XPaths.
WithSelector targets the editor pane, CallIfFound/CallIfNotFound react to whether the element was located, and Hover moves focus there. InsertFrom and AppendFrom add text at the cursor and at the end respectively; later, FillInFrom replaces the content entirely after the window has been minimized and restored.
application
.WithSelector(notepadPane)
.CallIfFound(CallIfFound)
.CallIfNotFound(CallIfNotFound)
.Hover()
.InsertFrom("Insert text")
.AppendFrom("Append Text")
.WaitFor(5000);
application.Minimize
.WaitFor(5000);
application
.Restore
.FillInFrom("Overwrite Text");
PressModifierKey/ReleaseModifierKey wrap a key combination like Ctrl+Z for undo, sending SendString calls in between. Hovering over a toolbar button or menu item is a simple way to verify a selector resolves to the right element before wiring up a click.
application
.PressModifierKey(ModifierKeys.Control)
.SendString("z")
.SendString("z")
.ReleaseModifierKey(ModifierKeys.Control);
application
.WithSelector(indentGuideButton)
.CallIfFound(CallIfFound)
.Hover()
.WaitFor(5000);
application.Close();
Hardware-level interaction (mouse moves, key sends) operates on whatever window has OS focus. If the target application loses focus between actions, hardware clicks and key presses will go to the wrong window. Use Hover or LeftClick to re-establish focus before hardware-level steps.
Showing off some plain text in these paragraphs eligendi laboriosam illo nostrum corporis at libero vel voluptas? Expedita, facere dolores voluptatem ad ab rem assumenda soluta!
Lorem ipsum dolor sit amet consectetur adipisicing elit. Obcaecati, iste distinctio veritatis eligendi laboriosam illo nostrum corporis at libero vel voluptas? Expedita, facere dolores voluptatem ad ab rem assumenda soluta!
Lorem ipsum dolor sit amet consectetur adipisicing elit. Obcaecati, iste distinctio veritatis eligendi laboriosam illo nostrum corporis at libero vel voluptas? Expedita, facere dolores voluptatem ad ab rem assumenda soluta!
Lorem ipsum dolor sit amet consectetur adipisicing elit. Quo veniam mollitia excepturi animi eum illum non libero sapiente provident assumenda, delectus voluptatum nobis sed dolorem adipisci laudantium incidunt. Error, ratione?
Lorem ipsum dolor sit amet consectetur adipisicing elit. Quo veniam mollitia excepturi animi eum illum non libero sapiente provident assumenda, delectus voluptatum nobis sed dolorem adipisci laudantium incidunt. Error, ratione?
Lorem ipsum dolor sit amet consectetur adipisicing elit. Quo veniam mollitia excepturi animi eum illum non libero sapiente provident assumenda, delectus voluptatum nobis sed dolorem adipisci laudantium incidunt. Error, ratione?
Lorem ipsum dolor sit amet consectetur adipisicing elit. Quo veniam mollitia excepturi animi eum illum non libero sapiente provident assumenda, delectus voluptatum nobis sed dolorem adipisci laudantium incidunt. Error, ratione?
Here you can find different accents and emphasis sit amet consectetur adipisicing elit. Obcaecati, iste distinctio veritatis eligendi laboriosam illo nostrum corporis at libero vel voluptas? Expedita, facere dolores voluptatem ad ab rem assumenda soluta!
This is a link and how it could look like bestlinkinthebeautifulworld. Obcaecati, iste distinctio veritatis eligendi laboriosam illo nostrum corporis at libero vel voluptas? Expedita, facere dolores voluptatem ad ab rem assumenda soluta!
Here's just some classic bold text adipisicing elit. Obcaecati, iste distinctio veritatis eligendi laboriosam notBoldSecondbestlinkinthebeautifulworld illo nostrum corporis at libero vel voluptas? Expedita, facere dolores voluptatem ad ab rem assumenda soluta!
Obcaecati, iste distinctio veritatis eligendi laboriosam adipisicing elit illo nostrum corporis at adipisicing elit libero vel voluptas? Expedita, adipisicing facere dolores voluptatem ad ab rem assumenda soluta!
Other cuple of colors in case we want to emphasize several ways adipisicing elit. Obcaecati, iste distinctio veritatis eligendi laboriosam adipisicing elit illo nostrum corporis at voluptatem libero vel voluptas? Expedita, facere dolores voluptatem ad ab rem assumenda soluta!
Lorem ipsum dolor sit amet consectetur adipisicing elit. Obcaecati, iste distinctio veritatis eligendi laboriosam illo nostrum corporis at libero vel voluptas? Expedita, facere dolores voluptatem ad ab rem assumenda soluta! Lorem ipsum dolor, sit amet consectetur adipisicing elit. Quod veniam, quam ad expedita laborum sed at voluptates culpa ipsam ut vel. Ullam temporibus a mollitia quod aliquam ratione exercitationem nesciunt.
Lorem ipsum dolor sit amet consectetur adipisicing elit. Obcaecati, iste distinctio veritatis eligendi laboriosam illo nostrum corporis at libero vel voluptas? Expedita, facere dolores voluptatem ad ab rem assumenda soluta! Lorem ipsum dolor, sit amet consectetur adipisicing elit. Quod veniam, quam ad expedita laborum sed at voluptates culpa ipsam ut vel. Ullam temporibus a mollitia quod aliquam ratione exercitationem nesciunt.
Lorem ipsum dolor sit amet consectetur adipisicing elit. Obcaecati, iste distinctio veritatis eligendi laboriosam illo nostrum corporis at libero vel voluptas? Expedita, facere dolores voluptatem ad ab rem assumenda soluta!
Lorem ipsum dolor sit amet consectetur adipisicing elit. Repudiandae quas consequuntur illo numquam assumenda autem exercitationem distinctio perspiciatis in natus. Eius dicta similique ipsam ipsa minima, nemo quae enim tempore.
GPAL
.CallIfNotFound(GenericCallIfNotFound)
.WithPublishToConsole();
//System.Drawing.Rectangle windowSize = new System.Drawing.Rectangle(10, 10, 1500, 1024);
// NOTE: we have to set browser = before we execute any steps
// this is due to the 'GenericCallIfNotFound' which might throw an exception, and BankScraper will not have the browser set when it calls scraper.Close()
// until the complete fluent line gets executed (meaning every step, meaning browser is not set until everything else succeeds)
browser = GPAL.Browser
.WithBrowserType(Enums.BrowserType.Chrome)
.WithProfileDataDirectory(ChromeProfileLocation)
.WithUseAutomationEngine(AutomationEngine.Selenium)
.WithWindowSize(new System.Drawing.Rectangle(0,0,1920,1080))
.ToGPALObject();