Selenium WebDriver Java Framework Course Limited Time Offer for $20

Selenium WebDriver Java Framework Course Limited Time Offer for $20

 

Print

Testing form with Selenium Code Sample C#

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 SeleniumTestForm
    {
        private IWebDriver driver;
        private StringBuilder verificationErrors;
        private string baseURL;
       
        [SetUp]
        public void SetupTest()
        {
            driver = new FirefoxDriver();
            baseURL = "http://www.seleniummaster.com/seleniumformtest/registrationform.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 TheSeleniumTestFormTest()
        {
            driver.Navigate().GoToUrl(baseURL + "/seleniumformtest/registrationform.aspx");
            driver.FindElement(By.Id("firstNameTextBox")).Clear();
            driver.FindElement(By.Id("firstNameTextBox")).SendKeys("abcd");
            driver.FindElement(By.Id("lastNameTextBox")).Clear();
            driver.FindElement(By.Id("lastNameTextBox")).SendKeys("abcd");
            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("phoneTextBox")).Clear();
            driver.FindElement(By.Id("phoneTextBox")).SendKeys("555-123-4567");
            new SelectElement(driver.FindElement(By.Id("booksDropDownList"))).SelectByText("Selenium RC");
            driver.FindElement(By.Id("osRadioButtonList_2")).Click();
            driver.FindElement(By.Id("registerButton")).Click();
            Assert.AreEqual("Registration Successful",driver.FindElement(By.Id("registrationconfirmation")).Text);
        }
        private bool IsElementPresent(By by)
        {
            try
            {
                driver.FindElement(by);
                return true;
            }
            catch (NoSuchElementException)
            {
                return false;
            }
        }
    }
}