forked from microsoft/WinUI-Gallery
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor tests and fix test failures
- Loading branch information
Showing
22 changed files
with
393 additions
and
468 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
global using OpenQA.Selenium; | ||
global using OpenQA.Selenium.Appium.Windows; | ||
global using Microsoft.VisualStudio.TestTools.UnitTesting; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,58 +1,23 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using System.Threading; | ||
using OpenQA.Selenium.Appium.Windows; | ||
namespace UITests | ||
{ | ||
|
||
[TestClass] | ||
public class SampleTestTemplate : Test_Base | ||
{ | ||
|
||
private static WindowsElement element1 = null; | ||
private static WindowsElement element2 = null; | ||
|
||
public static void ClassInitialize(TestContext context) | ||
{ | ||
Setup(context); | ||
var buttonTab = session.FindElementByName("Basic Input"); | ||
buttonTab.Click(); | ||
var button = session.FindElementByName("RadioButton"); | ||
button.Click(); | ||
Thread.Sleep(1000); | ||
element1 = session.FindElementByAccessibilityId("Element Locator"); | ||
Assert.IsNotNull(element1); | ||
element2 = session.FindElementByAccessibilityId("Element Locator"); | ||
Assert.IsNotNull(element2); | ||
} | ||
|
||
[ClassCleanup] | ||
public static void ClassCleanup() | ||
{ | ||
TearDown(); | ||
} | ||
|
||
[TestMethod] | ||
public void Test1() | ||
{ | ||
// Assert.AreEqual("Option 1", element1.Text); | ||
// Assert.AreEqual("Option 2", element2.Text); | ||
} | ||
|
||
[TestMethod] | ||
public void Test2() | ||
{ | ||
// Assert.AreEqual("Option 1", element1.Text); | ||
// Assert.AreEqual("Option 2", element2.Text); | ||
} | ||
} | ||
|
||
|
||
|
||
|
||
[TestClass] | ||
public class SampleTestTemplate : TestBase | ||
{ | ||
|
||
private static WindowsElement element1 = null; | ||
private static WindowsElement element2 = null; | ||
|
||
public static void ClassInitialize(TestContext context) | ||
{ | ||
OpenControlPage("MyControlPage"); | ||
Thread.Sleep(1000); | ||
element1 = Session.FindElementByAccessibilityId("Element Locator"); | ||
Assert.IsNotNull(element1); | ||
element2 = Session.FindElementByAccessibilityId("Element Locator"); | ||
Assert.IsNotNull(element2); | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
using OpenQA.Selenium.Appium; | ||
using OpenQA.Selenium.Appium.Windows; | ||
using System; | ||
using System.Diagnostics; | ||
using System.IO; | ||
using System.Threading; | ||
|
||
namespace UITests | ||
{ | ||
[TestClass] | ||
public class SessionManager | ||
{ | ||
private const string WindowsApplicationDriverUrl = "http://127.0.0.1:4723"; | ||
private static string[] WinUIGalleryAppIDs = new string[]{ | ||
"Microsoft.WinUI3ControlsGallery.Debug_s9y1p3hwd5qda!App", | ||
"Microsoft.WinUI3ControlsGallery_s9y1p3hwd5qda!App" | ||
}; | ||
|
||
private static uint appIdIndex = 0; | ||
|
||
private static WindowsDriver<WindowsElement> _session; | ||
public static WindowsDriver<WindowsElement> Session | ||
{ | ||
get | ||
{ | ||
if (_session is null) | ||
{ | ||
Setup(null); | ||
} | ||
return _session; | ||
} | ||
} | ||
|
||
[AssemblyInitialize] | ||
public static void Setup(TestContext _) | ||
{ | ||
if (_session is null) | ||
{ | ||
|
||
int timeoutCount = 50; | ||
|
||
tryInitializeSession(); | ||
if (_session is null) | ||
{ | ||
// WinAppDriver is probably not running, so lets start it! | ||
if (File.Exists(@"C:\Program Files (x86)\Windows Application Driver\WinAppDriver.exe")) | ||
{ | ||
Process.Start(@"C:\Program Files (x86)\Windows Application Driver\WinAppDriver.exe"); | ||
} | ||
else if (File.Exists(@"C:\Program Files\Windows Application Driver\WinAppDriver.exe")) | ||
{ | ||
Process.Start(@"C:\Program Files\Windows Application Driver\WinAppDriver.exe"); | ||
} | ||
else | ||
{ | ||
throw new Exception("Unable to start WinAppDriver since no suitable location was found."); | ||
} | ||
|
||
Thread.Sleep(10000); | ||
tryInitializeSession(); | ||
} | ||
|
||
while (_session is null && timeoutCount < 1000 * 4) | ||
{ | ||
tryInitializeSession(); | ||
Thread.Sleep(timeoutCount); | ||
timeoutCount *= 2; | ||
} | ||
|
||
Thread.Sleep(3000); | ||
Assert.IsNotNull(_session); | ||
Assert.IsNotNull(_session.SessionId); | ||
|
||
// Dismiss the disclaimer window that may pop up on the very first application launch | ||
// If the disclaimer is not found, this throws an exception, so lets catch that | ||
try | ||
{ | ||
_session.FindElementByName("Disclaimer").FindElementByName("Accept").Click(); | ||
} | ||
catch (OpenQA.Selenium.WebDriverException) { } | ||
|
||
// Wait if something is still animating in the visual tree | ||
_session.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(3); | ||
_session.Manage().Window.Maximize(); | ||
} | ||
} | ||
|
||
[AssemblyCleanup()] | ||
public static void TestRunTearDown() | ||
{ | ||
TearDown(); | ||
} | ||
|
||
public static void TearDown() | ||
{ | ||
if (_session is not null) | ||
{ | ||
_session.CloseApp(); | ||
_session.Quit(); | ||
_session = null; | ||
} | ||
} | ||
|
||
private static void tryInitializeSession() | ||
{ | ||
AppiumOptions appiumOptions = new AppiumOptions(); | ||
appiumOptions.AddAdditionalCapability("app", WinUIGalleryAppIDs[appIdIndex]); | ||
appiumOptions.AddAdditionalCapability("deviceName", "WindowsPC"); | ||
try | ||
{ | ||
_session = new WindowsDriver<WindowsElement>(new Uri(WindowsApplicationDriverUrl), appiumOptions); | ||
} | ||
catch (OpenQA.Selenium.WebDriverException exc) | ||
{ | ||
// Use next app ID since the current one was failing | ||
if (exc.Message.Contains("Package was not found")) | ||
{ | ||
appIdIndex++; | ||
} | ||
else | ||
{ | ||
Console.WriteLine("Failed to update start driver, got exception:" + exc.Message); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
using Microsoft.VisualBasic; | ||
using OpenQA.Selenium.Appium.Windows; | ||
using OpenQA.Selenium.Interactions; | ||
using System.Threading; | ||
|
||
namespace UITests | ||
{ | ||
public class TestBase | ||
{ | ||
public static WindowsDriver<WindowsElement> Session => SessionManager.Session; | ||
|
||
public static void OpenControlPage(string name) | ||
{ | ||
var search = Session.FindElementByName("Search"); | ||
search.Clear(); | ||
Thread.Sleep(1_000); | ||
search.SendKeys(name); | ||
Thread.Sleep(1_000); | ||
Session.FindElementByName(name).Click(); | ||
} | ||
|
||
public static void TypeText(string text) | ||
{ | ||
var actions = new Actions(Session); | ||
actions.SendKeys(text).Perform(); | ||
} | ||
|
||
[ClassCleanup] | ||
public static void Cleanup() | ||
{ | ||
Session.FindElementByName("Home"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.