Created
June 17, 2014 04:08
-
-
Save jchadwick/7ac66c2e763626916cde to your computer and use it in GitHub Desktop.
Selenium Extensions
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
using System; | |
using System.IO; | |
using NUnit.Framework; | |
using Website.Automation.Workflows; | |
public abstract class AutomatedTestFixture | |
{ | |
[TestFixtureTearDown] | |
public void TestFixtureTearDown() | |
{ | |
Browser.Close(); | |
} | |
[TestFixtureSetUp] | |
public virtual void TestFixtureSetUp() | |
{ | |
LoginWorkflow.LoginWithDefaultCredentials(); | |
} | |
[TearDown] | |
public virtual void TearDown() | |
{ | |
switch (TestContext.CurrentContext.Result.State) | |
{ | |
case TestState.Error: | |
case TestState.Failure: | |
var filename = | |
Path.Combine(TestContext.CurrentContext.WorkDirectory, TestContext.CurrentContext.Test.FullName) + | |
".png"; | |
Browser.TakeScreenshot(filename); | |
Console.WriteLine("Screenshot: " + new Uri(filename.Replace(" ", "%20"))); | |
break; | |
} | |
} | |
} |
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
using System.Configuration; | |
using System.Drawing.Imaging; | |
using OpenQA.Selenium; | |
using OpenQA.Selenium.Chrome; | |
using OpenQA.Selenium.Support.Extensions; | |
public static class Browser | |
{ | |
private static IWebDriver driver = new ChromeDriver(); | |
internal static IWebDriver Driver | |
{ | |
get { return driver; } | |
} | |
public static string BaseUrl | |
{ | |
get { return _baseUrl; } | |
set { _baseUrl = value; } | |
} | |
private static string _baseUrl = ConfigurationManager.AppSettings["Browser.BaseUrl"]; | |
public static void Close() | |
{ | |
driver.Close(); | |
driver.Quit(); | |
} | |
public static IWebDriver NavigateTo(string path) | |
{ | |
var url = path.Contains("://") ? path : BaseUrl + path; | |
driver.Navigate().GoToUrl(url); | |
return driver; | |
} | |
public static TPage NavigateTo<TPage>() where TPage : Page, new() | |
{ | |
return NavigateTo(new TPage()); | |
} | |
public static TPage NavigateTo<TPage>(TPage page) where TPage : Page | |
{ | |
NavigateTo(page.Path); | |
return page; | |
} | |
public static void TakeScreenshot(string filename) | |
{ | |
driver.TakeScreenshot().SaveAsFile(filename, ImageFormat.Png); | |
} | |
} |
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
using OpenQA.Selenium; | |
public abstract class Page | |
{ | |
protected readonly IWebDriver driver = Browser.Driver; | |
public virtual string Path { get; private set; } | |
protected Page(string path) | |
{ | |
Path = path; | |
} | |
} |
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
using System.Linq; | |
using OpenQA.Selenium; | |
public static class WebDriverExtensions | |
{ | |
public static void Click(this IWebDriver driver, By by) | |
{ | |
driver.FindElement(@by).Click(); | |
} | |
public static bool ElementIsValid(this IWebDriver driver, By by) | |
{ | |
return !driver.FindElement(@by).HasClass("input-validation-error"); | |
} | |
public static bool HasClass(this IWebElement el, string className) | |
{ | |
return el.GetAttribute("class").Split(' ').Contains(className); | |
} | |
public static string GetValue(this IWebElement element) | |
{ | |
return element.GetAttribute("value"); | |
} | |
public static string GetValue(this IWebDriver driver, By by) | |
{ | |
return driver.FindElement(by).GetValue(); | |
} | |
public static void SetValue(this IWebElement element, string value) | |
{ | |
element.Clear(); | |
element.SendKeys(value); | |
} | |
public static void SetValue(this IWebDriver driver, By by, string value) | |
{ | |
driver.FindElement(by).SetValue(value); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment