Click here to Skip to main content
13,901,221 members
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

10.4K views
4 bookmarked
Posted 6 Mar 2016
Licenced Ms-PL

Getting Started with Telerik Testing Framework C# in 10 Minutes

, 6 Mar 2016
Rate this:
Please Sign up or sign in to vote.
Brief tutorial how to start using one of the world best automation frameworks - Telerik Testing Framework. Exact steps to follow through a C# code.The post Getting Started with Telerik Testing Framework C# in 10 Minutes appeared first on Automate The Planet.

Introduction

The Telerik Testing Framework exposes numerous properties and methods to help you easily build non-brittle, maintainable functional tests. It is cross-browser compatible. Expands your browser compatibility testing to all the latest major browsers: IE, Chrome, Firefox and Safari without ever changing your test case code. It also allows you to set implicit and explicit waits, test drag-n-drop and hovers, dynamic page elements, complex animations, generic and custom UI controls, and more. In our division, we are heavily using the framework. Personally, I believe that it has one of the richest Testing APIs on the market. Keep in mind that the Telerik Testing Framework is completely FREE (a list of key features).  Because I honestly believe that this framework can increase the quality of your automated tests, I am introducing a new series of blog posts dedicated to it. This is the first article in the series that will help you to get started. 

Create Your First Telerik Testing Framework Project

1. Download the Telerik Testing Framework

2. Install the Telerik Testing Framework

Telerik's browsers' clients are automatically installed in your install directory.

3. Create a new C# Unit Test Project in Visual Studio.

4. Select your project in the Solution Explorer in Visual Studio

5. Right-click the References folder displayed in the solution and select 'Add Reference'.

6. Navigate to the ArtOfTest.WebAii.dll installed on your machine in your %InstallDir%bin folder.

7. Select ArtOfTest.WebAii.dll

7. Click OK to finish adding the needed references.

Find even more detailed information in the official Getting Started with Telerik Testing Framework guide.

Telerik Testing Framework Basics C# Code

Configure the Testing Manager and Start a New Browser Instance

[TestClass]
public class TelerikTestFrameworkGettingStarted
{
    private Manager manager;

    [TestInitialize]
    public void TestInitialize()
    {
        Settings mySettings = new Settings();
        mySettings.Web.DefaultBrowser = BrowserType.FireFox;
        manager = new Manager(mySettings);
        manager.Start();
        manager.LaunchNewBrowser();
    }

    [TestCleanup]
    public void TestCleanup()
    {
        manager.Dispose();
    }

    [TestMethod]
    public void NavigateToAutomateThePlanet()
    {
        manager.ActiveBrowser.NavigateTo("http://automatetheplanet.com/");
    }
}

First, you need to create a new Settings instance, part of the ArtOfTest.WebAii.Core namespace. Through the WebSettings class exposed via the Web property, you can configure various settings regarding the web execution. After that, pass this object to the constructor of the WebAii Manager. Through the manager instance, you can control the entire test execution like starting, closing or maximizing browser instances. You can start a new browser instance using the LaunchNewBrowser method. You can set up a default browser, initializing the DefaultBrowser property. The Telerik Testing Framework's manager exposes an easy access to the current browser's instance through it property ActiveBrowser

Navigate to URL

Just use the NavigateTo method of the ActiveBrowser.

[TestMethod]
public void NavigateToAutomateThePlanet()
{
    manager.ActiveBrowser.NavigateTo("http://automatetheplanet.com/");
}

Advanced Web Settings

Set Default URL

Also, you can set up a default URL. When set NavigateTo should use a relative URL (e.g. "~/default.aspx").

manager.Settings.Web.BaseUrl = "http://automatetheplanet.com/";

Reuse Browser Instance

If you want to reuse the browser instance throughout the tests from the entire test class, you can set the RecycleBrowser property to true.

manager.Settings.Web.RecycleBrowser = true;

Kill Browser Process on Close

The KillBrowserProcessOnClose property gets or sets whether to make sure the browser process is killed when closing the browser.

manager.Settings.Web.KillBrowserProcessOnClose = true;

Note: Firefox is a single process browser. If you are using multiple browser instances and this setting is on, it will kill all open instances of Firefox.

Finding Page Elements

  • By ID
HtmlAnchor anchorById = manager.ActiveBrowser.Find.ById<HtmlAnchor>("uniqueId");
  • By Name
HtmlInputText textInputByName = 
manager.ActiveBrowser.Find.ByName<HtmlInputText>("textInputName");
  • By Class
HtmlDiv divByClass = manager.ActiveBrowser.Find.ByAttributes<HtmlDiv>("class=myclass");

Searches for an element using an 'exact' or 'partial' list of attribute values (You can specify 1-N attribute/value pairs).

  • By Content
HtmlSpan spanByContent = 
manager.ActiveBrowser.Find.ByContent<HtmlSpan>(
  "l:Automate The Planet", 
  FindContentType.InnerText);

Searches for an element using 'exact', 'partial' or 'regex' of the element content. The element content can be: InnerText, InnerMarkup, OuterMarkup, TextConten.

  • 'l:' - signifies literal
  • 'p:' signifies partial
  • 'x:' signifies regular expression ("x:^(<tr>s*<tds*scope=.*>s*Education)")
 
  • By XPath
HtmlSelect selectByXpath = manager.ActiveBrowser.Find.ByXPath<HtmlSelect>("/html/body/select");
  • Find all By Tag name
IList<Element> allImages = manager.ActiveBrowser.Find.AllByTagName("img");
  • Find all by multiple criteria
var allElements = 
manager.ActiveBrowser.Find.AllByExpression("class=myclass", "textcontent=!Planet");

You can find more detailed information in the official Finding Page Elements Guide.

FindClauses With Operators

The key design goal for FindExpressions is to enable a flexible, rich and extensible search definition pattern. A FindClause is a name/value pair with an optional comparison operator. The optional operator is ALWAYS the first character after the = in the expression. This special character can be escaped with a preceding ' character if it is meant to be interpreted as a literal character.

 

Leading Character

Example

Description

~

foo=~bar

Find the element where attribute foo 'contains' bar

!

foo=!bar

Find the element where attribute foo 'does not contain' bar

 

^

foo=^bar

Find the element where attribute foo 'starts with' bar

?

foo=?bar

Find the element where attribute foo 'ends with' bar

#

foo=#ba*

Find the element where attribute foo 'matches the regular expression' ba* - See this link for a description of regular expressions.

|

'id=CenterDiv', '|',

'tagIndex=a:2'

Chains two expressions together. Chained expressions work by finding the element that matches the first expression, then underneath that find the element that matches the next expression. There is no technical limit to how many expressions can be chained together. The example tells Test Studio to find the first element whose ID = CenterDiv and then under that element find the third anchor element (tagIndex is 0 based).

 

'

textContent=''City

Escape special characters. Find the element whose text = 'City', including the ' characters.

 

+

foo=+

Find the element that has the specified attribute. Find the element that has the attribute 'foo'.

 

-

foo=-

Find the element that does not have the specified attribute. Find the element that does not have the attribute 'foo'.

 

You can find more detailed information in the official HTML Find Expressions Guide.

HTML Element Actions in Telerik Testing Framework

For the examples I am going to use the well-known Automate The Planet's page- 'Awesome Healthy Menu Generator'.

 
[TestMethod]
public void TelerikTestStudioFrameworkBasicActions()
{
    manager.ActiveBrowser.NavigateTo(
        "http://automatetheplanet.com/healthy-diet-menu-generator/");
    HtmlInputSubmit generateButton = 
    manager.ActiveBrowser.Find.ByName<HtmlInputSubmit>("_ninja_forms_field_28");
    HtmlInputCheckBox additionalSugarCheckbox = 
    manager.ActiveBrowser.Find.ById<HtmlInputCheckBox>("ninja_forms_field_18");
    HtmlInputText firstNameTextInput = 
    manager.ActiveBrowser.Find.ByXPath<HtmlInputText>("//*[@id='ninja_forms_field_23']");
    HtmlSelect burgersSelect = 
    manager.ActiveBrowser.Find.ByName<HtmlSelect>("ninja_forms_field_21");
    HtmlInputRadioButton coffeeRadioButton = 
    manager.ActiveBrowser.Find.ByExpression<HtmlInputRadioButton>("value=^1 x Trenta");


    coffeeRadioButton.Check(true, true, true);
    burgersSelect.SelectByText("10 x Double Cheeseburgers");
    firstNameTextInput.Text = "Anton";
    additionalSugarCheckbox.Check(
        isChecked: true, 
        invokeOnChange: true, 
        invokeOnClickChanged: true);
    generateButton.Click();
}
  • Check a Radio button
coffeeRadioButton.Check(isChecked: true, invokeOnChange: true, invokeOnClickChanged: true);
  • Select a Drop Down value
burgersSelect.SelectByText("10 x Double Cheeseburgers");
  • Type text in a Text Input
firstNameTextInput.Text = "Anton";
  • Check a Checkbox
additionalSugarCheckbox.Check(
    isChecked: true, 
    invokeOnChange: true, 
    invokeOnClickChanged: true);
  • Click a Submit button
generateButton.Click();

HTML Element Asserts in Telerik Testing Framework

Telerik Testing Framework has a set of Assert classes to make validation of your HTML controls easier. You can verify a specified property of the control that it has a particular value or setting. If it doesn't have that setting/value an exception is thrown.

[TestMethod]
public void AssertRequiredFieldValidation()
{
    manager.ActiveBrowser.NavigateTo(
        "http://automatetheplanet.com/healthy-diet-menu-generator/");
    HtmlInputSubmit generateButton = 
    manager.ActiveBrowser.Find.ByName<HtmlInputSubmit>("_ninja_forms_field_28");
    generateButton.Click();
    var requiredField = 
    manager.ActiveBrowser.Find.ByXPath<HtmlContainerControl>(
        "//*[@id='ninja_forms_field_18_error']/p");

    requiredField.AssertContent().InnerText(
        ArtOfTest.Common.StringCompareType.Exact, 
        "This is a required field");
}

You can find more detailed information in the official HTML Asserts Guide.

 

If you enjoy my publications, Get Instant Access to my future PRO tips and trips.

 

The post Getting Started with Telerik Testing Framework C# in 10 Minutes appeared first on Automate The Planet.

All images are purchased from DepositPhotos.com and cannot be downloaded and used for free.
License Agreement

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)

Share

About the Author

Anton Angelov
CEO Automate The Planet
Bulgaria Bulgaria
Anton Angelov is an IT Consultant and Quality Assurance Architect at Innovative Lab. He is passionate about automation testing and designing test harness and tools, having the best industry development practices in mind. In addition, he is an active blogger and the founder of Automate The Planet. He strives to make the site one of the leading authorities in Automation Testing by presenting compelling articles, inspiring ardent discussions amongst the community. He is also one of the most-rated-answer authors of questions about Test Automation Frameworks (WebDriver) on Stack Overflow.

You may also be interested in...

Pro
Pro

Comments and Discussions

 
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web02 | 2.8.190306.1 | Last Updated 7 Mar 2016
Article Copyright 2016 by Anton Angelov
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid