我是桌面自动化和FLaUI.I的新手。我使用的是visual,我正在测试Outlook的插件。因此,我可以点击新的电子邮件,它打开一个新的窗口,为新的电子邮件撰写。现在我想从我的主窗口切换到那个窗口。我怎么能这么做?我目前的代码是:
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");
}
}
}
发布于 2022-03-22 12:26:51
由于“新建电子邮件”是一个具有相同Process
Id的新窗口,而不是您的第一个子窗口,您可能需要这样做:
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
是您启动的进程的属性,这是将进程附加到Automation
对象的方法之一。Click()
,而是按照文档的指示使用Invoke()
;newWindow
是一个窗口列表,因此您可以通过传递索引值或任何您想要的索引来直接获取所需的窗口。https://stackoverflow.com/questions/71558065
复制相似问题