How to handle multiple windows in FlaUI desktop automation-outlook

3.4k views Asked by At

I am new to desktop automation and FLaUI.I am using visual studio I am testing a plugin for Outlook. So I am able to click on New Email and it opens a new window for ne email compose. Now I want to switch to that window from my main window. How can I do that ? My current code is :

using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using FlaUI.Core;
using FlaUI.UIA3;
using FlaUI.Core.Conditions;
using FlaUI.Core.AutomationElements;
using System.Threading;
using FlaUI.Core.Tools;
using System.Diagnostics;

namespace Test_Project_for_SCDP
{
    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void TestMethod1()
        {
           
            Process p = new Process();
            p.StartInfo.UseShellExecute = true;
            p.StartInfo.FileName = ("Outlook.exe");
            p.Start();
            System.Threading.Thread.Sleep(5000);
            p.OutputDataReceived += (obj, args) => Console.WriteLine(args.Data);   
            var application = Application.Attach(p);
            var window = application.GetMainWindow(new UIA3Automation());
            Console.WriteLine("window is " + window.Title);
            
            ConditionFactory cf = new ConditionFactory(new UIA3PropertyLibrary());
            
            window.FindFirstDescendant(cf.ByName("New Email")).AsButton().Click();
            System.Threading.Thread.Sleep(5000);
            var window_child = application.GetMainWindow(new UIA3Automation());
            Console.WriteLine("new window is " + window_child.Title);
                       window_child.FindFirstDescendant(cf.ByName("To")).AsTextBox().Enter("abcd");
            
            //window.FindFirstDescendant(cf.ByName("Close")).AsButton().Click();
            Console.WriteLine("Clicked and entered");



        }
    }
}

1

There are 1 answers

2
ojonasplima On BEST ANSWER

As the "New Email" is a new window with the same Process Id and not a child of you first one, you might wanna do something like this:

var app = FlaUI.Core.Application.Attach(process.Id);
                using (var automation = new UIA3Automation())
                {
                    var window = app.GetMainWindow(automation);
                    Console.WriteLine("window is " + window.Title);
                    ConditionFactory cf = new ConditionFactory(new UIA3PropertyLibrary());
                    var button1 = window.FindFirstDescendant(cf => cf.ByText("New Email"))?.AsButton();
                    button1?.Invoke();

                    var newWindow = app.GetAllTopLevelWindows(automation)[0];
                    //Do something with the newWindow variable
                }
  • Process.Id is a property of the Process you started, that's one of the ways to attach the process to the Automation object.
  • Instead of using Click(), use Invoke() as instructed by the documentation;
  • newWindow is a list of windows, so you can directly get the window you want by passing the index value [0] or whatever index you want.