Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.