[WinAppDriver tests] Affection on user setting files fix (#4186)

This commit is contained in:
Seraphima Zykova 2020-06-19 11:09:12 +03:00 committed by GitHub
parent c78e6588ad
commit 90efc5740f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 315 additions and 190 deletions

View File

@ -224,6 +224,7 @@ namespace JSONHelpers
{
std::wstring result = PTSettingsHelper::get_module_save_folder_location(moduleName);
jsonFilePath = result + L"\\" + std::wstring(L"zones-settings.json");
appZoneHistoryFilePath = result + L"\\" + std::wstring(L"app-zone-history.json");
}
#endif

View File

@ -261,6 +261,9 @@ namespace PowerToysTests
public static void ClassInitialize(TestContext context)
{
Setup(context, false);
if (session == null)
return;
ResetSettings();
if (!isPowerToysLaunched)
@ -281,6 +284,9 @@ namespace PowerToysTests
[TestInitialize]
public void TestInitialize()
{
if (session == null)
return;
//create canvas zone
OpenCreatorWindow("Create new custom", "Custom layout creator");
session.FindElementByAccessibilityId("newZoneButton").Click();
@ -289,6 +295,9 @@ namespace PowerToysTests
[TestCleanup]
public void TestCleanup()
{
if (session == null)
return;
new Actions(session).MoveToElement(session.FindElementByXPath("//Button[@Name=\"Cancel\"]")).Click().Perform();
}
}

View File

@ -292,6 +292,9 @@ namespace PowerToysTests
public static void ClassInitialize(TestContext context)
{
Setup(context, false);
if (session == null)
return;
ResetSettings();
}
@ -304,6 +307,9 @@ namespace PowerToysTests
[TestInitialize]
public void TestInitialize()
{
if (session == null)
return;
if (!isPowerToysLaunched)
{
LaunchPowerToys();

View File

@ -397,6 +397,9 @@ namespace PowerToysTests
public static void ClassInitialize(TestContext context)
{
Setup(context, false);
if (session == null)
return;
ResetSettings();
if (!isPowerToysLaunched)

View File

@ -233,6 +233,9 @@ namespace PowerToysTests
public static void ClassInitialize(TestContext context)
{
Setup(context, false);
if (session == null)
return;
ResetDefaultFancyZonesSettings(true);
}

View File

@ -1,4 +1,4 @@
using System;
using System;
using System.IO;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
@ -179,6 +179,9 @@ namespace PowerToysTests
public static void ClassInitialize(TestContext context)
{
Setup(context, false);
if (session == null)
return;
ResetSettings();
}

View File

@ -68,6 +68,9 @@ namespace PowerToysTests
public static void ClassInitialize(TestContext context)
{
Setup(context, false);
if (session == null)
return;
ResetDefaultFancyZonesSettings(true);
}
@ -81,6 +84,9 @@ namespace PowerToysTests
[TestInitialize]
public void TestInitialize()
{
if (session == null)
return;
OpenEditor();
OpenTemplates();
}

View File

@ -157,6 +157,9 @@ namespace PowerToysTests
public static void ClassInitialize(TestContext context)
{
Setup(context, false);
if (session == null)
return;
ResetDefaultFancyZonesSettings(false);
ResetDefaultZoneSettings(true);
}
@ -171,6 +174,9 @@ namespace PowerToysTests
[TestInitialize]
public void TestInitialize()
{
if (session == null)
return;
if (!isPowerToysLaunched)
{
LaunchPowerToys();

View File

@ -1,6 +1,7 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium.Appium.Windows;
using OpenQA.Selenium.Interactions;
using System;
namespace PowerToysTests
{
@ -15,6 +16,8 @@ namespace PowerToysTests
}
protected static void OpenEditor()
{
try
{
new Actions(session).KeyDown(OpenQA.Selenium.Keys.Command).SendKeys("`").KeyUp(OpenQA.Selenium.Keys.Command).Perform();
WaitSeconds(2);
@ -24,6 +27,11 @@ namespace PowerToysTests
//editorWindow = WaitElementByAccessibilityId("MainWindow1");
Assert.IsNotNull(editorWindow, "Couldn't find editor window");
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
protected static void CloseEditor()
{
@ -41,22 +49,38 @@ namespace PowerToysTests
}
protected static void OpenCustomLayouts()
{
try
{
WindowsElement customsTab = session.FindElementByName("Custom");
customsTab.Click();
string isSelected = customsTab.GetAttribute("SelectionItem.IsSelected");
Assert.AreEqual("True", isSelected, "Custom tab cannot be opened");
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
protected static void OpenTemplates()
{
try
{
WindowsElement templatesTab = session.FindElementByName("Templates");
templatesTab.Click();
string isSelected = templatesTab.GetAttribute("SelectionItem.IsSelected");
Assert.AreEqual("True", isSelected, "Templates tab cannot be opened");
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
protected static void OpenCreatorWindow(string tabName, string creatorWindowName, string buttonId = "EditCustomButton")
{
try
{
string elementXPath = "//Text[@Name=\"" + tabName + "\"]";
WaitElementByXPath(elementXPath).Click();
@ -65,6 +89,11 @@ namespace PowerToysTests
WindowsElement creatorWindow = WaitElementByName(creatorWindowName);
Assert.IsNotNull(creatorWindow, "Creator window didn't open");
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
protected void ZoneCountTest(int canvasZoneCount, int gridZoneCount)
{

View File

@ -713,6 +713,9 @@ namespace PowerToysTests
public static void ClassInitialize(TestContext context)
{
Setup(context);
if (session == null)
return;
Init();
}
@ -740,6 +743,9 @@ namespace PowerToysTests
[TestInitialize]
public void TestInitialize()
{
if (session == null)
return;
try
{
_initialSettingsJson = JObject.Parse(_initialSettings);

View File

@ -22,9 +22,11 @@ namespace PowerToysTests
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";
protected static string _appHistoryPath = _settingsFolderPath + "\\app-zone-history.json";
protected static string _initialSettings = "";
protected static string _initialZoneSettings = "";
protected static string _initialAppHistorySettings = "";
protected const string _defaultSettings = "{\"version\":\"1.0\",\"name\":\"FancyZones\",\"properties\":{\"fancyzones_shiftDrag\":{\"value\":true},\"fancyzones_mouseSwitch\":{\"value\":false},\"fancyzones_overrideSnapHotkeys\":{\"value\":false},\"fancyzones_moveWindowAcrossMonitors\":{\"value\":false},\"fancyzones_zoneSetChange_flashZones\":{\"value\":false},\"fancyzones_displayChange_moveWindows\":{\"value\":false},\"fancyzones_zoneSetChange_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\":[]}";
@ -32,17 +34,17 @@ namespace PowerToysTests
public static void Setup(TestContext context, bool isLaunchRequired = true)
{
ReadUserSettings(); //read settings before running tests to restore them after
if (session == null)
{
ReadUserSettings(); //read settings before running tests to restore them after
// Create a new Desktop session to use PowerToys.
AppiumOptions appiumOptions = new AppiumOptions();
appiumOptions.PlatformName = "Windows";
appiumOptions.AddAdditionalCapability("app", "Root");
try
{
session = new WindowsDriver<WindowsElement>(new Uri(WindowsApplicationDriverUrl), appiumOptions);
Assert.IsNotNull(session);
trayButton = session.FindElementByAccessibilityId("1502");
isPowerToysLaunched = CheckPowerToysLaunched();
@ -51,6 +53,11 @@ namespace PowerToysTests
LaunchPowerToys();
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
public static void TearDown()
@ -124,13 +131,22 @@ namespace PowerToysTests
}
public static void OpenSettings()
{
try
{
trayButton.Click();
session.FindElementByXPath("//Button[@Name=\"PowerToys\"]").Click();
trayButton.Click();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
public static void OpenFancyZonesSettings()
{
try
{
WindowsElement fzNavigationButton = WaitElementByXPath("//Button[@Name=\"FancyZones\"]");
Assert.IsNotNull(fzNavigationButton);
@ -138,6 +154,11 @@ namespace PowerToysTests
fzNavigationButton.Click();
fzNavigationButton.Click();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
public static void CloseSettings()
{
@ -149,28 +170,27 @@ namespace PowerToysTests
settings.SendKeys(Keys.Alt + Keys.F4);
}
}
catch(Exception)
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
private static bool CheckPowerToysLaunched()
{
trayButton.Click();
bool isLaunched = false;
try
{
trayButton.Click();
WindowsElement pt = WaitElementByXPath("//Button[@Name=\"PowerToys\"]");
isLaunched = (pt != null);
trayButton.Click(); //close
}
catch(OpenQA.Selenium.WebDriverException)
{
//PowerToys not found
}
trayButton.Click(); //close
return isLaunched;
}
@ -196,17 +216,23 @@ namespace PowerToysTests
}
public static void ExitPowerToys()
{
try
{
trayButton.Click();
WindowsElement pt = WaitElementByXPath("//Button[@Name=\"PowerToys\"]");
Assert.IsNotNull(pt, "Couldn't find \'PowerToys\' button");
new Actions(session).MoveToElement(pt).ContextClick().Perform();
WaitElementByXPath("//MenuItem[@Name=\"Exit\"]").Click();
trayButton.Click(); //close tray
isPowerToysLaunched = false;
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}
}
public static void ResetDefaultFancyZonesSettings(bool relaunch)
{
@ -240,22 +266,34 @@ namespace PowerToysTests
private static void ReadUserSettings()
{
try
{
if (_initialSettings.Length == 0)
{
_initialSettings = File.ReadAllText(_settingsPath);
}
catch (Exception)
{
//failed to read settings
}
catch (Exception)
{ }
try
{
if (_initialZoneSettings.Length == 0)
{
_initialZoneSettings = File.ReadAllText(_zoneSettingsPath);
}
catch (Exception)
{
//failed to read settings
}
catch (Exception)
{ }
try
{
if (_initialAppHistorySettings.Length == 0)
{
_initialAppHistorySettings = File.ReadAllText(_appHistoryPath);
}
}
catch (Exception)
{ }
}
private static void RestoreUserSettings()
@ -277,6 +315,15 @@ namespace PowerToysTests
{
File.Delete(_zoneSettingsPath);
}
if (_initialAppHistorySettings.Length > 0)
{
File.WriteAllText(_appHistoryPath, _initialAppHistorySettings);
}
else
{
File.Delete(_appHistoryPath);
}
}
}
}

View File

@ -1,4 +1,4 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium.Appium;
using OpenQA.Selenium.Appium.Windows;
using OpenQA.Selenium.Interactions;
@ -95,6 +95,9 @@ namespace PowerToysTests
[TestInitialize]
public void TestInitialize()
{
if (session == null)
return;
isSettingsOpened = false;
isTrayOpened = false;
}

View File

@ -111,6 +111,9 @@ namespace PowerToysTests
[TestInitialize]
public void TestInitialize()
{
if (session == null)
return;
isWinKeyPressed = false;
// If the start menu is open, close it.