Skip to content

Instantly share code, notes, and snippets.

@isaaclyman
Last active April 12, 2016 16:54
Show Gist options
  • Save isaaclyman/606295832b30b9f28484cc2f0e340cec to your computer and use it in GitHub Desktop.
Save isaaclyman/606295832b30b9f28484cc2f0e340cec to your computer and use it in GitHub Desktop.
ProtractorHelper: Run Protractor on multiple environments from a .NET Integration test
using System;
using System.ComponentModel;
using System.Diagnostics;
// Put this in whatever namespace you want, then construct and run it in an Integration test.
// The Run() method returns an exit code where 0 means the test was successful and anything else means it wasn't.
// So just assert that the result of Run() is 0, like so: `Assert.AreEqual(0, resultOfProtractorHelperDotRun);`
public class ProtractorHelper
{
private readonly string _configPath;
private readonly string _environment;
private readonly int _timeout;
public ProtractorHelper(string relativePath, string environment = null, int timeout = 60000)
{
_configPath = AppDomain.CurrentDomain.BaseDirectory + "../../../" + relativePath;
// In your CI server, set an environment variable "IntegrationTestEnvironment" before each run.
// Then use browser.params.environment in Protractor to compose the baseUrl for your test.
_environment = environment ?? Environment.GetEnvironmentVariable("IntegrationTestEnvironment");
_timeout = timeout;
}
public int Run()
{
// Kill any leftover console processes from previous runs
foreach (Process p in Process.GetProcessesByName("cmd"))
{
try
{
p.Kill();
p.WaitForExit(5000);
}
catch (Win32Exception)
{
// process was terminating or can't be terminated
}
catch (InvalidOperationException)
{
// process has already exited
}
}
// Run "protractor path/to/protractor.conf.js --params.environment=local|dev|stage|live"
var protractorInfo = new ProcessStartInfo("cmd.exe")
{
Arguments = string.Format("/C protractor {0} --params.environment={1}", _configPath, _environment),
CreateNoWindow = true,
RedirectStandardInput = true,
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false
};
var protractor = Process.Start(protractorInfo);
// Wait for protractor tests to end, then get the output and errors
protractor.WaitForExit(_timeout);
string output = protractor.StandardOutput.ReadToEnd();
string errors = protractor.StandardError.ReadToEnd();
int exitCode = protractor.ExitCode;
protractor.Close();
Console.WriteLine("[Protractor Output:] " + output);
if (errors.Length > 0)
{
Console.Error.WriteLine("[Protractor Errors:] " + errors);
}
return exitCode;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment