Tests updates (#1793)

* updated tests for v0.16.0
Co-Authored-By: Yevhenii Holovachov <55396981+yevhenii44@users.noreply.github.com>
This commit is contained in:
Seraphima Zykova 2020-04-01 17:05:01 +03:00 committed by GitHub
parent b5f2c8b583
commit ea18fa95ad
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 816 additions and 789 deletions

View File

@ -1,6 +1,7 @@
using System.IO;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Newtonsoft.Json.Linq;
using OpenQA.Selenium.Appium.Windows;
using OpenQA.Selenium.Interactions;
namespace PowerToysTests
@ -15,10 +16,11 @@ namespace PowerToysTests
private void CancelTest()
{
new Actions(session).MoveToElement(session.FindElementByXPath("//Button[@Name=\"Cancel\"]")).Click().Perform();
WindowsElement cancelButton = session.FindElementByXPath("//Window[@Name=\"FancyZones Editor\"]/Window/Button[@Name=\"Cancel\"]");
new Actions(session).MoveToElement(cancelButton).Click().Perform();
ShortWait();
Assert.AreEqual(_initialZoneSettings, File.ReadAllText(_zoneSettingsPath), "Settings were changed");
Assert.AreEqual(_defaultZoneSettings, File.ReadAllText(_zoneSettingsPath), "Settings were changed");
}
private void SaveTest()

View File

@ -17,29 +17,21 @@ namespace PowerToysTests
protected static bool isPowerToysLaunched = false;
protected static WindowsElement trayButton;
protected static string _settingsFolderPath = "";
protected static string _settingsPath = "";
protected static string _zoneSettingsPath = "";
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 _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
}
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\":[]}";
public static void Setup(TestContext context, bool isLaunchRequired = true)
{
ReadUserSettings(); //read settings before running tests to restore them after
if (session == null)
{
// Create a new Desktop session to use PowerToys.
@ -56,38 +48,20 @@ namespace PowerToysTests
{
LaunchPowerToys();
}
}
}
}
public static void TearDown()
{
//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);
}
{
RestoreUserSettings(); //restore initial settings files
if (session != null)
{
session.Quit();
session = null;
}
}
}
public static void WaitSeconds(double seconds)
{
Thread.Sleep(TimeSpan.FromSeconds(seconds));
@ -200,8 +174,10 @@ namespace PowerToysTests
try
{
AppiumOptions opts = new AppiumOptions();
opts.PlatformName = "Windows";
opts.AddAdditionalCapability("app", "Microsoft.PowerToys_8wekyb3d8bbwe!PowerToys");
opts.PlatformName = "Windows";
opts.AddAdditionalCapability("platformVersion", "10");
opts.AddAdditionalCapability("deviceName", "WindowsPC");
opts.AddAdditionalCapability("app", "C:/Program Files/PowerToys/PowerToys.exe");
WindowsDriver<WindowsElement> driver = new WindowsDriver<WindowsElement>(new Uri(WindowsApplicationDriverUrl), opts);
Assert.IsNotNull(driver);
@ -232,37 +208,72 @@ namespace PowerToysTests
public static void ResetDefaultFancyZonesSettings(bool relaunch)
{
if (!Directory.Exists(_settingsFolderPath))
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))
{
Directory.CreateDirectory(_settingsFolderPath);
Directory.CreateDirectory(folder);
}
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);
File.WriteAllText(filePath, data);
if (isPowerToysLaunched)
{
ExitPowerToys();
}
if (relaunch)
{
LaunchPowerToys();
}
}
public static void ResetDefautZoneSettings(bool relaunch)
private static void ReadUserSettings()
{
string zoneSettings = "{\"app-zone-history\":[],\"devices\":[],\"custom-zone-sets\":[]}";
File.WriteAllText(_zoneSettingsPath, zoneSettings);
if (isPowerToysLaunched)
try
{
ExitPowerToys();
_initialSettings = File.ReadAllText(_settingsPath);
}
if (relaunch)
catch (Exception)
{
LaunchPowerToys();
//failed to read settings
}
try
{
_initialZoneSettings = File.ReadAllText(_zoneSettingsPath);
}
catch (Exception)
{
//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);
}
}
}

View File

@ -1,4 +1,5 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium.Appium;
using OpenQA.Selenium.Appium.Windows;
using OpenQA.Selenium.Interactions;
@ -55,10 +56,12 @@ namespace PowerToysTests
trayButton.Click();
isTrayOpened = true;
WindowsElement pt = session.FindElementByName("PowerToys");
Assert.IsNotNull(pt);
WindowsElement notificationOverflow = session.FindElementByName("Notification Overflow");
AppiumWebElement overflowArea = notificationOverflow.FindElementByName("Overflow Notification Area");
AppiumWebElement powerToys = overflowArea.FindElementByName("PowerToys");
Assert.IsNotNull(powerToys);
new Actions(session).MoveToElement(pt).ContextClick().Perform();
new Actions(session).MoveToElement(powerToys).ContextClick().Perform();
ShortWait();
//exit
@ -66,10 +69,12 @@ namespace PowerToysTests
ShortWait();
//check PowerToys exited
pt = null;
powerToys = null;
try
{
pt = session.FindElementByName("PowerToys");
notificationOverflow = session.FindElementByName("Notification Overflow");
overflowArea = notificationOverflow.FindElementByName("Overflow Notification Area");
powerToys = overflowArea.FindElementByName("PowerToys");
}
catch (OpenQA.Selenium.WebDriverException)
{
@ -79,7 +84,7 @@ namespace PowerToysTests
LaunchPowerToys();
ShortWait();
Assert.IsNull(pt);
Assert.IsNull(powerToys);
}
[ClassInitialize]