Verification code samples (.NET SDK)

Basic verification example

The following example uses the Verify class to verify navigation to a page on a website.

public void Verify_linkNavigatesToLaptopsPage()
	//Launch the InternetExplorer browser.
	var browser = BrowserFactory.Launch(BrowserType.InternetExplorer);

	//Navigate to the New Tours website. 

	//Click the "LAPTOPS" link
	browser.Describe<ILink>(new LinkDescription
		TagName = @"SPAN",
		InnerText = @"LAPTOPS"

	//Wait for the browser to complete the navigation.

	//Verify that the correct page Web page opens.
	Verify.AreEqual("", browser.URL, "Verify_linkNavigatesToLaptopsPage", "Verify that the browser's URL is as expected.");


Back to top

Custom verification example

The following example shows how to use a custom verification method that verifies a sum of numbers. In this case, it is used to sum the price of two items in a shopping cart in a retail application.

The method receives three parameters: the two numbers to sum and the total.

The method is called in the test script, and the arguments passed are fetched from the application using UFT Developer API.

 // The example below shows usage of verification extensibility - implementing custom verifications
public void TestTotalPrice() 
	long price1;
	long price2;
	long totalPrice;
	// .
	// .
	// This is where you add code to: add two items to the shopping cart, find their price,
	// and update each item's price in the price1 and price2 variables.
	price1 = 100;
	price2 = 200;
	totalPrice = 300;

	// Then go to the check-out page, find the total price, add update the totalPrice variable with this value.
	// .
	// .
	// The following code calls the custom verification method verifyTotalSumOfNumbers.
	// The verification name and description are set and then passed as arguments along with the price arguments.
	String name = "Verify Total Price";
	String verificationDescription = "Verify that the total purchase price is indeed a sum of the items in the cart";
	VerifyTotalSumOfNumbers(price1, price2, totalPrice, name, verificationDescription);

	// .
	// .
	// Continue test logic
	// .
	// .


// This is the implementation of the custom verification method
protected void VerifyTotalSumOfNumbers(long num1, long num2,
					long total, String name,
					String verificationDescription)

	// Create a VerificationData object and set its name, description and operation.
	var verificationData = new VerificationData();
	verificationData.Name = name;
	verificationData.Description = verificationDescription;
	verificationData.OperationName = "totalSumOfNumbers";
	// Operation name helps better categorize the check made.
	// For example, for the same verification name ("Verify Total Price") you could also have
	// the “checkCurrency” operation (in another VerificationData object).

	// Create the verification parameters, set their label and values.
	VerificationParameter verificationParameter1 = new VerificationParameter("First Number", num1);
	VerificationParameter verificationParameter2 = new VerificationParameter("Second Number", num2);
	VerificationParameter verificationParameter3 = new VerificationParameter("Actual Sum", total);

	// Add the verification parameters to the parameters list of the verificationData object.

	// Call a custom comparison method
	bool sumMatches = (num1+num2 == total);

	// Check whether the sums match. If they do, return reportVerification with a passes status.
	// If not, return it with a failed status. The status is displayed in the test report.
	if (sumMatches){
		Reporter.ReportVerification(Status.Passed, verificationData);
		Reporter.ReportVerification(Status.Failed, verificationData);

Back to top

See also: