From 35054c1f590b4fc67e48a6c6b5fcdd4e3da99a8c Mon Sep 17 00:00:00 2001 From: Seraphima Zykova Date: Thu, 19 Mar 2020 19:25:51 +0300 Subject: [PATCH] Editor opening tests (#1633) * editor opening tests * moved fancy zones opening method * moved settings restore to base class * changed launch * fixed tray button search * added exit function --- .../FancyZonesTests/EditorOpeningTests.cs | 293 ++++++++++++++++++ .../FancyZonesSettingsTests.cs | 88 ++---- .../win-app-driver/FancyZonesTests/README.md | 64 ++-- src/tests/win-app-driver/PowerToysSession.cs | 99 +++++- .../win-app-driver/win-app-driver.csproj | 3 +- 5 files changed, 446 insertions(+), 101 deletions(-) create mode 100644 src/tests/win-app-driver/FancyZonesTests/EditorOpeningTests.cs diff --git a/src/tests/win-app-driver/FancyZonesTests/EditorOpeningTests.cs b/src/tests/win-app-driver/FancyZonesTests/EditorOpeningTests.cs new file mode 100644 index 000000000..469b948e4 --- /dev/null +++ b/src/tests/win-app-driver/FancyZonesTests/EditorOpeningTests.cs @@ -0,0 +1,293 @@ +using System.IO; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using OpenQA.Selenium.Appium.Windows; +using OpenQA.Selenium.Interactions; + +namespace PowerToysTests +{ + [TestClass] + public class FancyZonesEditorOpeningTests : PowerToysSession + { + WindowsElement editorWindow; + + static void ResetDefaultFancyZonesSettings() + { + if (!Directory.Exists(_settingsFolderPath)) + { + Directory.CreateDirectory(_settingsFolderPath); + } + + string settings = "{\"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\":\"\"}}}"; + File.WriteAllText(_settingsPath, settings); + } + + void RemoveSettingsFile() + { + File.Delete(_zoneSettingsPath); + } + + void RemoveSettingsFolder() + { + Directory.Delete(_settingsFolderPath, true); + } + + void CreateEmptySettingsFile() + { + string zoneSettings = ""; + File.WriteAllText(_zoneSettingsPath, zoneSettings); + } + + void CreateDefaultSettingsFile() + { + string zoneSettings = "{\"app-zone-history\":[],\"devices\":[],\"custom-zone-sets\":[]}"; + File.WriteAllText(_zoneSettingsPath, zoneSettings); + } + + void CreateValidSettingsFile() + { + string zoneSettings = "{\"app-zone-history\":[{\"app-path\":\"C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Community\\Common7\\IDE\\Extensions\\TestPlatform\\testhost.exe\",\"zone-index\":3,\"device-id\":\"DELA026#5&10a58c63&0&UID16777488_1920_1200_{39B25DD2-130D-4B5D-8851-4791D66B1539}\",\"zoneset-uuid\":\"{D13ABB6D-7721-4176-9647-C8C0836D99CC}\"}],\"devices\":[{\"device-id\":\"DELA026#5&10a58c63&0&UID16777488_1920_1200_{39B25DD2-130D-4B5D-8851-4791D66B1539}\",\"active-zoneset\":{\"uuid\":\"{D13ABB6D-7721-4176-9647-C8C0836D99CC}\",\"type\":\"columns\"},\"editor-show-spacing\":true,\"editor-spacing\":16,\"editor-zone-count\":3}],\"custom-zone-sets\":[]}"; + File.WriteAllText(_zoneSettingsPath, zoneSettings); + } + + void CreateValidSettingsFileWithUtf8() + { + string zoneSettings = "{\"app-zone-history\":[{\"app-path\":\"C:\\Program Files (x86)\\йцукен\\testhost.exe\",\"zone-index\":3,\"device-id\":\"DELA026#5&10a58c63&0&UID16777488_1920_1200_{39B25DD2-130D-4B5D-8851-4791D66B1539}\",\"zoneset-uuid\":\"{D13ABB6D-7721-4176-9647-C8C0836D99CC}\"}],\"devices\":[{\"device-id\":\"DELA026#5&10a58c63&0&UID16777488_1920_1200_{39B25DD2-130D-4B5D-8851-4791D66B1539}\",\"active-zoneset\":{\"uuid\":\"{D13ABB6D-7721-4176-9647-C8C0836D99CC}\",\"type\":\"columns\"},\"editor-show-spacing\":true,\"editor-spacing\":16,\"editor-zone-count\":3}],\"custom-zone-sets\":[]}"; + File.WriteAllText(_zoneSettingsPath, zoneSettings); + } + + void CreateInvalidSettingsFile() + { + string zoneSettings = "{\"app-zone-history\":[{\"app-path\":\"C:\\Program Files (x86)\\Microsoft Visual Studio\\testhost.exe\",\"zone-index\":3,\"device-id\":\"wrong-device-id\",\"zoneset-uuid\":\"{D13ABB6D-invalid-uuid-C8C0836D99CC}\"}],\"devices\":[{\"device-id\":\"DELA026#5&10a58c63&0&UID16777488_1920_1200_{39B25DD2-130D-4B5D-8851-4791D66B1539}\",\"active-zoneset\":{\"uuid\":\"{D13ABB6D-7721-4176-9647-C8C0836D99CC}\",\"type\":\"columns\"},\"editor-show-spacing\":true,\"editor-spacing\":16,\"editor-zone-count\":3}],\"custom-zone-sets\":[]}"; + File.WriteAllText(_zoneSettingsPath, zoneSettings); + } + + void CreateCroppedSettingsFile() + { + string zoneSettings = "{\"app-zone-history\":[],\"devices\":[],\"custom-zone-sets\":[{\"uuid\":\"{8BEC7183-C90E-4D41-AD1C-1AC2BC4760BA}\",\"name\":\""; + File.WriteAllText(_zoneSettingsPath, zoneSettings); + } + + void TestEditorOpened() + { + WindowsElement errorMessage = null; + try + { + errorMessage = session.FindElementByName("FancyZones Editor Exception Handler"); + if (errorMessage != null) + { + errorMessage.FindElementByName("OK").Click(); + ShortWait(); + } + } + catch (OpenQA.Selenium.WebDriverException) + { + //no error message, it's ok + } + + try + { + editorWindow = session.FindElementByXPath("//Window[@Name=\"FancyZones Editor\"]"); + } + catch (OpenQA.Selenium.WebDriverException) + { + } + + Assert.IsNotNull(editorWindow); + Assert.IsNull(errorMessage); + } + + void OpenEditorBySettingsButton() + { + OpenSettings(); + OpenFancyZonesSettings(); + + WindowsElement editorButton = session.FindElementByXPath("//Button[@Name=\"Edit zones\"]"); + Assert.IsNotNull(editorButton); + + editorButton.Click(); + ShortWait(); + + TestEditorOpened(); + } + + void OpenEditorByHotkey() + { + new Actions(session).KeyDown(OpenQA.Selenium.Keys.Command).SendKeys("`").KeyUp(OpenQA.Selenium.Keys.Command).Perform(); + ShortWait(); + + TestEditorOpened(); + } + + [TestMethod] + public void OpenEditorBySettingsButtonNoSettings() + { + RemoveSettingsFile(); + OpenEditorBySettingsButton(); + } + + [TestMethod] + public void OpenEditorBySettingsButtonNoSettingsFolder() + { + /* + if (isPowerToysLaunched) + { + ExitPowerToys(); + } + RemoveSettingsFolder(); + LaunchPowerToys(); + */ + + RemoveSettingsFolder(); + OpenEditorBySettingsButton(); + } + + [TestMethod] + public void OpenEditorBySettingsButtonEmptySettings() + { + CreateEmptySettingsFile(); + OpenEditorBySettingsButton(); + } + + [TestMethod] + public void OpenEditorBySettingsButtonDefaultSettings() + { + CreateDefaultSettingsFile(); + OpenEditorBySettingsButton(); + } + + [TestMethod] + public void OpenEditorBySettingsButtonValidSettings() + { + CreateValidSettingsFile(); + OpenEditorBySettingsButton(); + } + + [TestMethod] + public void OpenEditorBySettingsButtonValidUtf8Settings() + { + CreateValidSettingsFileWithUtf8(); + OpenEditorBySettingsButton(); + } + + [TestMethod] + public void OpenEditorBySettingsButtonInvalidSettings() + { + CreateInvalidSettingsFile(); + OpenEditorBySettingsButton(); + } + + [TestMethod] + public void OpenEditorBySettingsButtonCroppedSettings() + { + CreateCroppedSettingsFile(); + OpenEditorBySettingsButton(); + } + + [TestMethod] + public void OpenEditorByHotkeyNoSettings() + { + RemoveSettingsFile(); + OpenEditorByHotkey(); + } + + [TestMethod] + public void OpenEditorByHotkeyNoSettingsFolder() + { + /* + if (isPowerToysLaunched) + { + ExitPowerToys(); + } + RemoveSettingsFolder(); + LaunchPowerToys(); + */ + RemoveSettingsFolder(); + OpenEditorByHotkey(); + } + + [TestMethod] + public void OpenEditorByHotkeyEmptySettings() + { + CreateEmptySettingsFile(); + OpenEditorByHotkey(); + } + + [TestMethod] + public void OpenEditorByHotkeyDefaultSettings() + { + CreateDefaultSettingsFile(); + OpenEditorByHotkey(); + } + + [TestMethod] + public void OpenEditorByHotkeyValidSettings() + { + CreateValidSettingsFile(); + OpenEditorByHotkey(); + } + + [TestMethod] + public void OpenEditorByHotkeyValidUtf8Settings() + { + CreateValidSettingsFileWithUtf8(); + OpenEditorByHotkey(); + } + + [TestMethod] + public void OpenEditorByHotkeyInvalidSettings() + { + CreateInvalidSettingsFile(); + OpenEditorByHotkey(); + } + + [TestMethod] + public void OpenEditorByHotkeyCroppedSettings() + { + CreateCroppedSettingsFile(); + OpenEditorByHotkey(); + } + + [ClassInitialize] + public static void ClassInitialize(TestContext context) + { + Setup(context, false); + + if (isPowerToysLaunched) + { + ExitPowerToys(); + } + ResetDefaultFancyZonesSettings(); + LaunchPowerToys(); + } + + [ClassCleanup] + public static void ClassCleanup() + { + CloseSettings(); + ExitPowerToys(); + TearDown(); + } + + [TestInitialize] + public void TestInitialize() + { + + } + + [TestCleanup] + public void TestCleanup() + { + //Close editor + if (editorWindow != null) + { + editorWindow.SendKeys(OpenQA.Selenium.Keys.Alt + OpenQA.Selenium.Keys.F4); + ShortWait(); + } + + if (!Directory.Exists(_settingsFolderPath)) + { + Directory.CreateDirectory(_settingsFolderPath); + } + } + } +} \ No newline at end of file diff --git a/src/tests/win-app-driver/FancyZonesTests/FancyZonesSettingsTests.cs b/src/tests/win-app-driver/FancyZonesTests/FancyZonesSettingsTests.cs index 0fad86571..b7b4e7563 100644 --- a/src/tests/win-app-driver/FancyZonesTests/FancyZonesSettingsTests.cs +++ b/src/tests/win-app-driver/FancyZonesTests/FancyZonesSettingsTests.cs @@ -14,9 +14,7 @@ namespace PowerToysTests { [TestClass] public class FancyZonesSettingsTests : PowerToysSession - { - private static string _settingsPath = ""; - private string _initialSettings = ""; + { private JObject _initialSettingsJson; private static WindowsElement _saveButton; @@ -41,23 +39,19 @@ namespace PowerToysTests _scrollDown = new Actions(session).MoveToElement(_saveButton).MoveByOffset(0, _saveButton.Rect.Height).ContextClick() .SendKeys(OpenQA.Selenium.Keys.PageDown + OpenQA.Selenium.Keys.PageDown); Assert.IsNotNull(_scrollDown); - } - - private static void OpenFancyZonesSettings() - { - WindowsElement fzNavigationButton = session.FindElementByXPath("//Button[@Name=\"FancyZones\"]"); - Assert.IsNotNull(fzNavigationButton); - - fzNavigationButton.Click(); - fzNavigationButton.Click(); - - ShortWait(); - } - - private JObject getProperties() - { - JObject settings = JObject.Parse(File.ReadAllText(_settingsPath)); - return settings["properties"].ToObject(); + } + + private JObject getProperties() + { + try + { + JObject settings = JObject.Parse(File.ReadAllText(_settingsPath)); + return settings["properties"].ToObject(); + } + catch (Newtonsoft.Json.JsonReaderException) + { + return new JObject(); + } } private T getPropertyValue(string propertyName) @@ -237,19 +231,7 @@ namespace PowerToysTests WindowsElement fzTitle = session.FindElementByName("FancyZones Settings"); Assert.IsNotNull(fzTitle); } - /* - [TestMethod] - public void EditorOpen() - { - session.FindElementByXPath("//Button[@Name=\"Edit zones\"]").Click(); - ShortWait(); - WindowsElement editorWindow = session.FindElementByName("FancyZones Editor"); - Assert.IsNotNull(editorWindow); - - editorWindow.SendKeys(OpenQA.Selenium.Keys.Alt + OpenQA.Selenium.Keys.F4); - } - */ /* * click each toggle, * save changes, @@ -721,14 +703,6 @@ namespace PowerToysTests public static void ClassInitialize(TestContext context) { Setup(context); - - string settingsFolderPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Microsoft/PowerToys/FancyZones"); - if (!Directory.Exists(settingsFolderPath)) - { - Directory.CreateDirectory(settingsFolderPath); - } - _settingsPath = settingsFolderPath + "/settings.json"; - Init(); } @@ -751,31 +725,25 @@ namespace PowerToysTests } TearDown(); - } - - [TestInitialize] - public void TestInitialize() - { - try - { - _initialSettings = File.ReadAllText(_settingsPath); - _initialSettingsJson = JObject.Parse(_initialSettings); - } - catch (System.IO.FileNotFoundException) - { - _initialSettings = ""; - } + } + + [TestInitialize] + public void TestInitialize() + { + try + { + _initialSettingsJson = JObject.Parse(_initialSettings); + } + catch (Newtonsoft.Json.JsonReaderException) + { + //empty settings + } } [TestCleanup] public void TestCleanup() { - ScrollUp(); - - if (_initialSettings.Length > 0) - { - File.WriteAllText(_settingsPath, _initialSettings); - } + ScrollUp(); } } } diff --git a/src/tests/win-app-driver/FancyZonesTests/README.md b/src/tests/win-app-driver/FancyZonesTests/README.md index 7a31ed906..57ac0514e 100644 --- a/src/tests/win-app-driver/FancyZonesTests/README.md +++ b/src/tests/win-app-driver/FancyZonesTests/README.md @@ -4,36 +4,38 @@ - [x] Test if settings are saved in file properly ## Editor - - [] Open editor by clicking button from settings - - [] without settings file - - [] with valid settings file - - [] with invalid settings file - - [] Open editor by hotkey with opened settings window - - [] without settings file - - [] with valid settings file - - [] with invalid settings file - - [] Open editor by hotkey with closed settings window - - [] without settings file - - [] with valid settings file - - [] with invalid settings file - - [] Increase/decrease zone count, check min and max possible values - - [] Test if settings are saved in file properly - - [] `Show spacing` checked/unchecked - - [] `Space around zone` saved correctly - - [] `Space around zone` possible input values - - [] Edit templates, check settings files - - [] Create new custom layout - - [] empty - - [] one zone - - [] fullscreen - - [] not fullscreen - - [] many zones - - [] overlapping - - [] non-overlapping - - [] utf-16 layout name - - [] empty layout name - - [] special characters in layout name - - [] Remove custom layout - - [] Edit selected layout + - [x] Open editor by clicking button from settings + - [x] without settings file + - [x] without settings folder + - [x] with valid settings file + - [x] with valid settings file contained cyrillic characters + - [x] with invalid settings file + - [x] with cropped file + - [x] Open editor by hotkey + - [x] without settings file + - [x] without settings folder + - [x] with valid settings file + - [x] with valid settings file contained cyrillic characters + - [x] with invalid settings file + - [x] with cropped file + - [ ] Increase/decrease zone count, check min and max possible values + - [ ] Test if settings are saved in file properly + - [ ] `Show spacing` checked/unchecked + - [ ] `Space around zone` saved correctly + - [ ] `Space around zone` possible input values + - [ ] Edit templates, check settings files + - [ ] Create new custom layout + - [ ] empty + - [ ] one zone + - [ ] fullscreen + - [ ] not fullscreen + - [ ] many zones + - [ ] overlapping + - [ ] non-overlapping + - [ ] utf-16 layout name + - [ ] empty layout name + - [ ] special characters in layout name + - [ ] Remove custom layout + - [ ] Edit selected layout ### Usage diff --git a/src/tests/win-app-driver/PowerToysSession.cs b/src/tests/win-app-driver/PowerToysSession.cs index 7fc218347..a5b1d45b2 100644 --- a/src/tests/win-app-driver/PowerToysSession.cs +++ b/src/tests/win-app-driver/PowerToysSession.cs @@ -1,9 +1,11 @@ using System; +using System.IO; using System.Threading; using Microsoft.VisualStudio.TestTools.UnitTesting; +using OpenQA.Selenium; using OpenQA.Selenium.Appium; using OpenQA.Selenium.Appium.Windows; -using OpenQA.Selenium; +using OpenQA.Selenium.Interactions; namespace PowerToysTests { @@ -14,8 +16,29 @@ namespace PowerToysTests protected static bool isPowerToysLaunched = false; protected static WindowsElement trayButton; - public static void Setup(TestContext context) + protected static string _settingsFolderPath = ""; + protected static string _settingsPath = ""; + protected static string _zoneSettingsPath = ""; + protected static string _initialSettings = ""; + protected static string _initialZoneSettings = ""; + + public static void Setup(TestContext context, bool isLaunchRequired = true) { + //read settings before running tests to restore them after + _settingsFolderPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Microsoft\\PowerToys\\FancyZones"); + _settingsPath = _settingsFolderPath + "\\settings.json"; + _zoneSettingsPath = _settingsFolderPath + "\\zones-settings.json"; + try + { + _initialSettings = File.ReadAllText(_settingsPath); + _initialZoneSettings = File.ReadAllText(_zoneSettingsPath); + } + catch(Exception) + { + //failed to read settings + } + + if (session == null) { // Create a new Desktop session to use PowerToys. @@ -28,7 +51,7 @@ namespace PowerToysTests trayButton = session.FindElementByAccessibilityId("1502"); isPowerToysLaunched = CheckPowerToysLaunched(); - if (!isPowerToysLaunched) + if (!isPowerToysLaunched && isLaunchRequired) { LaunchPowerToys(); } @@ -38,11 +61,35 @@ namespace PowerToysTests public static void TearDown() { - if (session!=null) + //restore initial settings files + if (_initialSettings.Length > 0) + { + File.WriteAllText(_settingsPath, _initialSettings); + } + else + { + File.Delete(_settingsPath); + } + + if (_initialZoneSettings.Length > 0) + { + File.WriteAllText(_zoneSettingsPath, _initialZoneSettings); + } + else + { + File.Delete(_zoneSettingsPath); + } + + if (session != null) { session.Quit(); session = null; - } + } + } + + public static void WaitSeconds(int seconds) + { + Thread.Sleep(TimeSpan.FromSeconds(seconds)); } public static void ShortWait() @@ -53,14 +100,35 @@ namespace PowerToysTests public static void OpenSettings() { trayButton.Click(); - session.FindElementByName("PowerToys").Click(); + session.FindElementByXPath("//Button[@Name=\"PowerToys\"]").Click(); trayButton.Click(); } + public static void OpenFancyZonesSettings() + { + WindowsElement fzNavigationButton = session.FindElementByXPath("//Button[@Name=\"FancyZones\"]"); + Assert.IsNotNull(fzNavigationButton); + + fzNavigationButton.Click(); + fzNavigationButton.Click(); + + ShortWait(); + } + public static void CloseSettings() { - WindowsElement settings = session.FindElementByName("PowerToys Settings"); - settings.SendKeys(Keys.Alt + Keys.F4); + try + { + WindowsElement settings = session.FindElementByName("PowerToys Settings"); + if (settings != null) + { + settings.SendKeys(Keys.Alt + Keys.F4); + } + } + catch(Exception) + { + + } } private static bool CheckPowerToysLaunched() @@ -70,7 +138,7 @@ namespace PowerToysTests try { - WindowsElement pt = session.FindElementByName("PowerToys"); + WindowsElement pt = session.FindElementByXPath("//Button[@Name=\"PowerToys\"]"); isLaunched = (pt != null); } catch(OpenQA.Selenium.WebDriverException) @@ -99,5 +167,18 @@ namespace PowerToysTests //exception could be thrown even if app launched successfully } } + + public static void ExitPowerToys() + { + trayButton.Click(); + ShortWait(); + + WindowsElement pt = session.FindElementByXPath("//Button[@Name=\"PowerToys\"]"); + new Actions(session).MoveToElement(pt).ContextClick().Perform(); + ShortWait(); + + session.FindElementByXPath("//MenuItem[@Name=\"Exit\"]").Click(); + trayButton.Click(); //close tray + } } } diff --git a/src/tests/win-app-driver/win-app-driver.csproj b/src/tests/win-app-driver/win-app-driver.csproj index adee49eaf..9ef9acbdc 100644 --- a/src/tests/win-app-driver/win-app-driver.csproj +++ b/src/tests/win-app-driver/win-app-driver.csproj @@ -1,4 +1,4 @@ - + @@ -83,6 +83,7 @@ +