Saturday, December 24, 2016

Lesson #9 - Selenium WebDriver - Finding element using the ID attribute (C#)

Overview

תמונה קשורה
The ID attribute is the most common and probably the most simplified way to locate web elements, in real automation projects, the developers will assign a unique ID for each web object that we can use to identify the element without the need to use more advanced and complex locators. 



In addition:
  • Elements should be set as static and not dynamic.
  • The ID uniqueness must be enforced by the developer.
  • Many Web Elements are not including such parameter. 

Code Example








Let's use the ID attribute to locate the "Name" field in the contact form

HTML
<input type="text" value="" size="30" name="name" id="ContactForm1_contact-form-name" class="contact-form-name">

C# Code 

[TestMethod]
public void FindElementById()
{
IWebElement WebElement;
IWebDriver Firefox = new FirefoxDriver();
Firefox.Navigate().GoToUrl("http://www.machtested.com/");
WebElement = Firefox.FindElement(By.Id("ContactForm1_contact-form-name"));
}
}

Friday, December 23, 2016

Lesson #8 - Type of Locators available in the Selenium WebDriver API

Overview

Selenium WebDriver API, provides two functions that we can use for locating Web Elements, each function is used for a different purpose, as follows:

The FindElement () function is used when we need to work with a single web element, when executing this function; it will return a single object (Based on the search criteria) or throws up an exception.

The FindElements () function is used to retrieve more than a single element (Collection of elements).

תוצאת תמונה עבור ‪locators in selenium webdriver‬‏

Type of Locators


Locator Name
Code syntax (C# )
Description
ID
driver.FindElement(By.Id("Element ID"));
Locates an element using his ID
Name
driver.FindElement(By.Name("Element Name"));
Locates an element using his Name
Link text
driver.FindElement(By.LinkText ("Element link text"));
Locates an element using his Link Text
Class Name
driver.FindElement(By.ClassName("Element class name "));
Locates an element using his class name
Tag Name
driver.FindElement(By.TagName("Element Tag name"));
Locates an element using his tag name
CSS
driver.FindElement (By.CssSelector("Element CssSelector"));
Locates an element using CSS
XPath
driver.FindElement(By.XPath("Element XPath"));
Locates an element using XPath query
Partial Link text
driver.FindElement(By.PartialLinkText("Element PartialLinkText"));
Locates an element using a partial link text

Thursday, December 22, 2016

Lesson #7 - Selenium WebDriver - Using Web Browser Commands with C#


Overview

As you probably know, the first thing that we need to do, prior to executing the test plan, is to open a new URL session of the tested application.

In this post, we will review a list of methods and properties that we will use to control and manage the application under tests.

תוצאת תמונה עבור ‪selenium commands‬‏

Selenium Browser Commands

How to open a new web session?


[TestMethod]
public void HowToOpenANewWebSession()
{
IWebDriver Firefox = new FirefoxDriver();
Firefox.Navigate().GoToUrl("http://www.machtested.com/");
}

How to validate the page Title?

Very simple task, all we need to do is to use the “Title” IWebDriver property that returns a 'string' containing the web page title.

Example:
public void Tiltle_Validation()
        {
            IWebDriver Firefox = new FirefoxDriver();
            Firefox.Navigate().GoToUrl("Http://www.google.com");
            Assert.AreEqual("Google",Firefox.Title); //Case Sensitive           
        }

 

How to validate the current URL?

Sometimes we will need to validate that the web page URL is similar/Contains a specific syntax, the way to accomplish this task with C#, is to use the ‘String’ class methods as shown in the next code.

Example:

public void URL_Validation()
{
IWebDriver Firefox = new FirefoxDriver();           
Firefox.Navigate().GoToUrl("Http://www.machtested.com");               Assert.IsTrue(Firefox.Url.Contains("machtested.com"));          
}

 

How to refresh the web page?

In any case that your code execution needs to involve a web page refresh operation, the next code will help you to perform this task.

Example:
  
 public void URL_Refresh()
        {
            IWebDriver Firefox = new FirefoxDriver();           
            Firefox.Navigate().GoToUrl("http://www.machtested.com/");
            Firefox.Navigate().Refresh();       
        }

 

How to close a web page Tab?

Simple code that allows the user to close the current working URL

Example:
public void URL_Close()
        {
            IWebDriver Firefox = new FirefoxDriver();           
            Firefox.Navigate().GoToUrl("http://www.machtested.com/");
            Firefox.Close();
        }

How to quit and end the current session (All Tabs)?

 
Example:
public void URL_Quit()
   {
     IWebDriver Firefox = new FirefoxDriver();           
         Firefox.Navigate().GoToUrl("http://www.machtested.com/");
     Firefox.Quit();
   }

 

Return back

This method simulates the same operation as pressing on the browser "Back" navigation button (You will move back one page earlier in the browser)

Example:
Firefox.Navigate ().Back ();

 

Move forward

This method simulates the same operation as pressing on the browser "forward" navigation button (You will go forward one page)

Example:
Firefox.Navigate().Forward();

 

Delete all cookies

This function will remove the entire cookies from the selected web page

Example:

Firefox.Manage().Cookies.DeleteAllCookies();

 

Maximize window

This function will maximize the current window

Example:
Firefox.Manage().Window.Maximize();

 

Get the URL source code

This code will get the URL source code and return Boolean value for the assert command.

Example:

 
public void URL_Validation()
        {
            IWebDriver Firefox = new FirefoxDriver();
            Firefox.Navigate().GoToUrl("Http://www.dtvisiontech.com");
            Assert.IsTrue(Firefox.PageSource.StartsWith("StringA") || Firefox.PageSource.EndsWith("StringB") ;           
        }

My Presentations