Verifications
OpenText Functional Testing for Developers provides several methods in the SDKs that enable you to check the behavior and appearance of your application.
When these Boolean methods return a false value, the step and test are marked as failed in the HTML report, which also includes information about the step. However, unlike standard Asserts, no exception is thrown and the test does not stop running. This allows you, for example, to add steps that help recover from the step failure.
Note: When working with the JavaScript SDK, you can use the expect
and verify
functions to validate expected values. For details, see Setting up and working with the JavaScript SDK.
Verifying values in your application
To check that your application is behaving as expected, you can use the methods of the OpenText Functional Testing for Developers Verify class to check expected values or items.
For example, you can retrieve values or get objects from your application and then use the Verify class methods to check whether:
- The value in your application is equal, not equal, greater than, or less than an expected value.
- A retrieved string or collection contains an expected string or object.
- Any provided Boolean statement is true.
- A retrieved value or object in your application is empty or null or not empty/null.
- Whether a string retrieved from your application starts with or ends with an expected string.
You can add verification steps using the Test Recorder's graphical user interface, or add the verification steps directly in the code.
For details, see:
- Test Recorder.
- Verification methods in the .NET and Java SDK references.
- Validate values, in JavaScript.
Verifying the appearance of your application
To check that the objects in your application appear as expected, you can use the OpenText Functional Testing for Developers VerifyImageMatch and VerifyImageExists methods, which are available for every test object.
For details on these methods, see the .NET, Java, and JavaScript SDK references.
These methods compare an image you provide with a snapshot of object in the application, as it appears when the step is performed.
- You can verify that the image you provide is contained somewhere within your application object.
- You can verify that your application object is identical to the image you provide, or you can define acceptable tolerances or similarity levels.
-
You can also define a maskArea object that specifies areas to include and/or exclude from the image comparison, and then compare only the maskArea of each image.
The test results of all VerifyImage* steps display both the expected and actual images.
You can also use the OpenText Functional Testing for Developers LocateImage and CompareImages methods from the ImageUtils class to compare any two provided images, with or without defined tolerances or mask areas. These methods also return Boolean values, but a false return value does not affect the step or test status.
Note:
- When providing images to any of the above methods, it's best to create the image object (
system.drawing.image
orjava.awt.image
) using a BMP or PNG image and not a JPG image. The JPG compression algorithm loses information, such that a comparison between captured images may fail even if the items being evaluated are identical. -
Images can be displayed differently on different machines and operating systems because of different operations like stretching and resizing the image. Different resolution and font size can also effect on how the image looks like on different OSs and browsers.
When designing or running tests on different operating systems, adjust the tolerance and similarity level for image verification accordingly.
See the code samples for Java and .NET.
Create custom verifications and display them in the test report
OpenText Functional Testing for Developers includes a set of comprehensive methods that can be used for verifying values and images, as described in Verifying values in your application and in Verifying the appearance of your application. In some cases, you might want to include verifications in your script for which the Verify class does not have dedicated methods. In such cases, you can develop your own custom verification and use it in your scripts.
OpenText Functional Testing for Developers enables you to extend its verification capability and display your own custom verification in your test report by using the reportVerification method in the Reporter class. This capability is supported for all SDKs:
When running scripts that include calls to custom verifications, OpenText Functional Testing for Developers treats these verifications as it does built-in verifications and displays the verification results in the test report in the same manner.
Note: when passing a status of Warning to Report.reportVerification, the status is presented in the report as Passed.
Custom verification example
The following Java 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 OpenText Functional Testing for Developers API.
package com.hp.leanft.examples.verifications; import com.hp.lft.report.*; import com.hp.lft.sdk.web.*; import com.hp.lft.verifications.Verify; import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Test; import unittesting.UnitTestClassBase; public class CompleteExamples extends UnitTestClassBase { @BeforeClass public static void beforeClass() throws Exception { globalSetup(CompleteExamples.class); } @AfterClass public static void afterClass() throws Exception { globalTearDown(); } @Test public void verify_linkNavigatesToLaptopsPage() throws Exception { //Launch the Chrome browser. Browser browser = BrowserFactory.launch(BrowserType.CHROME); try{ //Navigate to the online shopping demo website. browser.navigate("http://www.advantageonlineshopping.com/#/"); //Click the "LAPTOPS" link browser.describe(Link.class, new LinkDescription.Builder() .tagName("SPAN").innerText("LAPTOPS").build()).click(); //Wait for the browser to complete the navigation. browser.sync(); //Verify that the correct Web page opens. Verify.areEqual("http://www.advantageonlineshopping.com/#/category/Laptops/1", browser.getURL(), "verify_linkNavigatesToLaptopsPage", "Verify that the browser's URL is as expected."); } catch(Error e){ Reporter.reportEvent("verify_linkNavigatesToLaptopsPage","Failed during test",Status.Failed, e); throw e; } finally{ browser.close(); } } // The example below shows usage of verification extensibility - implementing custom verifications @Test public void testTotalPrice() throws ReportException { 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) throws ReportException{ // Create a VerificationData object and set its name, description and operation. VerificationData 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. verificationData.verificationParameters.add(verificationParameter1); verificationData.verificationParameters.add(verificationParameter2); verificationData.verificationParameters.add(verificationParameter3); // Call a custom comparison method boolean 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); } else{ Reporter.reportVerification(Status.Failed, verificationData); } } }
The test report for this example looks like this:
See also: