// Copyright (c) Microsoft Corporation // The Microsoft Corporation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. using System; using System.IO; using System.Reflection; using Microsoft.VisualStudio.TestTools.UnitTesting; using OpenQA.Selenium; using OpenQA.Selenium.Appium; using OpenQA.Selenium.Appium.Windows; namespace Microsoft.FancyZonesEditor.UnitTests.Utils { public class FancyZonesEditorSession { protected const string WindowsApplicationDriverUrl = "http://127.0.0.1:4723"; private const string FancyZonesEditorPath = @"\..\..\..\PowerToys.FancyZonesEditor.exe"; public WindowsDriver? Session { get; } public WindowsElement? MainEditorWindow { get; } public FancyZonesEditorSession(TestContext testContext) { try { // Launch FancyZonesEditor string? path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); path += FancyZonesEditorPath; AppiumOptions opts = new AppiumOptions(); opts.AddAdditionalCapability("app", path); Session = new WindowsDriver(new Uri(WindowsApplicationDriverUrl), opts); } catch (Exception ex) { testContext.WriteLine(ex.Message); } Assert.IsNotNull(Session, "Session not initialized"); // Set implicit timeout to 1.5 seconds to make element search to retry every 500 ms for at most three times Session.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(1.5); // Find main editor window MainEditorWindow = Session.FindElementByAccessibilityId("MainWindow1"); Assert.IsNotNull(MainEditorWindow, "Main editor window not found"); } public void Close(TestContext testContext) { // Close the session if (Session != null) { try { // FZEditor applcation can be closed by explicitly closing main editor window MainEditorWindow?.SendKeys(Keys.Alt + Keys.F4); } catch (Exception ex) { testContext.WriteLine(ex.Message); } Session.Quit(); Session.Dispose(); } } } }