Last active
April 24, 2018 19:23
-
-
Save mrlacey/4c87832917ca88ddbc57721338ae871d to your computer and use it in GitHub Desktop.
Base class for WinAppDriver based UI tests
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
public class AutomatedWizardTestingBase : IDisposable | |
{ | |
protected WindowsDriver<WindowsElement> AppSession { get; private set; } | |
protected AutomatedWizardTestingBase() | |
{ | |
CheckWinAppDriverInstalled(); | |
StartWinAppDriverIfNotRunning(); | |
} | |
public void Dispose() | |
{ | |
AppSession?.Dispose(); | |
StopWinAppDriverIfRunning(); | |
} | |
private void CheckWinAppDriverInstalled() | |
{ | |
// Assume default install location. adjust as appropriate | |
Assert.True( | |
File.Exists(@"C:\Program Files (x86)\Windows Application Driver\WinAppDriver.exe"), | |
"WinAppDriver is not installed. Download from https://github.com/Microsoft/WinAppDriver/releases"); | |
} | |
private void StartWinAppDriverIfNotRunning() | |
{ | |
var script = @" | |
$wad = Get-Process WinAppDriver -ErrorAction SilentlyContinue | |
if ($wad -eq $Null) | |
{ | |
Start-Process ""C:\Program Files (x86)\Windows Application Driver\WinAppDriver.exe"" | |
}"; | |
ExecutePowerShellScript(script); | |
} | |
private void StopWinAppDriverIfRunning() | |
{ | |
var script = @" | |
$wad = Get-Process WinAppDriver -ErrorAction SilentlyContinue | |
if ($wad -ne $null) | |
{ | |
$wad.CloseMainWindow() | |
}"; | |
ExecutePowerShellScript(script); | |
} | |
private Collection<PSObject> ExecutePowerShellScript(string script) | |
{ | |
using (var ps = System.Management.Automation.PowerShell.Create()) | |
{ | |
ps.AddScript(script); | |
Collection<PSObject> psOutput = ps.Invoke(); | |
if (ps.Streams.Error.Count > 0) | |
{ | |
foreach (var errorRecord in ps.Streams.Error) | |
{ | |
Debug.WriteLine(errorRecord.ToString()); | |
} | |
} | |
return psOutput; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment