111 lines
4.6 KiB
C#
111 lines
4.6 KiB
C#
using OpenQA.Selenium;
|
|
using OpenQA.Selenium.Appium;
|
|
using OpenQA.Selenium.Appium.Windows;
|
|
using System.Diagnostics;
|
|
using System.Windows;
|
|
|
|
namespace Tests.E2ETests {
|
|
public class TestBase {
|
|
|
|
protected const string WindowsApplicationDriverUrl = "http://127.0.0.1:4723";
|
|
//private const string ApplicationPath = @"..\..\Elwig\bin\Release\net8.0-windows\Elwig.exe";
|
|
private const string ApplicationPath = @"C:\Users\tom\source\repos\elwig\Elwig\bin\Release\net8.0-windows\Elwig.exe";
|
|
private const int WaitForAppLaunch = 3;
|
|
private const string WinAppDriverPath = @"C:\Program Files (x86)\Windows Application Driver\WinAppDriver.exe";
|
|
private static Process winAppDriverProcess;
|
|
public WindowsDriver<WindowsElement> AppSession { get; private set; }
|
|
public WindowsDriver<WindowsElement> DesktopSession { get; private set; }
|
|
|
|
|
|
private static void StartWinAppDriver() {
|
|
ProcessStartInfo psi = new(WinAppDriverPath) {
|
|
UseShellExecute = true,
|
|
//Verb = "runas" // run as administrator
|
|
};
|
|
//psi.EnvironmentVariables.Add("DX.UITESTINGENABLED", "1");
|
|
winAppDriverProcess = Process.Start(psi);
|
|
}
|
|
|
|
public void Setup() {
|
|
//StartWinAppDriver();
|
|
var appiumOptions = new AppiumOptions();
|
|
appiumOptions.AddAdditionalCapability("app", ApplicationPath);
|
|
appiumOptions.AddAdditionalCapability("deviceName", "WindowsPC");
|
|
appiumOptions.AddAdditionalCapability("ms:waitForAppLaunch", WaitForAppLaunch);
|
|
AppSession = new WindowsDriver<WindowsElement>(new Uri(WindowsApplicationDriverUrl), appiumOptions);
|
|
Assert.That(AppSession, Is.Not.Null);
|
|
Assert.That(AppSession.SessionId, Is.Not.Null);
|
|
AppSession.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(1.5);
|
|
AppiumOptions desktopOptions = new AppiumOptions();
|
|
desktopOptions.AddAdditionalCapability("app", "Root");
|
|
desktopOptions.AddAdditionalCapability("deviceName", "WindowsPC");
|
|
DesktopSession = new WindowsDriver<WindowsElement>(new Uri(WindowsApplicationDriverUrl), desktopOptions);
|
|
}
|
|
|
|
public void TearDown() {
|
|
|
|
// Close the AppSession
|
|
if (AppSession != null) {
|
|
CloseElwig();
|
|
AppSession.Quit();
|
|
AppSession = null;
|
|
}
|
|
// Close the DesktopSession
|
|
if (DesktopSession != null) {
|
|
DesktopSession.Close();
|
|
DesktopSession.Quit();
|
|
DesktopSession = null;
|
|
}
|
|
}
|
|
|
|
private void CloseElwig() {
|
|
try {
|
|
AppSession.Close();
|
|
string currentHandle = AppSession.CurrentWindowHandle;
|
|
|
|
DismissOpenWindowsDialog();
|
|
} catch { }
|
|
}
|
|
|
|
private void DismissOpenWindowsDialog() {
|
|
try {
|
|
DesktopSession.FindElementByName("Yes").Click();
|
|
} catch { }
|
|
}
|
|
|
|
protected static void StopWinappDriver() {
|
|
// Stop the WinAppDriverProcess
|
|
if (winAppDriverProcess != null) {
|
|
foreach (var process in Process.GetProcessesByName("WinAppDriver")) {
|
|
process.Kill();
|
|
}
|
|
}
|
|
}
|
|
|
|
protected WindowsDriver<WindowsElement> CreateWindowDriver(string windowName) {
|
|
var mainWindow = DesktopSession.FindElementByAccessibilityId(windowName);
|
|
var mainWindowHandle = mainWindow.GetAttribute("NativeWindowHandle");
|
|
mainWindowHandle = (int.Parse(mainWindowHandle)).ToString("x"); // Convert to Hex
|
|
var appiumOptions = new AppiumOptions();
|
|
appiumOptions.AddAdditionalCapability("appTopLevelWindow", mainWindowHandle);
|
|
return new WindowsDriver<WindowsElement>(new Uri(WindowsApplicationDriverUrl), appiumOptions);
|
|
}
|
|
|
|
protected void ClickComboBoxByCount(WindowsDriver<WindowsElement> session, string accessibilityId, int count) {
|
|
var element = session.FindElementByAccessibilityId(accessibilityId);
|
|
element.Click();
|
|
for (int i = 0; i < count; i++) {
|
|
element.SendKeys(Keys.Down);
|
|
}
|
|
element.SendKeys(Keys.Enter);
|
|
}
|
|
|
|
protected void ClickComboBoxByText(WindowsDriver<WindowsElement> session, string accessibilityId, string text) {
|
|
var element = session.FindElementByAccessibilityId(accessibilityId);
|
|
element.Click();
|
|
element.SendKeys(text);
|
|
element.SendKeys(Keys.Enter);
|
|
}
|
|
}
|
|
}
|