using OpenQA.Selenium; using OpenQA.Selenium.Edge; using OpenQA.Selenium.Remote; using OpenQA.Selenium.Support.UI; using System; using System.IO; using System.Linq; using System.Threading; namespace Microsoft.Edge.A11y { /// /// This is another wrapper around WebDriver. The reason this is used is to maintain /// compatibility with Edge internal testing tools. /// public class DriverManager { private RemoteWebDriver _driver; private TimeSpan _searchTimeout; /// /// Only ctor /// /// How long to search for elements public DriverManager(TimeSpan searchTimeout) { try { _driver = new EdgeDriver(EdgeDriverService.CreateDefaultService(DriverExecutablePath, "MicrosoftWebDriver.exe", 17556)); } catch (InvalidOperationException) { Console.WriteLine("Unable to start a WebDriver session. Ensure that the previous server window is closed."); Environment.Exit(1); } _searchTimeout = searchTimeout; } /// /// The root directory of the A11y project /// public static string ProjectRootFolder { get { return new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory).Parent.Parent.FullName; } } /// /// The path to the WebDriver executables for Edge /// private static string DriverExecutablePath { get { //TODO support for linux/mac paths return Path.Combine(ProjectRootFolder, Environment.Is64BitOperatingSystem ? "DriversExecutables\\AMD64" : "DriversExecutables\\X86"); } } /// /// Navigate to url /// /// public void NavigateToUrl(string url) { _driver.Navigate().GoToUrl(url); } /// /// Execute a script on the current page /// /// The script /// The timeout in seconds /// How long to wait before executing the script in milliseconds /// Parameters to pass to the script /// public object ExecuteScript(string script, int timeout, int additionalSleep = 0, params object[] args) { Thread.Sleep(additionalSleep); var wait = new WebDriverWait(_driver, new TimeSpan(0, 0, 0, timeout)); wait.Until(driver => ((IJavaScriptExecutor)driver).ExecuteScript("return document.readyState").Equals("complete")); var js = (IJavaScriptExecutor)_driver; return js.ExecuteScript(script, args); } /// /// Send the given keys to the element with the given id /// /// The element's id /// The keys to send public void SendKeys(string elementId, string keys) { _driver.FindElement(By.Id(elementId)).SendKeys(keys); } public Screenshot GetScreenshot() { return _driver.GetScreenshot(); } /// /// Close the driver /// internal void Close() { if (null != _driver) { try { _driver.Quit(); _driver.Dispose(); } catch (Exception) { // Don't throw here } _driver = null; } } } }