2020-03-17 23:03:02 +08:00
|
|
|
using System;
|
2020-03-20 18:47:57 +08:00
|
|
|
using System.Diagnostics;
|
2020-03-20 00:25:51 +08:00
|
|
|
using System.IO;
|
2020-03-17 23:03:02 +08:00
|
|
|
using System.Threading;
|
|
|
|
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
2020-03-20 00:25:51 +08:00
|
|
|
using OpenQA.Selenium;
|
2020-03-17 23:03:02 +08:00
|
|
|
using OpenQA.Selenium.Appium;
|
|
|
|
using OpenQA.Selenium.Appium.Windows;
|
2020-03-20 00:25:51 +08:00
|
|
|
using OpenQA.Selenium.Interactions;
|
2020-03-17 23:03:02 +08:00
|
|
|
|
|
|
|
namespace PowerToysTests
|
|
|
|
{
|
|
|
|
public class PowerToysSession
|
|
|
|
{
|
|
|
|
protected const string WindowsApplicationDriverUrl = "http://127.0.0.1:4723";
|
2020-04-10 23:52:16 +08:00
|
|
|
protected const string AppPath = "C:\\Program Files\\PowerToys\\PowerToys.exe";
|
|
|
|
|
2020-03-17 23:03:02 +08:00
|
|
|
protected static WindowsDriver<WindowsElement> session;
|
|
|
|
protected static bool isPowerToysLaunched = false;
|
|
|
|
protected static WindowsElement trayButton;
|
|
|
|
|
2020-04-01 22:05:01 +08:00
|
|
|
protected static string _settingsFolderPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Microsoft\\PowerToys\\FancyZones");
|
|
|
|
protected static string _settingsPath = _settingsFolderPath + "\\settings.json";
|
|
|
|
protected static string _zoneSettingsPath = _settingsFolderPath + "\\zones-settings.json";
|
|
|
|
|
2020-03-20 00:25:51 +08:00
|
|
|
protected static string _initialSettings = "";
|
|
|
|
protected static string _initialZoneSettings = "";
|
|
|
|
|
2020-04-01 22:05:01 +08:00
|
|
|
protected const string _defaultSettings = "{\"version\":\"1.0\",\"name\":\"FancyZones\",\"properties\":{\"fancyzones_shiftDrag\":{\"value\":true},\"fancyzones_overrideSnapHotkeys\":{\"value\":false},\"fancyzones_zoneSetChange_flashZones\":{\"value\":false},\"fancyzones_displayChange_moveWindows\":{\"value\":false},\"fancyzones_zoneSetChange_moveWindows\":{\"value\":false},\"fancyzones_virtualDesktopChange_moveWindows\":{\"value\":false},\"fancyzones_appLastZone_moveWindows\":{\"value\":false},\"use_cursorpos_editor_startupscreen\":{\"value\":true},\"fancyzones_zoneHighlightColor\":{\"value\":\"#0078D7\"},\"fancyzones_highlight_opacity\":{\"value\":90},\"fancyzones_editor_hotkey\":{\"value\":{\"win\":true,\"ctrl\":false,\"alt\":false,\"shift\":false,\"code\":192,\"key\":\"`\"}},\"fancyzones_excluded_apps\":{\"value\":\"\"}}}";
|
|
|
|
protected const string _defaultZoneSettings = "{\"app-zone-history\":[],\"devices\":[],\"custom-zone-sets\":[]}";
|
2020-03-20 00:25:51 +08:00
|
|
|
|
|
|
|
|
2020-04-01 22:05:01 +08:00
|
|
|
public static void Setup(TestContext context, bool isLaunchRequired = true)
|
|
|
|
{
|
|
|
|
ReadUserSettings(); //read settings before running tests to restore them after
|
|
|
|
|
2020-03-17 23:03:02 +08:00
|
|
|
if (session == null)
|
|
|
|
{
|
|
|
|
// Create a new Desktop session to use PowerToys.
|
|
|
|
AppiumOptions appiumOptions = new AppiumOptions();
|
|
|
|
appiumOptions.PlatformName = "Windows";
|
|
|
|
appiumOptions.AddAdditionalCapability("app", "Root");
|
|
|
|
session = new WindowsDriver<WindowsElement>(new Uri(WindowsApplicationDriverUrl), appiumOptions);
|
|
|
|
Assert.IsNotNull(session);
|
|
|
|
|
|
|
|
trayButton = session.FindElementByAccessibilityId("1502");
|
|
|
|
|
|
|
|
isPowerToysLaunched = CheckPowerToysLaunched();
|
2020-03-20 00:25:51 +08:00
|
|
|
if (!isPowerToysLaunched && isLaunchRequired)
|
2020-03-17 23:03:02 +08:00
|
|
|
{
|
|
|
|
LaunchPowerToys();
|
|
|
|
}
|
2020-04-01 22:05:01 +08:00
|
|
|
}
|
2020-03-17 23:03:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public static void TearDown()
|
2020-04-01 22:05:01 +08:00
|
|
|
{
|
|
|
|
RestoreUserSettings(); //restore initial settings files
|
2020-03-20 00:25:51 +08:00
|
|
|
|
|
|
|
if (session != null)
|
2020-03-17 23:03:02 +08:00
|
|
|
{
|
|
|
|
session.Quit();
|
|
|
|
session = null;
|
2020-03-20 00:25:51 +08:00
|
|
|
}
|
2020-04-01 22:05:01 +08:00
|
|
|
}
|
|
|
|
|
2020-03-20 18:47:57 +08:00
|
|
|
public static void WaitSeconds(double seconds)
|
2020-03-20 00:25:51 +08:00
|
|
|
{
|
|
|
|
Thread.Sleep(TimeSpan.FromSeconds(seconds));
|
2020-03-17 23:03:02 +08:00
|
|
|
}
|
2020-03-20 18:47:57 +08:00
|
|
|
|
|
|
|
//Trying to find element by XPath
|
2020-04-10 23:52:16 +08:00
|
|
|
protected static WindowsElement WaitElementByName(string name, double maxTime = 10)
|
2020-03-20 18:47:57 +08:00
|
|
|
{
|
|
|
|
WindowsElement result = null;
|
|
|
|
Stopwatch timer = new Stopwatch();
|
|
|
|
timer.Start();
|
|
|
|
while (timer.Elapsed < TimeSpan.FromSeconds(maxTime))
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2020-04-10 23:52:16 +08:00
|
|
|
result = session.FindElementByName(name);
|
2020-03-20 18:47:57 +08:00
|
|
|
}
|
|
|
|
catch { }
|
2020-04-10 23:52:16 +08:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Trying to find element by XPath
|
|
|
|
protected static WindowsElement WaitElementByXPath(string xPath, double maxTime = 10)
|
|
|
|
{
|
|
|
|
WindowsElement result = null;
|
|
|
|
Stopwatch timer = new Stopwatch();
|
|
|
|
timer.Start();
|
|
|
|
while (timer.Elapsed < TimeSpan.FromSeconds(maxTime))
|
|
|
|
{
|
|
|
|
try
|
2020-03-20 18:47:57 +08:00
|
|
|
{
|
2020-04-10 23:52:16 +08:00
|
|
|
result = session.FindElementByXPath(xPath);
|
2020-03-20 18:47:57 +08:00
|
|
|
}
|
2020-04-10 23:52:16 +08:00
|
|
|
catch { }
|
|
|
|
return result;
|
2020-03-20 18:47:57 +08:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Trying to find element by AccessibilityId
|
2020-04-10 23:52:16 +08:00
|
|
|
protected static WindowsElement WaitElementByAccessibilityId(string accessibilityId, double maxTime = 10)
|
2020-03-20 18:47:57 +08:00
|
|
|
{
|
|
|
|
WindowsElement result = null;
|
|
|
|
Stopwatch timer = new Stopwatch();
|
|
|
|
timer.Start();
|
|
|
|
while (timer.Elapsed < TimeSpan.FromSeconds(maxTime))
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
result = session.FindElementByAccessibilityId(accessibilityId);
|
|
|
|
}
|
|
|
|
catch { }
|
2020-04-10 23:52:16 +08:00
|
|
|
return result;
|
2020-03-20 18:47:57 +08:00
|
|
|
}
|
|
|
|
return null;
|
2020-03-17 23:03:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public static void OpenSettings()
|
|
|
|
{
|
|
|
|
trayButton.Click();
|
2020-03-20 00:25:51 +08:00
|
|
|
session.FindElementByXPath("//Button[@Name=\"PowerToys\"]").Click();
|
2020-03-17 23:03:02 +08:00
|
|
|
trayButton.Click();
|
|
|
|
}
|
|
|
|
|
2020-03-20 00:25:51 +08:00
|
|
|
public static void OpenFancyZonesSettings()
|
|
|
|
{
|
2020-04-10 23:52:16 +08:00
|
|
|
WindowsElement fzNavigationButton = WaitElementByXPath("//Button[@Name=\"FancyZones\"]");
|
2020-03-20 00:25:51 +08:00
|
|
|
Assert.IsNotNull(fzNavigationButton);
|
|
|
|
|
|
|
|
fzNavigationButton.Click();
|
|
|
|
fzNavigationButton.Click();
|
|
|
|
}
|
|
|
|
|
2020-03-17 23:03:02 +08:00
|
|
|
public static void CloseSettings()
|
|
|
|
{
|
2020-03-20 00:25:51 +08:00
|
|
|
try
|
|
|
|
{
|
|
|
|
WindowsElement settings = session.FindElementByName("PowerToys Settings");
|
|
|
|
if (settings != null)
|
|
|
|
{
|
|
|
|
settings.SendKeys(Keys.Alt + Keys.F4);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch(Exception)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
2020-03-17 23:03:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
private static bool CheckPowerToysLaunched()
|
|
|
|
{
|
|
|
|
trayButton.Click();
|
|
|
|
bool isLaunched = false;
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2020-04-10 23:52:16 +08:00
|
|
|
WindowsElement pt = WaitElementByXPath("//Button[@Name=\"PowerToys\"]");
|
2020-03-17 23:03:02 +08:00
|
|
|
isLaunched = (pt != null);
|
|
|
|
}
|
|
|
|
catch(OpenQA.Selenium.WebDriverException)
|
|
|
|
{
|
|
|
|
//PowerToys not found
|
|
|
|
}
|
|
|
|
|
|
|
|
trayButton.Click(); //close
|
|
|
|
return isLaunched;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void LaunchPowerToys()
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
AppiumOptions opts = new AppiumOptions();
|
2020-04-10 23:52:16 +08:00
|
|
|
opts.PlatformName = "Windows";
|
|
|
|
opts.AddAdditionalCapability("app", AppPath);
|
|
|
|
|
2020-03-17 23:03:02 +08:00
|
|
|
WindowsDriver<WindowsElement> driver = new WindowsDriver<WindowsElement>(new Uri(WindowsApplicationDriverUrl), opts);
|
|
|
|
Assert.IsNotNull(driver);
|
|
|
|
driver.LaunchApp();
|
|
|
|
}
|
2020-03-24 22:27:22 +08:00
|
|
|
catch (OpenQA.Selenium.WebDriverException ex)
|
2020-03-17 23:03:02 +08:00
|
|
|
{
|
2020-03-24 22:27:22 +08:00
|
|
|
Console.WriteLine("Exception on PowerToys launch:" + ex.Message);
|
2020-03-17 23:03:02 +08:00
|
|
|
//exception could be thrown even if app launched successfully
|
2020-03-24 22:27:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
isPowerToysLaunched = true;
|
2020-03-17 23:03:02 +08:00
|
|
|
}
|
2020-03-20 00:25:51 +08:00
|
|
|
|
|
|
|
public static void ExitPowerToys()
|
|
|
|
{
|
|
|
|
trayButton.Click();
|
|
|
|
|
2020-04-10 23:52:16 +08:00
|
|
|
WindowsElement pt = WaitElementByXPath("//Button[@Name=\"PowerToys\"]");
|
|
|
|
Assert.IsNotNull(pt, "Couldn't find \'PowerToys\' button");
|
2020-03-20 00:25:51 +08:00
|
|
|
new Actions(session).MoveToElement(pt).ContextClick().Perform();
|
2020-04-10 23:52:16 +08:00
|
|
|
|
|
|
|
WaitElementByXPath("//MenuItem[@Name=\"Exit\"]").Click();
|
2020-03-20 00:25:51 +08:00
|
|
|
trayButton.Click(); //close tray
|
2020-03-21 21:35:23 +08:00
|
|
|
isPowerToysLaunched = false;
|
2020-03-24 16:32:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public static void ResetDefaultFancyZonesSettings(bool relaunch)
|
|
|
|
{
|
2020-04-01 22:05:01 +08:00
|
|
|
ResetSettings(_settingsFolderPath, _settingsPath, _defaultSettings, relaunch);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void ResetDefautZoneSettings(bool relaunch)
|
|
|
|
{
|
|
|
|
ResetSettings(_settingsFolderPath, _zoneSettingsPath, _defaultZoneSettings, relaunch);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void ResetSettings(string folder, string filePath, string data, bool relaunch)
|
|
|
|
{
|
|
|
|
if (!Directory.Exists(folder))
|
2020-03-24 16:32:12 +08:00
|
|
|
{
|
2020-04-01 22:05:01 +08:00
|
|
|
Directory.CreateDirectory(folder);
|
2020-03-24 16:32:12 +08:00
|
|
|
}
|
2020-04-01 22:05:01 +08:00
|
|
|
File.WriteAllText(filePath, data);
|
2020-03-24 16:32:12 +08:00
|
|
|
|
|
|
|
if (isPowerToysLaunched)
|
|
|
|
{
|
|
|
|
ExitPowerToys();
|
|
|
|
}
|
2020-04-01 22:05:01 +08:00
|
|
|
|
2020-03-24 16:32:12 +08:00
|
|
|
if (relaunch)
|
|
|
|
{
|
|
|
|
LaunchPowerToys();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-01 22:05:01 +08:00
|
|
|
private static void ReadUserSettings()
|
2020-03-24 16:32:12 +08:00
|
|
|
{
|
2020-04-01 22:05:01 +08:00
|
|
|
try
|
|
|
|
{
|
|
|
|
_initialSettings = File.ReadAllText(_settingsPath);
|
|
|
|
}
|
|
|
|
catch (Exception)
|
|
|
|
{
|
|
|
|
//failed to read settings
|
|
|
|
}
|
2020-03-24 16:32:12 +08:00
|
|
|
|
2020-04-01 22:05:01 +08:00
|
|
|
try
|
2020-03-31 20:15:51 +08:00
|
|
|
{
|
2020-04-01 22:05:01 +08:00
|
|
|
_initialZoneSettings = File.ReadAllText(_zoneSettingsPath);
|
2020-03-31 20:15:51 +08:00
|
|
|
}
|
2020-04-01 22:05:01 +08:00
|
|
|
catch (Exception)
|
2020-03-24 16:32:12 +08:00
|
|
|
{
|
2020-04-01 22:05:01 +08:00
|
|
|
//failed to read settings
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void RestoreUserSettings()
|
|
|
|
{
|
|
|
|
if (_initialSettings.Length > 0)
|
|
|
|
{
|
|
|
|
File.WriteAllText(_settingsPath, _initialSettings);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
File.Delete(_settingsPath);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_initialZoneSettings.Length > 0)
|
|
|
|
{
|
|
|
|
File.WriteAllText(_zoneSettingsPath, _initialZoneSettings);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
File.Delete(_zoneSettingsPath);
|
2020-03-24 16:32:12 +08:00
|
|
|
}
|
2020-03-20 00:25:51 +08:00
|
|
|
}
|
2020-03-17 23:03:02 +08:00
|
|
|
}
|
|
|
|
}
|