Skip to content

Instantly share code, notes, and snippets.

@barancev
Created May 22, 2020 19:32
Show Gist options
  • Save barancev/e4b54a0fd0b3530cbd3d41ad3d62192e to your computer and use it in GitHub Desktop.
Save barancev/e4b54a0fd0b3530cbd3d41ad3d62192e to your computer and use it in GitHub Desktop.
using System;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Support.UI;
namespace addressbook_web_tests
{
[TestFixture]
public class UntitledTestCase
{
private IWebDriver driver;
private StringBuilder verificationErrors;
private string baseURL;
private bool acceptNextAlert = true;
[SetUp]
public void SetupTest()
{
driver = new FirefoxDriver();
baseURL = "http://localhost/addressbook/";
verificationErrors = new StringBuilder();
}
[TearDown]
public void TeardownTest()
{
try
{
driver.Quit();
}
catch (Exception)
{
// Ignore errors if unable to close the browser
}
Assert.AreEqual("", verificationErrors.ToString());
}
[Test]
public void TheUntitledTestCaseTest()
{
driver.Navigate().GoToUrl(baseURL);
driver.FindElement(By.XPath("//input[@value='Login']")).Click();
driver.FindElement(By.LinkText("groups")).Click();
driver.FindElement(By.Name("new")).Click();
driver.FindElement(By.Name("group_name")).Click();
driver.FindElement(By.Name("group_name")).Clear();
driver.FindElement(By.Name("group_name")).SendKeys("a");
driver.FindElement(By.Name("group_header")).Click();
driver.FindElement(By.Name("group_header")).Clear();
driver.FindElement(By.Name("group_header")).SendKeys("a");
driver.FindElement(By.Name("group_footer")).Click();
driver.FindElement(By.Name("group_footer")).Clear();
driver.FindElement(By.Name("group_footer")).SendKeys("a");
driver.FindElement(By.Name("submit")).Click();
driver.FindElement(By.LinkText("Logout")).Click();
driver.FindElement(By.Name("user")).Clear();
driver.FindElement(By.Name("user")).SendKeys("admin");
}
private bool IsElementPresent(By by)
{
try
{
driver.FindElement(by);
return true;
}
catch (NoSuchElementException)
{
return false;
}
}
private bool IsAlertPresent()
{
try
{
driver.SwitchTo().Alert();
return true;
}
catch (NoAlertPresentException)
{
return false;
}
}
private string CloseAlertAndGetItsText()
{
try
{
IAlert alert = driver.SwitchTo().Alert();
string alertText = alert.Text;
if (acceptNextAlert)
{
alert.Accept();
}
else
{
alert.Dismiss();
}
return alertText;
}
finally
{
acceptNextAlert = true;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment