New to Telerik Test Studio? Download free 30-day trial

How to Fail the Test on a Condition

There are two ways to accomplish this: Assert and Throw New Exception statements. Any unhandled exception within the coded step will fail the test step (and test) immediately.

Assert Statements

This is most easily achieved with standard (and non-coded) Verification and Wait steps. You can resort to code, however, for more in-depth scenarios. See here for more information.

The following code contains basic examples for demonstration purposes:

HtmlTable table = Find.ByExpression<HtmlTable>("id=Grid", "|", "tagIndex=table:1"); 
int r = table.Rows.Count; 
Assert.AreEqual(r, 10); 
Dim table As HtmlTable = Find.ByExpression(Of HtmlTable)("id=Grid", "|", "tagIndex=table:1") 
Dim r As Integer = table.Rows.Count 
Assert.AreEqual(r, 10) 

Throw New Exception

When an Assert statement is not appropriate, you can throw your own exception. In the example below, the FileNotFoundException will result in failure if the specified file does not exist on disk.

string fileName = "C:\test.txt"; 
if (System.IO.File.Exists(fileName) == true) 
    Log.WriteLine("File Exists: " + fileName); 
    throw new System.IO.FileNotFoundException("File does not exist: ", fileName); 
Dim fileName As String = "C:\test.txt" 
If System.IO.File.Exists(fileName) = True Then 
    Log.WriteLine("File Exists: " + fileName) 
    Throw New System.IO.FileNotFoundException("File does not exist: ", fileName) 
End If 
In this article
Not finding the help you need? Improve this article