TestStack / White

DEPRECATED - no longer actively maintained
Other
1.02k stars 485 forks source link

How To Start Desktop Application's Automation Using TestStack.White in C# .net #657

Closed shshaakk closed 4 years ago

shshaakk commented 4 years ago

TestStack.White Automation Regarding

Launch new application: TestStack.White.Application application = TestStack.White.Application.Launch(tallyApplicationPath);

Launch new application with its working directory: (using System.Diagnostics;) ProcessStartInfo p = new ProcessStartInfo(applicationPath); p.WorkingDirectory = applicationWorkingDirectoryPath; TestStack.White.Application application = TestStack.White.Application.Launch(p);

Connect with already launch application: TestStack.White.Application applicationOfvideoWindow = TestStack.White.Application.Attach("TallyVideoPlayer");

Set timeout: TestStack.White.Configuration.CoreAppXmlConfiguration.Instance.FindWindowTimeout = 10000; TestStack.White.Configuration.CoreAppXmlConfiguration.Instance.BusyTimeout = 300000000; // 5 Hours

Find window with name: (using System.Linq;) var getvideoWindow = applicationOfvideoWindow.GetWindows().Where(t => t.Title == GeneralDeclarations.TallyVideoWindowTitle).FirstOrDefault(); do { getvideoWindow = applicationOfvideoWindow.GetWindows().Where(t => t.Title == GeneralDeclarations.TallyVideoWindowTitle).FirstOrDefault(); } while (getvideoWindow == null); Window videoWindow = getvideoWindow;

Close window: videoWindow.Close();

Find all UIItems of a window: Window windowTransfer = getWindowTransfer; IUIItem[] windowTransferWindowAllUItems = windowTransfer.GetMultiple(TestStack.White.UIItems.Finders.SearchCriteria.All);

Find,set and click a UIItem of a window:

IUIItem btnNo = null; btnNo = windowTransferWindowAllUItems.Where(t => t.Id == "No").FirstOrDefault(); btnNo.Click();

Find,set and click a comboBox of a window:

mainWindow = application.GetWindows().Where(t => t.Title == GeneralDeclarations.TallyMainWindowTitle).FirstOrDefault(); TestStack.White.UIItems.ListBoxItems.ComboBox SelcallcmbBox = mainWindow.Get(SearchCriteria.ByAutomationId("23945")); SelcallcmbBox.Focus(); var selectAllcmbItem = SelcallcmbBox.Items.Where(t => t.Name == "All Tax Returns").FirstOrDefault(); selectAllcmbItem.Select(); selectAllcmbItem.Click();

Find,set and click a MenuBar’s menu item of a window:

MenuBar menuBar = mainWindow.MenuBar; Thread.Sleep(1000);

            TestStack.White.UIItems.MenuItems.Menu level2Menu = menuBar.MenuItem("File", "Client File Maintenance", "Restore...");
            level2Menu.Click();

Set Keyboard for particular window and Press Special Key one time:

TestStack.White.InputDevices.AttachedKeyboard mainKeyboard = mainWindow.Keyboard; mainKeyboard.PressSpecialKey(TestStack.White.WindowsAPI.KeyboardInput.SpecialKeys.TAB);

Set Keyboard for particular window and Press Ctrl + P one time:

mainKeyboard.HoldKey(TestStack.White.WindowsAPI.KeyboardInput.SpecialKeys.CONTROL); mainKeyboard.Enter("P"); mainKeyboard.LeaveKey(TestStack.White.WindowsAPI.KeyboardInput.SpecialKeys.CONTROL);

Key Press for entire PC:

Desktop.Instance.Keyboard.PressSpecialKey(TestStack.White.WindowsAPI.KeyboardInput.SpecialKeys.RETURN);

Get Checkbox and Select and Unselect it:

Window printCenterWindow = mainWindow.ModalWindow(GeneralDeclarations.TallyPrintCenterWindowTitle); IUIItem[] printCenterWindowAllUIItems = printCenterWindow.GetMultiple(TestStack.White.UIItems.Finders.SearchCriteria.All);

            IUIItem filingCopyBtn = printCenterWindowAllUIItems.Where(t => t.Name == "Filing Copy").FirstOrDefault();
            TestStack.White.UIItems.CheckBox filingCopyCheckBox = new TestStack.White.UIItems.CheckBox(filingCopyBtn.AutomationElement, printCenterWindow.ActionListener);
            filingCopyCheckBox.UnSelect();

filingCopyCheckBox.Select();

Browse a folder using path given path:

Window browseForFolderWindow = getBrowseForFolderWindow; IUIItem[] browseForFolderWindowAllUIItems = browseForFolderWindow.GetMultiple(TestStack.White.UIItems.Finders.SearchCriteria.All); TestStack.White.UIItems.TreeItems.Tree selectPathTree = (TestStack.White.UIItems.TreeItems.Tree)browseForFolderWindowAllUIItems.Where(t => t.Id == "100").FirstOrDefault();

                    String[] breakApart = localPath.Split('\\');

                    breakApart = breakApart.Where(t => t != "").Select(t => t).ToArray();

                    Common.WriteLog(GeneralDeclarations.LoggingPath, "loop on selectPathTree.Nodes Start.q", GeneralDeclarations.LoggingEnabled, true);
                    foreach (TestStack.White.UIItems.TreeItems.TreeNode node in selectPathTree.Nodes)
                    {
                        if (node.Name == "Desktop")
                        {
                            IUIItem thisPCTreeNode = node.Nodes.Where(t => t.Name == "This PC").FirstOrDefault();
                            thisPCTreeNode.Click();
                            Thread.Sleep(50); //For Expanding This PC tree node.
                            TestStack.White.UIItems.TreeItems.TreeNode thisPCTree = (TestStack.White.UIItems.TreeItems.TreeNode)thisPCTreeNode;
                            foreach (TestStack.White.UIItems.TreeItems.TreeNode innerNode in thisPCTree.Nodes)
                            {
                                if (innerNode.Name == "Local Disk (" + breakApart[0] + ')')
                                {
                                    IUIItem tempUIItem = (IUIItem)innerNode;
                                    for (int sd = 0; sd < breakApart.Length - 1; sd++)
                                    {
                                        IUIItem newUIItem = getSubDirAndClickIt(breakApart[sd], breakApart[sd + 1], tempUIItem);
                                        tempUIItem = newUIItem;
                                    }
                                    break;
                                }
                            }
                        }
                    }
                    Common.WriteLog(GeneralDeclarations.LoggingPath, "loop on selectPathTree.Nodes Finished.q", GeneralDeclarations.LoggingEnabled, true);

                    IUIItem oKBtn = browseForFolderWindowAllUIItems.Where(t => t.Name == "OK").FirstOrDefault();

Click an element dynamically using another elements location:

WriteLog(GeneralDeclarations.LoggingPath, "rfreshBtn getting start." + Environment.NewLine, GeneralDeclarations.LoggingEnabled, true); IUIItem rfreshBtn = mainWindowAllUItems.Where(t => t.Id == "Item 41476").FirstOrDefault(); WriteLog(GeneralDeclarations.LoggingPath, "rfreshBtn getting done." + Environment.NewLine, GeneralDeclarations.LoggingEnabled, true);

                            WriteLog(GeneralDeclarations.LoggingPath, "rfreshBtn location getting start." + Environment.NewLine, GeneralDeclarations.LoggingEnabled, true);
                            var return7MenuXLocation = (((int)rfreshBtn.Bounds.Left + (int)rfreshBtn.Bounds.Right) / 2);
                            var return7MenuYLocation = rfreshBtn.Bounds.Width;

                            TestStack.White.InputDevices.Mouse.Instance.Location = new Point((int)return7MenuXLocation, (int)return7MenuYLocation);
                            WriteLog(GeneralDeclarations.LoggingPath, "rfreshBtn location getting done." + Environment.NewLine, GeneralDeclarations.LoggingEnabled, true);

                            TestStack.White.InputDevices.Mouse.MouseLeftButtonUpAndDown();

Useful C# .net laibraries for automation:

using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using TestStack.White; using TestStack.White.Factory; using TestStack.White.UIItems; using TestStack.White.UIItems.Finders; using TestStack.White.UIItems.WindowItems; using TestStack.White.UIItems.WindowStripControls; using TestStack.White.InputDevices; using System.ServiceProcess; using System.Windows.Automation; using System.Windows; using TestStack.White.Utility; using System.Windows.Forms; using System.Text.RegularExpressions; using System.Configuration; using TestStack.White.Configuration; using SharpCompress.Common; using TestStack.White.WindowsAPI;

megaspaz commented 4 years ago

Is there anything special that needs to be done if on win10 using .NET 4.7? I'm trying a simple POC and I'm hitting File not found error.

Unhandled exception. System.IO.FileNotFoundException: Could not load file or assembly 'System.Configuration.ConfigurationManager, Version=4.0.2.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'. The system cannot find the file specified.
File name: 'System.Configuration.ConfigurationManager, Version=4.0.2.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'
   at TestStack.White.Application.Launch(ProcessStartInfo processStartInfo)
   at TestStack.White.Application.AttachOrLaunch(ProcessStartInfo processStartInfo)
   at ConsoleApp1.Program.Main(String[] args) in C:\Users\vincent-j\Documents\poc\poc2\ConsoleApp1\Program.cs:line 21

The code is what I'm trying to do, following what I've seen online...

using TestStack.White;
using TestStack.White.Factory;
using TestStack.White.UIItems.Finders;
using TestStack.White.InputDevices;

namespace ConsoleApp1
{
    class Program
    {
        private const string ExeSourceFile = @"C:\Windows\System32\calc.exe";
        //Global Variable to for Application launch
        private static TestStack.White.Application _application;
        //Global variable to get the Main window of calculator from application.
        private static TestStack.White.UIItems.WindowItems.Window _mainWindow;

        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo(ExeSourceFile);
            TestStack.White.Application _application = TestStack.White.Application.AttachOrLaunch(psi);
        }
    }
}
shshaakk commented 4 years ago

Hi megaspaz,

Try Below code I think it will help you:

using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using TestStack.White; using TestStack.White.Factory; using TestStack.White.UIItems; using TestStack.White.UIItems.Finders; using TestStack.White.UIItems.WindowItems; using TestStack.White.UIItems.WindowStripControls; using TestStack.White.InputDevices; using System.ServiceProcess; using System.Windows.Automation; using System.Windows; using TestStack.White.Utility; using System.Windows.Forms; using System.Text.RegularExpressions; using System.Configuration; using TestStack.White.Configuration; using SharpCompress.Common; using TestStack.White.WindowsAPI;

public static void sample() { string ExeSourceFile = @"C:\Windows\System32\calc.exe"; //Global Variable to for Application launch TestStack.White.Application _application; //Global variable to get the Main window of calculator from application. TestStack.White.UIItems.WindowItems.Window _mainWindow;

        Console.WriteLine("Hello World!");
        _application = TestStack.White.Application.Launch(ExeSourceFile);
    }