Selenium WebDriver Java Framework Course Limited Time Offer for $20

Selenium WebDriver Java Framework Course Limited Time Offer for $20

 

Print

Selenium Guest Book Test Case C# Code

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 SeleniumTests
{
    [TestFixture]
    public class Seleniumguestbooktest
    {
        private IWebDriver driver;
        private StringBuilder verificationErrors;
        private string baseURL;
       
        [SetUp]
        public void SetupTest()
        {
            driver = new FirefoxDriver();
            baseURL = "http://www.seleniummaster.com/seleniumguestbook/Guestbook.aspx";
            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 TheSeleniumguestbookTest()
        {
            driver.Navigate().GoToUrl(baseURL + "/seleniumguestbook/Guestbook.aspx");
            for (int second = 0;; second++) {
                if (second >= 60) Assert.Fail("timeout");
                try
                {
                    if (IsElementPresent(By.Id("submitButton"))) break;
                }
                catch (Exception)
                {}
                Thread.Sleep(1000);
            }
            Assert.IsTrue(IsElementPresent(By.Id("submitButton")));
            driver.FindElement(By.Id("nameTextBox")).Clear();
            driver.FindElement(By.Id("nameTextBox")).SendKeys("seleniumtest");
            driver.FindElement(By.Id("emailTextBox")).Clear();
            driver.FindElement(By.Id("emailTextBox")).SendKeys(" This email address is being protected from spambots. You need JavaScript enabled to view it. ");
            driver.FindElement(By.Id("messageTextBox")).Clear();
            driver.FindElement(By.Id("messageTextBox")).SendKeys("This is a test!!!");
            driver.FindElement(By.Id("submitButton")).Click();
            for (int second = 0;; second++) {
                if (second >= 60) Assert.Fail("timeout");
                try
                {
                    if (IsElementPresent(By.Id("messageConfirmation"))) break;
                }
                catch (Exception)
                {}
                Thread.Sleep(1000);
            }
            // ERROR: Caught exception [ERROR: Unsupported command [isTextPresent]]
        }
        private bool IsElementPresent(By by)
        {
            try
            {
                driver.FindElement(by);
                return true;
            }
            catch (NoSuchElementException)
            {
                return false;
            }
        }
    }
}