153 lines
5.2 KiB
C#
153 lines
5.2 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using IntegrationTests.Properties;
|
|
using OpenQA.Selenium;
|
|
using OpenQA.Selenium.Chrome;
|
|
using OpenQA.Selenium.Firefox;
|
|
using OpenQA.Selenium.IE;
|
|
using OpenQA.Selenium.Support.UI;
|
|
|
|
namespace IntegrationTests.Helpers
|
|
{
|
|
[Serializable]
|
|
public enum Browsers
|
|
{
|
|
[Description("Windows Internet Explorer")]
|
|
InternetExplorer,
|
|
|
|
[Description("Mozilla Firefox")]
|
|
Firefox,
|
|
|
|
[Description("Google Chrome")]
|
|
Chrome
|
|
}
|
|
|
|
public static class Browser
|
|
{
|
|
//public static Browsers SelectedBrowser
|
|
//{
|
|
// get { return Settings.Default.Browser; }
|
|
//}
|
|
//private static IWebDriver _webDriver;
|
|
//private static string _mainWindowHandler;
|
|
//private static IWebDriver WebDriver
|
|
//{
|
|
// get { return _webDriver ?? StartWebDriver(); }
|
|
//}
|
|
//private static IWebDriver StartWebDriver()
|
|
//{
|
|
// //TODO: ensure it is not required
|
|
// //Contract.Ensures(Contract.Result<IWebDriver>() != null);
|
|
|
|
// if (_webDriver != null) return _webDriver;
|
|
|
|
// switch (SelectedBrowser)
|
|
// {
|
|
// case Browsers.InternetExplorer:
|
|
// _webDriver = StartInternetExplorer();
|
|
// break;
|
|
// case Browsers.Firefox:
|
|
// _webDriver = StartFirefox();
|
|
// break;
|
|
// case Browsers.Chrome:
|
|
// _webDriver = StartChrome();
|
|
// break;
|
|
// default:
|
|
// throw new Exception(string.Format("Unknown browser selected: {0}.", SelectedBrowser));
|
|
// }
|
|
|
|
// _webDriver.Manage().Window.Maximize();
|
|
// _mainWindowHandler = _webDriver.CurrentWindowHandle;
|
|
|
|
// return WebDriver;
|
|
//}
|
|
public static object ExecuteJavaScript(IWebDriver driver, string javaScript, params object[] args)
|
|
{
|
|
var javaScriptExecutor = (IJavaScriptExecutor)driver;
|
|
|
|
return javaScriptExecutor.ExecuteScript(javaScript, args);
|
|
}
|
|
private static InternetExplorerDriver StartInternetExplorer()
|
|
{
|
|
var internetExplorerOptions = new InternetExplorerOptions
|
|
{
|
|
IntroduceInstabilityByIgnoringProtectedModeSettings = true,
|
|
InitialBrowserUrl = "about:blank",
|
|
EnableNativeEvents = true
|
|
};
|
|
|
|
return new InternetExplorerDriver(Directory.GetCurrentDirectory(), internetExplorerOptions);
|
|
}
|
|
|
|
private static FirefoxDriver StartFirefox()
|
|
{
|
|
var firefoxProfile = new FirefoxProfile
|
|
{
|
|
AcceptUntrustedCertificates = true,
|
|
EnableNativeEvents = true
|
|
};
|
|
|
|
return new FirefoxDriver(firefoxProfile);
|
|
}
|
|
|
|
private static ChromeDriver StartChrome()
|
|
{
|
|
var chromeOptions = new ChromeOptions();
|
|
//TODO: ensure that we dont need to delete data folder
|
|
//var defaultDataFolder = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\..\Local\Google\Chrome\User Data\Default";
|
|
|
|
//if (Directory.Exists(defaultDataFolder))
|
|
//{
|
|
// Executor.Try(() => DirectoryExtension.ForceDelete(defaultDataFolder));
|
|
//}
|
|
|
|
return new ChromeDriver(Directory.GetCurrentDirectory(), chromeOptions);
|
|
}
|
|
|
|
public static void WaitForPageLoad(IWebDriver driver, int seconds2Wait)
|
|
{
|
|
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(seconds2Wait));
|
|
wait.Until(webDriver => ((IJavaScriptExecutor)webDriver).ExecuteScript("return document.readyState").Equals("complete"));
|
|
}
|
|
public static void WaitForAjax(IWebDriver driver)
|
|
{
|
|
while (true) // Handle timeout somewhere
|
|
{
|
|
var ajaxIsComplete = (bool)((IJavaScriptExecutor)driver).ExecuteScript("return (typeof($) === 'undefined') ? true : !$.active;");
|
|
if (ajaxIsComplete)
|
|
break;
|
|
Thread.Sleep(100);
|
|
}
|
|
}
|
|
|
|
public static bool SelectOptionByText(IWebDriver driver, string selectId, string optionText)
|
|
{
|
|
var select = driver.FindElement(By.Id(selectId));
|
|
var options = select.FindElements(By.TagName("option"));
|
|
foreach (var option in options.Where(option => option.Text.Equals(optionText)))
|
|
{
|
|
option.Click();
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
public static bool SelectOptionByValue(IWebDriver driver, string selectId, string optionValue)
|
|
{
|
|
var select = driver.FindElement(By.Id(selectId));
|
|
var options = select.FindElements(By.TagName("option"));
|
|
foreach (var option in options.Where(option => option.GetAttribute("value").Equals(optionValue)))
|
|
{
|
|
option.Click();
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
}
|
|
}
|