Tuesday, April 28, 2015

Lesson #9 - Selenium WebDriver - Web Browser Elements (Text Field & Button) with C#


Overview


In this article I want to review two different web elements that you will encounter no matter what is the tested software, the first element is a “Button” and the second is “Text Box”.

During this article I will explain the different properties and methods that you can use to control and manipulate those elements.

 

Text Box/Field

Definition:  
Text box (can also call “Text Field”) is a web element that enables the user to insert/Remove text.

Preparation:

//Search Box Locator
 
WebElement = Firefox.FindElement(By.Name("search")); 

Tasks 1: Add New Text

//Insert new text into the search box
 
WebElement.SendKeys("Insert New Text"); 

Tasks 2: Remove Text

//Clear all text from the selected field
 
WebElement.Clear();

Tasks 3: Submit the text directly (Simulate “Enter” command)


WebElement.Submit();

Button

Definition:  
Button is a controller that allows the user to trigger a new event

 

Press Button

WebElement.Click(); //Press the button

 

Practical Example

Scenario


  1. Log-in to my blog.
  2. Maximize the window.
  3. Validate that the “Search” Field is available.
  4. Insert search query : “AD Hoc Testing”
  5. Validate that the “Search” button is available.
  6. Click the “Search” button.
  7. Validate the Search results.
  8. Quit.

 

Code


public void CodeExample()
        {           
            //Preperations:
            IWebElement WebElement;
            IWebDriver Firefox = new FirefoxDriver();
            Firefox.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(30));

            //Log-In to my blog:
            Firefox.Navigate().GoToUrl("Http://www.dtvisiontech.com");

            //Maximize the page:
            Firefox.Manage().Window.Maximize();

            //Find Element by name(Search Field)
            WebElement = Firefox.FindElement(By.Name("search"));

            //Validate that the element is available
            Assert.IsTrue(WebElement.Enabled);

            //Insert search query
            WebElement.SendKeys("AD Hoc Testing");

            ////Find Element by Xpath(Search Button)
            WebElement = Firefox.FindElement(By.XPath(("//input[@value='Search']")));

            //Validate that the element is available

            Assert.IsTrue(WebElement.Enabled);

            //Click Button
            WebElement.Click();

            //Validate that the search is successfuly submitted           

            //Extract the Element text
            string validation = Firefox.FindElement(By.ClassName("gs-title")).Text;
           
            //Compare the Actual Against the expected text
            Assert.AreEqual("David Tzemach - Technical Vision: AD Hoc Testing | Best ...",validation);

            //Quit
            Firefox.Quit();
           
        }

My Presentations