Last active
August 8, 2021 01:51
-
-
Save elizabeth-young/5363034 to your computer and use it in GitHub Desktop.
Extensions to Selenium WebDriver
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
public static class WebDriverExtensions | |
{ | |
public static void ClickElement(this IWebElement element, Drivers driverChoice) | |
{ | |
switch (driverChoice) | |
{ | |
case Drivers.InternetExplorer: | |
element.SendKeys("\n"); | |
break; | |
default: | |
element.Click(); | |
break; | |
} | |
} | |
public static void Sleep(this IWebDriver driver, int sleeptime) | |
{ | |
Thread.Sleep(sleeptime * 1000); | |
} | |
public static IWebElement FindElement(this IWebDriver driver, By by, int timeout) | |
{ | |
if (timeout > 0) | |
{ | |
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(timeout)); | |
return wait.Until(drv => drv.FindElement(by)); | |
} | |
return driver.FindElement(by); | |
} | |
public static T WaitUntil<T>(this IWebDriver driver, Func<IWebDriver, T> condition, int timeout) | |
{ | |
var wait = new WebDriverWait(driver, new TimeSpan(0, 0, timeout)); | |
return wait.Until(condition); | |
} | |
public static T Wait<T>(this IWebDriver driver, Func<IWebDriver, T> condition, int waittime) | |
{ | |
Thread.Sleep(waittime * 1000); | |
var wait = new WebDriverWait(driver, new TimeSpan(0, 0, waittime)); | |
return wait.Until(condition); | |
} | |
public static IList<IWebElement> WaitForItems(this IWebDriver driver, By by, int numItems, int timeout) | |
{ | |
Thread.Sleep(1000); | |
IList<IWebElement> elements; | |
bool timedout = false; | |
var now = DateTime.Now; | |
do | |
{ | |
elements = driver.FindElements(by); | |
timedout = (DateTime.Now - now).Seconds > TestConfiguration.SleepTimeout; | |
} while (!timedout && elements.Count != numItems); | |
return elements; | |
} | |
public static string WaitForValue(this IWebDriver driver, By by, string expectedValue, int timeout) | |
{ | |
Thread.Sleep(1000); | |
IWebElement element; | |
string value = ""; | |
bool timedout = false; | |
var now = DateTime.Now; | |
do | |
{ | |
element = driver.FindElement(by); | |
try | |
{ | |
if (element != null) | |
{ | |
value = element.GetAttribute("value"); | |
} | |
} | |
catch (Exception) | |
{ | |
} | |
timedout = (DateTime.Now - now).Seconds > TestConfiguration.SleepTimeout; | |
} while (!timedout && element != null && value != expectedValue); | |
return value; | |
} | |
public static string WaitForText(this IWebDriver driver, By by, string expectedText, int timeout) | |
{ | |
Thread.Sleep(1000); | |
IWebElement element = null; | |
string text = ""; | |
bool timedout = false; | |
var now = DateTime.Now; | |
do | |
{ | |
try | |
{ | |
element = driver.FindElement(by); | |
if (element != null) | |
{ | |
text = element.Text; | |
} | |
timedout = (DateTime.Now - now).Seconds > TestConfiguration.SleepTimeout; | |
} | |
catch (Exception) | |
{ | |
} | |
} while (!timedout && element != null && text != expectedText); | |
return text; | |
} | |
public static IWebElement WaitForElement(this IWebDriver driver, By by, int timeout) | |
{ | |
Thread.Sleep(1000); | |
IWebElement element = null; | |
bool timedout = false; | |
var now = DateTime.Now; | |
do | |
{ | |
try | |
{ | |
element = driver.FindElement(by); | |
timedout = (DateTime.Now - now).Seconds > TestConfiguration.SleepTimeout; | |
} | |
catch (Exception) | |
{ | |
} | |
} while (!timedout && element != null); | |
return element; | |
} | |
public static bool WaitForDisplayed(this IWebDriver driver, By by, int timeout) | |
{ | |
Thread.Sleep(1000); | |
IWebElement element = null; | |
bool displayed = false; | |
bool timedout = false; | |
var now = DateTime.Now; | |
do | |
{ | |
try | |
{ | |
element = driver.FindElement(by); | |
if (element != null) | |
{ | |
displayed = element.Displayed; | |
} | |
timedout = (DateTime.Now - now).Seconds > TestConfiguration.SleepTimeout; | |
} | |
catch (Exception) | |
{ | |
} | |
} while (!timedout && !displayed); | |
return displayed; | |
} | |
public static bool WaitForNotDisplayed(this IWebDriver driver, By by, int timeout) | |
{ | |
Thread.Sleep(1000); | |
IWebElement element = null; | |
bool displayed = true; | |
bool timedout = false; | |
var now = DateTime.Now; | |
do | |
{ | |
try | |
{ | |
element = driver.FindElement(by); | |
if (element != null) | |
{ | |
displayed = element.Displayed; | |
} | |
timedout = (DateTime.Now - now).Seconds > TestConfiguration.SleepTimeout; | |
} | |
catch (Exception) | |
{ | |
} | |
} while (!timedout && element != null && displayed); | |
return displayed; | |
} | |
public static bool ElementPresent(this IWebDriver driver, By by) | |
{ | |
try | |
{ | |
return driver.FindElement(by).Displayed; | |
} | |
catch (Exception) | |
{ | |
return false; | |
} | |
} | |
public static bool ElementPresent(this IWebElement element, By by) | |
{ | |
try | |
{ | |
return element.FindElement(by).Displayed; | |
} | |
catch (Exception) | |
{ | |
return false; | |
} | |
} | |
public static bool ClickWhenVisible(this IWebElement element, By by, int timeout) | |
{ | |
Thread.Sleep(1000); | |
bool timedout = false; | |
bool clicked = false; | |
var now = DateTime.Now; | |
do | |
{ | |
try | |
{ | |
timedout = (DateTime.Now - now).Seconds > TestConfiguration.SleepTimeout; | |
element.FindElement(by).Click(); | |
clicked = true; | |
} | |
catch (Exception) { } | |
} while (!timedout && !clicked); | |
return clicked; | |
} | |
public static string GetValue(this IWebDriver driver, By by) | |
{ | |
return driver.FindElement(by).GetAttribute("value"); | |
} | |
public static object ExecuteJavaScript(this IWebDriver driver, string script) | |
{ | |
return ((IJavaScriptExecutor)driver).ExecuteScript(script); | |
} | |
public static void ClickLink(this IWebDriver driver, string linkText) | |
{ | |
driver.FindElement(By.LinkText(linkText), TestConfiguration.SleepTimeout).Click(); | |
} | |
public static void ClickButton(this IWebDriver driver, string buttonId) | |
{ | |
driver.FindElement(By.Id(buttonId), TestConfiguration.SleepTimeout).Click(); | |
} | |
public static bool IsChecked(this IWebDriver driver, By by) | |
{ | |
return driver.FindElement(by).Selected; | |
} | |
public static void SelectOption(this IWebDriver driver, By by, string option) | |
{ | |
IWebElement select = driver.FindElement(by); | |
IList<IWebElement> options = select.FindElements(By.TagName("option")); | |
foreach (IWebElement optionElement in options) | |
{ | |
if(String.Compare(optionElement.Text, option, StringComparison.OrdinalIgnoreCase) == 0) | |
{ | |
optionElement.Click(); | |
break; | |
} | |
} | |
} | |
public static string SwitchWindow(this IWebDriver driver, string title) | |
{ | |
string mainHandle = driver.CurrentWindowHandle; | |
foreach (var handle in driver.WindowHandles) | |
{ | |
driver.SwitchTo().Window(handle); | |
if (driver.Title == title) | |
{ | |
return mainHandle; | |
} | |
} | |
throw new ArgumentException(string.Format("Unable to find window with title: '{0}'", title)); | |
} | |
public static void SwitchToFirstWindow(this IWebDriver driver) | |
{ | |
driver.SwitchTo().Window(driver.WindowHandles.First()); | |
} | |
public static void SwitchToLastWindow(this IWebDriver driver) | |
{ | |
driver.SwitchTo().Window(driver.WindowHandles.Last()); | |
} | |
public static void Action(this IWebDriver driver, string url) | |
{ | |
driver.Navigate().GoToUrl("http://localhost:" + TestConfiguration.iisport + "/" + url); | |
} | |
public static void MaximizeBrowser(this IWebDriver driver, string browser) | |
{ | |
Win32.SetWindowSize(browser, 1920, 1040); | |
} | |
public static void MaximiseFirstBrowser(this IWebDriver driver, string browser) | |
{ | |
Win32.SetFirstWindowSize(browser, 1920, 1040); | |
} | |
public static void MaximiseAllBrowsers(this IWebDriver driver, string browser) | |
{ | |
Win32.SetAllWindowSize(browser, 1920, 1040); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment