Skip to content

Instantly share code, notes, and snippets.

@mrlacey
Last active April 24, 2018 19:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrlacey/4c87832917ca88ddbc57721338ae871d to your computer and use it in GitHub Desktop.
Save mrlacey/4c87832917ca88ddbc57721338ae871d to your computer and use it in GitHub Desktop.
Base class for WinAppDriver based UI tests
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