Saturday, March 25, 2017

Selenium WebDriver – Using the Text Field and Button elements (C#)

תמונה קשורהI think that you can agree with me that these two elements are a mandatory part of any software that you will need to test; the next code examples will demonstrate how to use them.

Definitions:

Text box /Field:  is a web element that enables the user to insert/Remove text.
Button: is a controller that allows the user to trigger a new event.


List of available commands 


Algorithm:

  1. Log in to Yahoo.
  2. Validate both the Title and URL (using the Assert function).
  3. Enter Text to the search field and remove it (SendKeys () and Clear ()).
  4. Enter text www.machtested.com and use the "Submit" function.
  5. Select the first link and click on it using the click ().
  6. Quit.

Code:

[TestMethod]
public void TextFieldAndButtonExample ()
{
IWebElement TempElement;
IWebDriver Firefox = new FirefoxDriver();
Firefox.Navigate().GoToUrl("https://www.yahoo.com/");
Assert.AreEqual("Yahoo", Firefox.Title);
Assert.AreEqual("https://www.yahoo.com/", Firefox.Url);
TempElement = Firefox.FindElement(By.Id("uh-search-box"));
TempElement.SendKeys("Clear text example");
TempElement.Clear();
TempElement.SendKeys("http://www.machtested.com/");
TempElement.Submit();
Firefox.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(30));
Firefox.FindElement(By.XPath("//a[contains(@href,'RU=http%3a%2f%2fwww.machtested.com%2f/RK')]")).Click();
Firefox.Quit();
}




No comments:

Post a Comment

My Presentations