Skip to content

Instantly share code, notes, and snippets.

@ChrisMoney
Created April 13, 2015 17:04
Show Gist options
  • Save ChrisMoney/97d90962d4263ba293ee to your computer and use it in GitHub Desktop.
Save ChrisMoney/97d90962d4263ba293ee to your computer and use it in GitHub Desktop.
Unit Test - Selenium UI
OpenQA.Selenium;
OpenQA.Selenium.FireFox;
OpenQA.Selenium.Support.UI;
[TestMethod]
public void TestMethod() {
IWebDriver driver = new FireFoxDriver();
driver.Navigate().GoToUrl("http://localhost:8080/webpage");
IwebElement username = driver.FindElement(By.Id("username");
username.SendKeys("admin");
IWebElement password = driver.FindElement(By.Id("password");
password.SendKeys("test");
IWebElement button = driver.FindElement(By.Name("submit");
button.Submit();
driver.Quit();
}
// find elements with XPath
IWebElement element = driver.FindElements(By.XPath("//p");
// find elements by CSS selector
IWebElement element = driver.FindElements(By.CssSelector(".myClass");
// find elements by Link Text
IWebElement element = driver.FindElement(By.LinkText("myHyperlink");
// write a line to browser
Debug.WriteLine(element.Text);
// click element
element.Click;
// Alerts
IWebElement button = driver.FindElement(By.Id("showConfirm");
button.Click;
string message = "Alert";
// create alert
IAlert alert = driver.SwitchTo().Alert();
alert.SendKey(message);
alert.Dismiss();
// find current window
string originalWindowHandle = driver.CurrentWindowHandle;
// find Windows
IWebElement window = driver.FindElement(By.Id("windowName");
//loop through tabs
Foreach (string handle in driver.WindowHandles) {
driver.SwitchTo().Window(handle);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment