Skip to content

Instantly share code, notes, and snippets.

@stuartwhiteford
Created October 6, 2019 21:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save stuartwhiteford/bc21df9e1b98785beef0a6ed66b8c4f8 to your computer and use it in GitHub Desktop.
Save stuartwhiteford/bc21df9e1b98785beef0a6ed66b8c4f8 to your computer and use it in GitHub Desktop.
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.IE;
using System;
using System.IO;
namespace EfCoreSandbox.Tests
{
[TestClass]
public class HomePageTests
{
private const string webAppBaseUrl = "https://efcoresandbox.azurewebsites.net/";
private static IWebDriver driver;
private static TestContext testContextInstance;
[ClassInitialize]
public static void ClassInitialise(TestContext testContext)
{
testContextInstance = testContext;
SetupDriver();
driver.Url = webAppBaseUrl;
driver.Navigate();
}
[ClassCleanup]
public static void ClassCleanup()
{
TeardownDriver();
}
[TestMethod]
public void HomePageHeadingContainsWelcome()
{
// Arrange/Act/Assert
driver.FindElement(By.TagName("h1")).Text.Should().Contain("Welcome");
}
[TestCleanup]
public void TestCleanup()
{
if (testContextInstance.CurrentTestOutcome != UnitTestOutcome.Passed)
{
TakeScreenshot($"{testContextInstance.TestName}.png");
}
}
private static void SetupDriver()
{
try
{
InternetExplorerOptions ieOptions = new InternetExplorerOptions
{
EnableNativeEvents = false,
UnhandledPromptBehavior = UnhandledPromptBehavior.Accept,
EnablePersistentHover = true,
IntroduceInstabilityByIgnoringProtectedModeSettings = true,
IgnoreZoomLevel = true,
EnsureCleanSession = true,
};
// Attempt to read the IEWebDriver environment variable that exists on the Azure
// platform and then fall back to the local directory.
string ieWebDriverPath = Environment.GetEnvironmentVariable("IEWebDriver");
if (string.IsNullOrEmpty(ieWebDriverPath))
{
ieWebDriverPath = Path.GetDirectoryName(AppDomain.CurrentDomain.BaseDirectory);
}
driver = new InternetExplorerDriver(ieWebDriverPath, ieOptions)
{
Url = webAppBaseUrl
};
}
catch (Exception ex)
{
TeardownDriver();
throw new ApplicationException("Could not setup IWebDriver.", ex);
}
}
private static void TeardownDriver()
{
if (driver != null)
{
driver.Close();
driver.Quit();
driver.Dispose();
driver = null;
}
}
private static void TakeScreenshot(string fileName)
{
Screenshot ss = ((ITakesScreenshot)driver).GetScreenshot();
string path = Path.Combine(Directory.GetCurrentDirectory(), fileName);
ss.SaveAsFile(path);
testContextInstance.AddResultFile(path);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment