New to Telerik Test StudioStart a free 30-day trial

HTML Asserts

Telerik Testing Framework has a set of Assert classes to make validation of your HTML controls easier. An 'Assert' basically says "Verify the specified property of the control has the specified value or setting. If it doesn't have that setting/value, then throw an error and stop the test."

For example: AssertCheck.IsTrue() will verify the associated checkbox control is checked. If it isn't the framework will throw an assert exception and stop the test. If it is the test will simply continue to the next step. Nothing else happens for Asserts that pass validation.

Assert ClassClass DescriptionAssert methodMethod Description
AssertAttributeValidates the properties of an attribute.ValueChecks the string value of the specified attribute. Asserts if the expected value was not found.
ExistsAsserts if the specified attribute does not exist for this element.
AssertCheckValidates the checked state of check boxes and radio buttons.IsFalseAsserts if the control is checked.
IsTrueAsserts if the control is not checked.
AssertContentValidates the content of an HtmlControl.InnerMarkupAsserts if the inner HTML of the control does not match.
InnerTextAsserts if the inner text of the control does not match.
OuterMarkupAsserts if the outer HTML of the control does not match.
StartTagContentAsserts if the value of the tag for this HTML control does not match.
TextContentAsserts if the text content of the HTML control does not match.
AssertSelectValidates the various properties of an HtmlSelect.ItemsCountIsAsserts if the total number of items in the list does not match the expected value.
SelectedIndexAsserts if the index value of the currently selected item does not match the expected value.
SelectedTextAsserts if the text of the currently selected item does not match the expected value.
SelectedValueAsserts if the value of the currently selected item does not match the expected value.
TextExistsAsserts if the expected value was not found in the list of text's to display for this control.
TextExistsNotAsserts if the expected value not found in the list of text's to display for this control.
ValueExistsAsserts if the expected value was not found in the list of values for the items in this control.
ValueExistsNotAsserts if the expected value not found in the list of values for the items in this control.
AssertStyleValidates the various common styles of an HtmlControl.BoxAsserts if a particular box style does not match (e.g. margin, border, padding).
ColorAndBackgroundAsserts if a particular color or background style does not match (e.g. background color, background image, etc.) NOTE: AssertStyle.ColorAndBackground() automatically detects colors and will perform the proper validation regardless of the format of the color encoding attached to the style e.g. "red" will match "#FF0000".
DisplayAsserts if a particular display style does not match (e.g. width, height, display, position, etc.)
FontAsserts if a particular font style does not match (e.g. family, variant, size, etc.)
ListAsserts if a particular list style does not match (e.g. type, image, position, etc.)
TextAsserts if a particular text style does not match (e.g. word spacing, letter spacing, text align, etc.)
AssertTableValidates the common properties of an HtmlTable control.ColumnCountAsserts if the number of columns does not match the expected value.
ColumnRangeAsserts if the number of columns is not inside or not outside of the specified range.
ContainsAsserts if the table contains/does not contain the expected value.
RowCountAsserts if the number of rows does not match the expected value.
RowRangeAsserts if the number of rows is not inside or not outside of the specified range.

Now let's see how this works in action:

C#
// Attribute checks
HtmlSpan span = Find.ById<HtmlSpan>("Warning");
span.AssertAttribute().Exists("style");
span.AssertAttribute().Value("style", ArtOfTest.WebAii.Controls.HtmlControls.HtmlAsserts.StringCompareType.Contains, "color");
  
// Checkbox checks
HtmlInputCheckBox cbx = Find.ById<HtmlInputCheckBox>("Checkbox1");
cbx.AssertCheck().IsTrue();
cbx.Click();
cbx.AssertCheck().IsFalse();
  
// Content checks
span.AssertContent().InnerText(ArtOfTest.WebAii.Controls.HtmlControls.HtmlAsserts.StringCompareType.Contains, "Warning");
span.AssertContent().InnerText(ArtOfTest.WebAii.Controls.HtmlControls.HtmlAsserts.StringCompareType.NotContain, "Error");
span.AssertContent().StartTagContent(ArtOfTest.WebAii.Controls.HtmlControls.HtmlAsserts.StringCompareType.StartsWith, "<span");
  
HtmlDiv topdiv = Find.ById<HtmlDiv>("topmost");
topdiv.AssertContent().TextContent(ArtOfTest.WebAii.Controls.HtmlControls.HtmlAsserts.StringCompareType.Exact, "Top most text");
topdiv.AssertContent().InnerText(ArtOfTest.WebAii.Controls.HtmlControls.HtmlAsserts.StringCompareType.Exact, "Top most textMiddle level textInnermost text");
topdiv.AssertContent().OuterMarkup(ArtOfTest.WebAii.Controls.HtmlControls.HtmlAsserts.StringCompareType.Exact, "<DIV id=topmost>Top most text<DIV id=middle>Middle level text<DIV id=innermost>Innermost text</DIV></DIV></DIV>");
topdiv.AssertContent().InnerMarkup(ArtOfTest.WebAii.Controls.HtmlControls.HtmlAsserts.StringCompareType.Exact, "Top most text<DIV id=middle>Middle level text<DIV id=innermost>Innermost text</DIV></DIV>");
  
// Select checks
HtmlSelect select = Find.ById<HtmlSelect>("color_product");
select.AssertSelect().ItemsCountIs(ArtOfTest.WebAii.Controls.HtmlControls.HtmlAsserts.NumberCompareType.Equals, 5);
select.AssertSelect().SelectedIndex(ArtOfTest.WebAii.Controls.HtmlControls.HtmlAsserts.NumberCompareType.Equals, 0);
select.AssertSelect().SelectedText(ArtOfTest.WebAii.Controls.HtmlControls.HtmlAsserts.StringCompareType.Exact, "Color : Blue");
select.AssertSelect().SelectedValue(ArtOfTest.WebAii.Controls.HtmlControls.HtmlAsserts.StringCompareType.Exact, "Blue");
  
select.SelectByIndex(3);
select.AssertSelect().SelectedIndex(ArtOfTest.WebAii.Controls.HtmlControls.HtmlAsserts.NumberCompareType.Equals, 3);
select.AssertSelect().SelectedText(ArtOfTest.WebAii.Controls.HtmlControls.HtmlAsserts.StringCompareType.Exact, "Color : Orange");
select.AssertSelect().SelectedValue(ArtOfTest.WebAii.Controls.HtmlControls.HtmlAsserts.StringCompareType.Exact, "Orange");
  
select.AssertSelect().TextExists("Color : Black");
select.AssertSelect().TextExistsNot("Color : Magenta");
select.AssertSelect().ValueExists("Black");
select.AssertSelect().ValueExistsNot("Magenta");
  
// Style checks
NameValueCollection styles = span.Styles;
span.AssertStyle().Font(ArtOfTest.WebAii.Controls.HtmlControls.HtmlAsserts.HtmlStyleFont.Style, "italic");
span.AssertStyle().Text(ArtOfTest.WebAii.Controls.HtmlControls.HtmlAsserts.HtmlStyleText.TextAlign, "right");
span.AssertStyle().ColorAndBackground(ArtOfTest.WebAii.Controls.HtmlControls.HtmlAsserts.HtmlStyleColorAndBackground.Color, "red",
    ArtOfTest.WebAii.Controls.HtmlControls.HtmlAsserts.HtmlStyleType.Computed,
    ArtOfTest.WebAii.Controls.HtmlControls.HtmlAsserts.StringCompareType.Exact);
  
// Table checks
HtmlTable table = Find.ById<HtmlTable>("outertable1");
table.AssertTable().ColumnCount(ArtOfTest.WebAii.Controls.HtmlControls.HtmlAsserts.NumberCompareType.Equals, 3);
table.AssertTable().ColumnRange(ArtOfTest.Common.NumberRangeCompareType.InRange, 2, 5);
table.AssertTable().RowCount(ArtOfTest.WebAii.Controls.HtmlControls.HtmlAsserts.NumberCompareType.Equals, 3);
table.AssertTable().RowRange(ArtOfTest.Common.NumberRangeCompareType.OutsideRange, 1, 2);
table.AssertTable().Contains(ArtOfTest.WebAii.Controls.HtmlControls.HtmlAsserts.StringCompareType.Contains, "TD5");
table.AssertTable().Contains(ArtOfTest.WebAii.Controls.HtmlControls.HtmlAsserts.StringCompareType.NotContain, "TD37");

There are many other possible assert verification combinations built into the framework that you can take advantage of. The above examples are just a small subset of what is possible to help you get started implementing them in your code and crafting the kinds of verification you need for your particular website. Consult the API reference manual to learn about the other combinations.

Not finding the help you need?
Contact Support